1. Vb中,如何修改命令按鈕的顏色
用標准控制項是無法改變的。有兩種方法可以解決:
(1)採用圖片框來代替
(2)搜xp控制項,是windows xp風格的控制項(如命令按鈕、單選按鈕、復選框……)。通常都會有示例說明,顏色很豐富,也很漂亮
2. VB里如何改變命令按鈕中文字的顏色
不能直接通過屬性框修改按鈕中文本的顏色
根本沒有foreColor屬性
下邊是一個參考方法
首先請把要改的按鈕的Style設置為1
在工程中添加以下模塊(Mole):
Mole modExtButton.bas
Option Explicit
'==================================================================
' modExtButton.bas
'
' 本模塊可讓你改變命令按鈕的文本顏色。
' 使用方法:
'
' - 在設計時將文本的Style設為Graphical.
'
' - 隨意設定背景色和圖象屬性.
'
' - 在Form_Load中調用 SetButton :
' SetButton Command1.hWnd, vbBlue
' (你可以任意次的調用該過程甚至不必先調用 RemoveButton.)
'
' - 在Form_Unload中調用 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
3. 如何實現VB的命令按鈕command1單擊後按鈕上的文字變成紅色,再單擊變成默認的顏色
這個可以,首先要記錄下原來的顏色,然後變成紅色,最後根據記錄的顏色還原,但是電腦運行的速度很快,要是馬上還原顏色的話看不到效果,所以要做一下延時,這樣才能看到它變色,首先添加一個timer控制項,屬性interval設置成500(時間設為0.5妙)
enabled設為false(不可用)
Dim color as long
在Command1_click加入以下代碼
color=command1.backcolor
Command1.backcolor=vbred
Timer1.enable=true
說明,讓按鈕顏色變為紅色,讓時間記可用…
雙擊timer1
在Timer1_timer事件中添加代碼
command1.backcolor=color
timer1.enabled=false