窗體通過修改BackColor屬性來改變背景色。
我們提供的服務有:成都網(wǎng)站設計、網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、興業(yè)ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的興業(yè)網(wǎng)站制作公司
按鈕先要修改它的Style屬性為Graphical,然后再設置BackColor屬性就可以更改背景色了。
你可以這樣的解決你的問題:
(我用的是vs。net2005)
button1.backcolor=transprent
這樣你自己好好的試一試吧,我試了一下,感覺不錯。
您好,這種方法可以通過按鈕的MouseDown和MouseUp事件來實現(xiàn),比如說,當單擊按鈕RedColor(RedColor為這個按鈕的Name屬性的屬性值)的時候,要使按鈕的顏色變成紅色,這可以在代碼視圖中的控件選擇下拉列表中選擇RedColor這個控件,然后在右邊的事件下拉列表中選擇MouseDown事件(當按下鼠標時發(fā)生),這時,Visual Studio會自動生成如下代碼:
Private Sub RedColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RedColor.MouseDown
End Sub
接下來就在當前生成的代碼中寫入如下代碼:
RedColor.BackColor = Color.FromArgb(255, 0, 0)
這句代碼的作用是利用Color下的FromArgb方法來設置RGB顏色參數(shù),在RGB顏色系統(tǒng)中,R代表紅色,G代表綠色,B代表藍色,根據(jù)這三種顏色的不同程度的調(diào)和,就會得到不同的顏色,如果您要得到深色的天藍色,則可以設置FromArgb(0, 191, 255),這個顏色轉(zhuǎn)換為HTML中的RGB顏色就是#00BFFF。
當然顏色還原最好是寫在MouseUp事件里面,這樣就不必在單擊其他按鈕時來還原上一次按下的按鈕的顏色,在這里,為了方便描述,我們假設按鈕的默認顏色為白色,也就是FromArgb方法設置為FromArgb(255, 255, 255)的時候,所標示的顏色就是白色
所以選擇了RedColor按鈕的MouseUp事件后,Visual Studio會自動生成下列代碼:
Private Sub RedColor_MouseUp(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RedColor.MouseUp
End Sub
MouseUp的含義是當鼠標彈起時發(fā)生,所以在這個事件就可以設置按鈕顏色的還原,即添加下列代碼來實現(xiàn):
RedColor.BackColor = Color.FromArgb(255, 255, 255)
當然,這就編寫好了一個按鈕按下時變色,回彈時還原的功能,至于另外一個按鈕的功能,這與這個按鈕的功能一樣,只是代碼有一些細微的差別而已,假設這個按鈕的Name屬性的屬性值為BlueColor,相應的代碼對應如下:
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown
BlueColor.BackColor = Color.FromArgb(0, 0, 255) '藍色的FromArgb
End Sub
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown
BlueColor.BackColor = Color.FromArgb(255, 255, 255)
End Sub
另外,為了避免代碼的冗余,可以將RedColor和BlueColor的MouseUp事件進行合并,這合并后的代碼如下:
Private Sub BlueColor_MouseDown(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueColor.MouseDown, RedColor.MouseUp
BlueColor.BackColor = Color.FromArgb(255, 255, 255)
RedColor.BackColor = Color.FromArgb(255, 255, 255)
End Sub
當然,這個你只要分析了控件事件代碼的基本結(jié)構(gòu),一切就一目了然了,控件事件代碼的基本結(jié)構(gòu)如下:
[作用域] Sub 事件名([參數(shù)列表]) Handles 控件事件1 [, 控件事件2, 控件事件3...]
[事件代碼區(qū)域]
End Sub
如果此回答對您有幫助,別忘了采納哦,如果沒有解決您的問題,您可以在互聯(lián)網(wǎng)上尋找更多關于此問題的解決方案,謝謝!
首先command的style要設為1,否則無法改變
然后在backcolor設置顏色
按鈕的字體顏色不能改
如果想改字體顏色,簡單點可以用image做按鈕。
如果一定要,請看:
在工程中添加以下模塊(Module):?
Module?modExtButton.bas?
Option?Explicit?
'==================================================================?
'?modExtButton.bas?
'?
'?本模塊可讓你改變命令按鈕的文本顏色。?
'?使用方法:?
'?
'?-?在設計時將文本的Style設為Graphical.?
'?
'?-?隨意設定背景色和圖象屬性.?
'?
'?-?在Form_Load中調(diào)用?SetButton?:?
'?SetButton?Command1.hWnd,?vbBlue?
'?(你可以任意次的調(diào)用該過程甚至不必先調(diào)用?RemoveButton.)?
'?
'?-?在Form_Unload中調(diào)用?RemoveButton?:?
'?RemoveButton?Command1.hWnd?
'?
'==================================================================?
Private?Type?RECT?
Left?As?Long?
Top?As?Long?
Right?As?Long?
Bottom?As?Long?
End?Type?
Private?Declare?Function?GetParent?Lib?"user32"?_?
(ByVal?hWnd?As?Long)?As?Long?
Private?Declare?Function?GetWindowLong?Lib?"user32"?Alias?_?
"GetWindowLongA"?(ByVal?hWnd?As?Long,?_?
ByVal?nIndex?As?Long)?As?Long?
Private?Declare?Function?SetWindowLong?Lib?"user32"?Alias?_?
"SetWindowLongA"?(ByVal?hWnd?As?Long,?ByVal?nIndex?As?Long,?_?
ByVal?dwNewLong?As?Long)?As?Long?
Private?Const?GWL_WNDPROC?=?(-4)?
Private?Declare?Function?GetProp?Lib?"user32"?Alias?"GetPropA"?_?
(ByVal?hWnd?As?Long,?ByVal?lpString?As?String)?As?Long?
Private?Declare?Function?SetProp?Lib?"user32"?Alias?"SetPropA"?_?
(ByVal?hWnd?As?Long,?ByVal?lpString?As?String,?_?
ByVal?hData?As?Long)?As?Long?
Private?Declare?Function?RemoveProp?Lib?"user32"?Alias?_?
"RemovePropA"?(ByVal?hWnd?As?Long,?_?
ByVal?lpString?As?String)?As?Long?
Private?Declare?Function?CallWindowProc?Lib?"user32"?Alias?_?
"CallWindowProcA"?(ByVal?lpPrevWndFunc?As?Long,?_?
ByVal?hWnd?As?Long,?ByVal?Msg?As?Long,?ByVal?wParam?As?Long,?_?
ByVal?lParam?As?Long)?As?Long?
Private?Declare?Sub?CopyMemory?Lib?"kernel32"?Alias?"RtlMoveMemory"?_?
(Destination?As?Any,?Source?As?Any,?ByVal?Length?As?Long)?
'Owner?draw?constants?
Private?Const?ODT_BUTTON?=?4?
Private?Const?ODS_SELECTED?=?H1?
'Window?messages?we're?using?
Private?Const?WM_DESTROY?=?H2?
Private?Const?WM_DRAWITEM?=?H2B?
Private?Type?DRAWITEMSTRUCT?
CtlType?As?Long?
CtlID?As?Long?
itemID?As?Long?
itemAction?As?Long?
itemState?As?Long?
hwndItem?As?Long?
hDC?As?Long?
rcItem?As?RECT?
itemData?As?Long?
End?Type?
Private?Declare?Function?GetWindowText?Lib?"user32"?Alias?_?
"GetWindowTextA"?(ByVal?hWnd?As?Long,?ByVal?lpString?As?String,?_?
ByVal?cch?As?Long)?As?Long?
'Various?GDI?painting-related?functions?
Private?Declare?Function?DrawText?Lib?"user32"?Alias?"DrawTextA"?_?
(ByVal?hDC?As?Long,?ByVal?lpStr?As?String,?ByVal?nCount?As?Long,?_?
lpRect?As?RECT,?ByVal?wFormat?As?Long)?As?Long?
Private?Declare?Function?SetTextColor?Lib?"gdi32"?(ByVal?hDC?As?Long,?_?
ByVal?crColor?As?Long)?As?Long?
Private?Declare?Function?SetBkMode?Lib?"gdi32"?(ByVal?hDC?As?Long,?_?
ByVal?nBkMode?As?Long)?As?Long?
Private?Const?TRANSPARENT?=?1?
Private?Const?DT_CENTER?=?H1?
Public?Enum?TextVAligns?
DT_VCENTER?=?H4?
DT_BOTTOM?=?H8?
End?Enum?
Private?Const?DT_SINGLELINE?=?H20?
Private?Sub?DrawButton(ByVal?hWnd?As?Long,?ByVal?hDC?As?Long,?_?
rct?As?RECT,?ByVal?nState?As?Long)?
Dim?s?As?String?
Dim?va?As?TextVAligns?
va?=?GetProp(hWnd,?"VBTVAlign")?
'Prepare?DC?for?drawing?
SetBkMode?hDC,?TRANSPARENT?
SetTextColor?hDC,?GetProp(hWnd,?"VBTForeColor")?
'Prepare?a?text?buffer?
s?=?String$(255,?0)?
'What?should?we?print?on?the?button??
GetWindowText?hWnd,?s,?255?
'Trim?off?nulls?
s?=?Left$(s,?InStr(s,?Chr$(0))?-?1)?
If?va?=?DT_BOTTOM?Then?
'Adjust?specially?for?VB's?CommandButton?control?
rct.Bottom?=?rct.Bottom?-?4?
End?If?
If?(nState?And?ODS_SELECTED)?=?ODS_SELECTED?Then?
'Button?is?in?down?state?-?offset?
'the?text?
rct.Left?=?rct.Left?+?1?
rct.Right?=?rct.Right?+?1?
rct.Bottom?=?rct.Bottom?+?1?
rct.Top?=?rct.Top?+?1?
End?If?
DrawText?hDC,?s,?Len(s),?rct,?DT_CENTER?Or?DT_SINGLELINE?_?
Or?va?
End?Sub?
Public?Function?ExtButtonProc(ByVal?hWnd?As?Long,?_?
ByVal?wMsg?As?Long,?ByVal?wParam?As?Long,?_?
ByVal?lParam?As?Long)?As?Long?
Dim?lOldProc?As?Long?
Dim?di?As?DRAWITEMSTRUCT?
lOldProc?=?GetProp(hWnd,?"ExtBtnProc")?
ExtButtonProc?=?CallWindowProc(lOldProc,?hWnd,?wMsg,?wParam,?lParam)?
If?wMsg?=?WM_DRAWITEM?Then?
CopyMemory?di,?ByVal?lParam,?Len(di)?
If?di.CtlType?=?ODT_BUTTON?Then?
If?GetProp(di.hwndItem,?"VBTCustom")?=?1?Then?
DrawButton?di.hwndItem,?di.hDC,?di.rcItem,?_?
di.itemState?
End?If?
End?If?
ElseIf?wMsg?=?WM_DESTROY?Then?
ExtButtonUnSubclass?hWnd?
End?If?
End?Function?
Public?Sub?ExtButtonSubclass(hWndForm?As?Long)?
Dim?l?As?Long?
l?=?GetProp(hWndForm,?"ExtBtnProc")?
If?l??0?Then?
'Already?subclassed?
Exit?Sub?
End?If?
SetProp?hWndForm,?"ExtBtnProc",?_?
GetWindowLong(hWndForm,?GWL_WNDPROC)?
SetWindowLong?hWndForm,?GWL_WNDPROC,?AddressOf?ExtButtonProc?
End?Sub?
Public?Sub?ExtButtonUnSubclass(hWndForm?As?Long)?
Dim?l?As?Long?
l?=?GetProp(hWndForm,?"ExtBtnProc")?
If?l?=?0?Then?
'Isn't?subclassed?
Exit?Sub?
End?If?
SetWindowLong?hWndForm,?GWL_WNDPROC,?l?
RemoveProp?hWndForm,?"ExtBtnProc"?
End?Sub?
Public?Sub?SetButton(ByVal?hWnd?As?Long,?_?
ByVal?lForeColor?As?Long,?_?
Optional?ByVal?VAlign?As?TextVAligns?=?DT_VCENTER)?
Dim?hWndParent?As?Long?
hWndParent?=?GetParent(hWnd)?
If?GetProp(hWndParent,?"ExtBtnProc")?=?0?Then?
ExtButtonSubclass?hWndParent?
End?If?
SetProp?hWnd,?"VBTCustom",?1?
SetProp?hWnd,?"VBTForeColor",?lForeColor?
SetProp?hWnd,?"VBTVAlign",?VAlign?
End?Sub?
Public?Sub?RemoveButton(ByVal?hWnd?As?Long)?
RemoveProp?hWnd,?"VBTCustom"?
RemoveProp?hWnd,?"VBTForeColor"?
RemoveProp?hWnd,?"VBTVAlign"?
End?Sub?
'然后回到FORM中:?
'添加CommandButton,不必更改它們的名稱,將它們的Style設為Graphical,給第3個按鈕設置一幅圖片。?
'CommandButton也可以放置在一個容器如PictureBox或Frame中,模塊會判斷,如果需要的話將CommandButton的容器也子類化。?
'在Form中的代碼:?
Private?Sub?Form_Load()?
'Initialize?each?button?color.?
SetButton?Command1.hWnd,?vbRed?
SetButton?Command2.hWnd,?H8000?'深綠色?
'Assign?this?one?a?DT_BOTTOM?alignment?because?
SetButton?Command3.hWnd,?vbBlue,?DT_BOTTOM?'含有圖片,將文本放置在按鈕底部?
SetButton?Command4.hWnd,?H8080?'暗棕黃色?
End?Sub?
Private?Sub?Form_Unload(Cancel?As?Integer)?
'手動解除按鈕的子類化?
'這并不是必須的?
RemoveButton?Command1.hWnd?
RemoveButton?Command2.hWnd?
RemoveButton?Command3.hWnd?
RemoveButton?Command4.hWnd?
End?Sub?
For?m?=?0?To?9?
SetButton?CmdNum(m).hWnd,?vbBlue?
Next?
For?n?=?1?To?4?
SetButton?CmdCal(n).hWnd,?vbRed?
Next?
For?l?=?2?To?4?
SetButton?CmdOth(l).hWnd,?vbRed?
Next