vb.net按钮默认颜色 vb单击按钮改变背景颜色

VB中按钮的背景色怎么改VB的按钮的背景色修改步骤:
1)设置按钮的Style属性为VbButtonGraphical 。
2)在按钮的BackColor属性中选择需要的背景色或在程序代码中设置 。
Private Sub Form_Load()
Command1.BackColor = vbGreen
End SubStyle
属性

返回或设置一个值,该值用来指示控件的显示类型和行为 。在运行时是只读的 。
BackColor
属性,BackColor-返回或设置对象的背景颜色 。
效果:
VB按钮如何设置颜色和按钮文字设置颜色首先commandvb.net按钮默认颜色的style要设为1vb.net按钮默认颜色,否则无法改变
然后在backcolor设置颜色
按钮的字体颜色不能改
如果想改字体颜色vb.net按钮默认颜色,简单点可以用image做按钮 。
如果一定要vb.net按钮默认颜色 , 请看:
在工程中添加以下模块(Module):
Module modExtButton.bas
Option Explicit
'==================================================================
' modExtButton.bas
'
' 本模块可让vb.net按钮默认颜色你改变命令按钮的文本颜色 。
' 使用方法:
'
' - 在设计时将文本的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.Left1
rct.Right = rct.Right1
rct.Bottom = rct.Bottom1
rct.Top = rct.Top1
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 l0 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"
【vb.net按钮默认颜色 vb单击按钮改变背景颜色】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.net怎么实现单击数字区域的按钮使按钮变红色,单击运算符按钮变蓝色同时数字区按钮还原?紧急在线等…您好,这种方法可以通过按钮vb.net按钮默认颜色的MouseDown和MouseUp事件来实现,比如说 , 当单击按钮RedColor(RedColor为这个按钮的Name属性的属性值)的时候,要使按钮的颜色变成红色,这可以在代码视图中的控件选择下拉列表中选择RedColor这个控件,然后在右边的事件下拉列表中选择MouseDown事件(当按下鼠标时发生),这时,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颜色参数,在RGB颜色系统中,R代表红色,G代表绿色,B代表蓝色,根据这三种颜色的不同程度的调和,就会得到不同的颜色,如果您要得到深色的天蓝色 , 则可以设置FromArgb(0, 191, 255) , 这个颜色转换为HTML中的RGB颜色就是#00BFFF 。
当然颜色还原最好是写在MouseUp事件里面,这样就不必在单击其他按钮时来还原上一次按下的按钮的颜色,在这里,为vb.net按钮默认颜色了方便描述 , vb.net按钮默认颜色我们假设按钮的默认颜色为白色 , 也就是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的含义是当鼠标弹起时发生,所以在这个事件就可以设置按钮颜色的还原,即添加下列代码来实现:
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
当然 , 这个vb.net按钮默认颜色你只要分析了控件事件代码的基本结构,一切就一目了然了,控件事件代码的基本结构如下:
[作用域] Sub 事件名([参数列表]) Handles 控件事件1 [, 控件事件2, 控件事件3...]
[事件代码区域]
End Sub
如果此回答对您有帮助,别忘了采纳哦,如果没有解决您的问题,您可以在互联网上寻找更多关于此问题的解决方案,谢谢vb.net按钮默认颜色!
VB.NET更改按键颜色后怎样改回默认?Dim LngC1 As Color, LngC2 As Color
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Button1.BackColor = Color.Blue
Me.Button1.ForeColor = Color.Gold
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LngC1 = Me.Button1.BackColor
LngC2 = Me.Button1.ForeColor
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Button1.BackColor = LngC1
Me.Button1.ForeColor = LngC2
End Sub
vb.net按钮默认颜色的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于vb单击按钮改变背景颜色、vb.net按钮默认颜色的信息别忘了在本站进行查找喔 。

    推荐阅读