各位大虾:
我遇到一个问题
创建出来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,只是添加了可以换背景色的功能呢?
谢谢啦!!!

解决方案 »

  1.   

    没那么复杂吧,不用API。用Microsoft Forms 2.0 Object Library中的CommandButton控件,可以随意设置背景颜色。
      

  2.   

    一楼已经说得够明白了,不使用VB标准控件库,而是使用扩展的控件库,即在“部件”里选择“Microsoft Forms 2.0 Object Library”,然后使用这个库里的COMMANDBUTTON就可以了。
      

  3.   

    Dim WithEvents vbBtn As VB.CommandButton 改为Private WithEvents vbBtn As VB.CommandButton
      

  4.   

    Option Explicit
    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
      

  5.   

    选择"Microsoft Forms 2.0 Object Library",然后用以下代码修改背景颜色:Dim WithEvents Button1 As MSForms.CommandButton
    Private Sub Form_Load()
        Set Button1 = Me.Controls.Add("Forms.CommandButton.1", "btn1")
        Button1.Visible = True
        Button1.BackColor = vbRed
    End Sub
      

  6.   

    好像这样变没有区别吧
    Private定义和Dim得到的结果没有区别吧可能真要用Microsoft Forms 2.0 Object Library?
    但是我加载了Microsoft Forms 2.0 Form
    就出现了Designers
    是不是我不应该这样加载阿请教一下阿
      

  7.   

    老张的代码背景颜色是变了,不过Command的样式可变了,好像CheckBox
      

  8.   

    五楼的代码和我写的代码结果是一样的阿
    我的代码就是那个效果阿,你没有测试过吗?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没有这个要求阿?请教一下哦
      

  9.   

    由于Microsoft Forms 2.0 Object Library是扩展控件库,当工程中没有明显引用时,需要设置工程属性中的生成选项,即取消默认的“删除有关未使用的ActiveX控件的信息”。
      

  10.   

    你把老张的代码中:
    NewStyle = NewStyle Or &H4 ' Or &H40000  '
    这句删除(或注释掉),它就仍然是 CommandButton 。别人的代码你还是仔细看看,动一下脑筋吧~~~  ^_^
      

  11.   


    删除有关未使用的ActiveX控件的信息。这句很重要!
      

  12.   

    我试过了,8楼可以。但要先取消默认的“删除有关未使用的ActiveX控件的信息”。
      

  13.   


    要是去掉这句还能显示背景颜色吗?^_^问题解决了
    lyserver 的方法可行哦谢谢啦