VB按钮如何设置颜色和按钮文字设置颜色 VB里如何改变命令按钮中文字的颜色?

作者&投稿:顾菁 (若有异议请与网页底部的电邮联系)

首先command的style要设为1,否则无法改变

然后在backcolor设置颜色

按钮的字体颜色不能改
如果想改字体颜色,简单点可以用image做按钮。

如果一定要,请看:

在工程中添加以下模块(Module): 
Module 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


右边该按钮的属性设置里都有。

请问在VB中,怎样改变按钮以及按钮表面字体的颜色?~

如果把按扭的COMMAND.STYLE=2你可以给按扭添加图片.即.COMMAND.PICTURE 中设置就可以了.但是颜色字体不能改.(至少我不会).不过你可以用LANBEL标签,跟LINE 直线做按扭,可以改变字体颜色,而且更漂亮,我做过的,你可以试一下的。

不能直接通过属性框修改按钮中文本的颜色
根本没有foreColor属性
下边是一个参考方法
首先请把要改的按钮的Style设置为1

在工程中添加以下模块(Module):
Module 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

百度贴吧回帖的时候,怎样改变字体颜色大小?
答:百度贴吧无法自行设置字体大小与颜色。签到10天以后,可以字体加粗,20天以后,可以飘红。这时,输入框右上方就会有AB两个按钮,A是红色,B是粗体。1、设置字体颜色。点击如图所示按钮即可将输入的内容设置为红色。2、设置字体加粗。点击上图中的“B”按钮,即可将输入内容加粗。

电脑如何设置屏幕文字的字体和背景颜色
答:1、按住键盘“win+R"键,调出命令提示符,输入”cmd“,点击确定。2、右键点击运行命令上方空白处,选择属性,进入属性设置。3、设置字体大小类型。点击字体,选择合适大小和类型,点击确定即可。4、设置屏幕文字颜色。点击颜色,勾选屏幕文字(T),下方选择合适颜色,点击确定即可。5、设置屏幕背景颜色。

如何让点击过的按钮,变颜色
答:以下是改变文本框字体的颜色!<input name="test" type="text" id="test" value="点击按钮看我变化"> <input name="a" type="button" id="a" Tvalue="black" value="黑色" onClick="document.getElementById('test').style.color=document.getElementById('test').Tvalue=this.Tvalue;">...

怎么设置qq聊天界面的字体大小和颜色
答:第三,点击一种喜欢的颜色,再于右侧拉动黑色三角形,选择颜色的深浅。怎样设置QQ聊天界面的字体大小和颜色 怎样设置QQ聊天界面的字体大小和颜色 第四,察看预览处的颜色是否喜欢,可以的话就点击“确定”按钮。怎样设置QQ聊天界面的字体大小和颜色 第五,如图点击“B”按钮,可将文字加粗;旁边的斜I,可...

PS通过定义画笔预设灵活设置水印的大小颜色和透明度
答:操作步骤 1.打开PS软件,按Ctrl+N组合键新建一个文档,这里设置文档宽度为500像素,高度为500像素,分辨率为300像素/英寸,最后单击“创建”按钮。2.按键盘上的T键切换到文字工具,在窗口中间的编辑区域点击鼠标可输入想要的水印文字,这里输入“清风徐来”,并设置好文字的字体和颜色。建议将文字颜色设...

如何修改ppt文字颜色?
答:选择你想要更改字体颜色的文本框或文本段落。在 PowerPoint 的顶部菜单栏中,点击"开始"选项卡。请点击输入图片描述 在"字体"组中,点击"字体颜色"按钮,它通常显示为一个带有"A"和红色下划线的图标。请点击输入图片描述 在弹出的颜色选择器中,你可以选择预设的颜色或自定义颜色。如果你想要自定义颜色,...

如何更改电脑字体颜色?
答:随着科技的发展,电脑已经成为我们日常生活中必不可少的工具。在使用电脑时,我们通常会注意到屏幕上的文字颜色,但是有时候文字颜色不够明显或者不够适合自己的口味,这时就需要对电脑字体的颜色进行调整。本文将从两个方面介绍如何调整电脑字体的颜色:一、在系统设置中调整 对于Windows系统,可以打开“控制...

按钮开关的接线图?
答:2. 按钮的脚接点说明:- 扩展材料:按钮开关符号用于表示型号,如LA、GQ、XB、LAY、Y090、AD16等,这些字母代表不同类别的按钮开关,包括塑料开关、金属按钮和信号指示灯等。- 按钮颜色参数符号涉及按钮开关的灯珠颜色和按钮头部颜色,常见颜色有R(红色)、G(绿色)、W(白色)、N(黑色)、B(蓝色...

问道手游怎么打彩色字?
答:问道彩色字怎么打 只有会员才能改变字体的颜色。在输入聊天文字的时候点 击聊天栏右侧的颜色按钮,选择颜色即可。或者在文字的前面 加入以下特殊符号就能改变文字的颜色:R-表示后面的字体为红色(red)G-表示后面的字体为绿色(green)B-表示后面的字体为蓝**lue)K-表示后面的字体为黑**lack)Y-表示后面的...

Excel 如何设定以下条件? A 列和B列里的文字不一样时 ,B列的文字变成...
答:选中B列---条件格式---公式--- =A1<>B1 ---格式---设置单元格格式---字体颜色设置成红色---确定