各位大虾:
我遇到一个问题
创建出来button后,我想修改button的背景颜色,例如改为blue or red。
但是.style是只读的,不能修改
我查了可以用api函数SetWindowLong这个函数来修改,但是参数如何设定才能达到效果一直搞不出来。
请各位大虾帮我看看Option Explicit
Dim WithEvents vbBtn As VB.CommandButton
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000Private Sub Form_Load()
Dim NewStyle As Long, NewStyle1 As Long
Set vbBtn = Form1.Controls.Add("VB.CommandButton", "vbBtn", Me)
NewStyle = GetWindowLong(vbBtn.hwnd, GWL_STYLE)
NewStyle = NewStyle Or &H4 ' Or &H40000 '
Call SetWindowLong(vbBtn.hwnd, GWL_STYLE, NewStyle)
With vbBtn
.Left = 500
.Top = 500
.Width = 800
.Caption = "aa"
.Visible = True
.BackColor = &HFF00&
.CausesValidation = True
.Enabled = True
' .Refresh
'.Style = 1
End With
End Sub这个代码使普通的Button变成了Radio Button,但是他没有了button的那个点击按下的动作了。
如何设置参数才能使这个button是正常的Button,只是添加了可以换背景色的功能呢?
谢谢啦!!!
我遇到一个问题
创建出来button后,我想修改button的背景颜色,例如改为blue or red。
但是.style是只读的,不能修改
我查了可以用api函数SetWindowLong这个函数来修改,但是参数如何设定才能达到效果一直搞不出来。
请各位大虾帮我看看Option Explicit
Dim WithEvents vbBtn As VB.CommandButton
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000Private Sub Form_Load()
Dim NewStyle As Long, NewStyle1 As Long
Set vbBtn = Form1.Controls.Add("VB.CommandButton", "vbBtn", Me)
NewStyle = GetWindowLong(vbBtn.hwnd, GWL_STYLE)
NewStyle = NewStyle Or &H4 ' Or &H40000 '
Call SetWindowLong(vbBtn.hwnd, GWL_STYLE, NewStyle)
With vbBtn
.Left = 500
.Top = 500
.Width = 800
.Caption = "aa"
.Visible = True
.BackColor = &HFF00&
.CausesValidation = True
.Enabled = True
' .Refresh
'.Style = 1
End With
End Sub这个代码使普通的Button变成了Radio Button,但是他没有了button的那个点击按下的动作了。
如何设置参数才能使这个button是正常的Button,只是添加了可以换背景色的功能呢?
谢谢啦!!!
Private WithEvents vbBtn As VB.CommandButton
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000Private Sub Form_Load()
Dim NewStyle As Long, NewStyle1 As Long
Set vbBtn = Form1.Controls.Add("VB.CommandButton", "vbBtn", Me)
vbBtn.Enabled = False
NewStyle = GetWindowLong(vbBtn.hwnd, GWL_STYLE)
NewStyle = NewStyle Or &H4 ' Or &H40000 '
Call SetWindowLong(vbBtn.hwnd, GWL_STYLE, NewStyle)
With vbBtn
.Left = 500
.Top = 500
.Width = 800
.Caption = "aa"
.Visible = True
.BackColor = &HFF00&
.CausesValidation = True
.Enabled = True
' .Refresh
'.Style = 1
End With
vbBtn.Enabled = True
End Sub
Private Sub vbBtn_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox "您选中的是动态增加的按钮!"
End Sub
Private Sub Form_Load()
Set Button1 = Me.Controls.Add("Forms.CommandButton.1", "btn1")
Button1.Visible = True
Button1.BackColor = vbRed
End Sub
Private定义和Dim得到的结果没有区别吧可能真要用Microsoft Forms 2.0 Object Library?
但是我加载了Microsoft Forms 2.0 Form
就出现了Designers
是不是我不应该这样加载阿请教一下阿
我的代码就是那个效果阿,你没有测试过吗?click响应的效果我知道怎么加阿
我的目的是不改变button的任何属性,只是能修改button的颜色。
我想知道如何加我已经添加了Microsoft Forms 2.0 Object Library
可以看到效果了
但是我有一个疑惑,我必须先在Form窗口内画一个这个button,然后才能起效
Set Button1 = Me.Controls.Add("Forms.CommandButton.1", "btn1")不然,会出现报错信息:
Run-time error"50153"
'Form.CommandButton.1' cannot be added because it is referenced but not in use by any items in the project. To correct this, uncheck 'Remove information about unused ActiveX Controls' in Project Options.好像说要在form内先画好一个实实在在的button,然后才能这样动态生成?
为什么一定要先画一个实在的button呢?
普通的VB.commandbutton没有这个要求阿?请教一下哦
NewStyle = NewStyle Or &H4 ' Or &H40000 '
这句删除(或注释掉),它就仍然是 CommandButton 。别人的代码你还是仔细看看,动一下脑筋吧~~~ ^_^
删除有关未使用的ActiveX控件的信息。这句很重要!
要是去掉这句还能显示背景颜色吗?^_^问题解决了
lyserver 的方法可行哦谢谢啦