怎么在VB6里改变command控件上字体的颜色????

解决方案 »

  1.   

    Option Explicit
    Dim c
    Dim d
    Dim a(), g(1) As Single
    Dim b
    Dim cmd_cap
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command1, Picture1, "你好")
         Command1.SetFocus
    End SubPrivate Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command2, Picture1, "Command2")
    End SubPrivate Sub Command3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command3, Picture1, "Command3")
    End SubPrivate Sub Command4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command4, Picture1, "Command4")
    End SubPrivate Sub Command5_LostFocus()
        Call setcolor(vbRed, vbWhite, Command5, Picture1, "Command5")
    End SubPrivate Sub Command5_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command5, Picture1, "Command5")
         Command5.SetFocus
    End SubPrivate Sub Command6_LostFocus()
        Call setcolor(vbRed, vbWhite, Command6, Picture1, "Command6")
    End SubPrivate Sub Command6_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command6, Picture1, "Command6")
         Command6.SetFocus
    End SubPrivate Sub Command7_LostFocus()
        Call setcolor(vbRed, vbWhite, Command7, Picture1, "Command7")
    End SubPrivate Sub Command7_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command7, Picture1, "Command7")
         Command7.SetFocus
    End SubPrivate Sub Command8_LostFocus()
        Call setcolor(vbRed, vbWhite, Command8, Picture1, "Command8")
    End SubPrivate Sub Command8_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
         c = X
         d = Y
         Call setcolor(vbWhite, vbBlue, Command8, Picture1, "Command8")
         Command8.SetFocus
    End SubPrivate Sub Form_Load()
    Picture1.Visible = False
    End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If c <> X And d <> Y And Command2.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command2, Picture1, "Command2")
        End If
        If c <> X And d <> Y And Command3.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command3, Picture1, "Command3")
        End If
        If c <> X And d <> Y And Command4.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command4, Picture1, "Command4")
        End If
        If c <> X And d <> Y And Command5.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command5, Picture1, "Command5")
        End If
        If c <> X And d <> Y And Command6.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command6, Picture1, "Command6")
        End If
        If c <> X And d <> Y And Command7.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command7, Picture1, "Command7")
        End If
        If c <> X And d <> Y And Command8.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command8, Picture1, "Command8")
        End If
        If c <> X And d <> Y And Command1.BackColor <> vbWhite Then
        Call setcolor(vbRed, vbWhite, Command1, Picture1, "你好")
        End If
    End SubSub setcolor(color1 As Long, color2 As Long, cmd As CommandButton, pic As PictureBox, cmd_str As String)
    pic.Cls
    cmd.Caption = ""
    pic.AutoRedraw = True
    cmd.BackColor = color2
    pic.BackColor = cmd.BackColor
    Set pic.Font = cmd.Font
    pic.Width = cmd.Width
    pic.Height = cmd.Height
    pic.ForeColor = color1pic.CurrentX = (pic.Width - pic.TextWidth(cmd_str)) / 2
    pic.CurrentY = (pic.Height - pic.TextHeight(cmd_str)) / 2
    pic.Print cmd_strSet cmd.Picture = pic.Image
    pic.AutoRedraw = False
    End SubPrivate Sub Form_Activate()
    Dim j, i
    If g(0) = 0 Then '原始值只記錄一次
     g(0) = Form1.ScaleWidth: g(1) = Form1.ScaleHeight '一開始表單的大小
     ReDim a(Form1.Controls.Count - 1, 5)
     j = 0
     For Each i In Form1.Controls '記錄每個物件的資料
       a(j, 0) = i.Name
       On Error Resume Next '避免某些物件沒有指定的屬性而錯誤
       a(j, 1) = i.Left:  a(j, 2) = i.Top
       a(j, 3) = i.Width:  a(j, 4) = i.Height
       a(j, 5) = i.FontSize
       On Error GoTo 0 '取消錯誤處理
      j = j + 1
     Next i
    End If
    End Sub
    Private Sub Form_Resize()
    Dim i, j
    If Form1.WindowState <> 1 And g(0) > 0 And g(1) > 0 Then
    '重算物件的新位置
     For i = 0 To Form1.Controls.Count - 1
      Set b = Controls(a(i, 0))
       On Error Resume Next
       b.Left = a(i, 1) / g(0) * Form1.ScaleWidth
       b.Top = a(i, 2) / g(1) * Form1.ScaleHeight
       b.Width = a(i, 3) / g(0) * Form1.ScaleWidth
       b.Height = a(i, 4) / g(1) * Form1.ScaleHeight
       If Form1.ScaleWidth / g(0) < Form1.ScaleHeight / g(1) Then
        b.FontSize = a(i, 5) / g(0) * Form1.ScaleWidth
       Else
        b.FontSize = a(i, 5) / g(1) * Form1.ScaleHeight
       End If
       On Error GoTo 0
       'If TypeOf b Is PictureBox Then
        'b.PaintPicture b.Picture, 0, 0, b.ScaleWidth, b.ScaleHeight
       'End If
      Set b = Nothing
     Next i
    End If
        Call setcolor(vbRed, vbWhite, Command2, Picture1, "Command2")
        Call setcolor(vbRed, vbWhite, Command3, Picture1, "Command3")
        Call setcolor(vbRed, vbWhite, Command4, Picture1, "Command4")
        Call setcolor(vbRed, vbWhite, Command5, Picture1, "Command5")
        Call setcolor(vbRed, vbWhite, Command6, Picture1, "Command6")
        Call setcolor(vbRed, vbWhite, Command7, Picture1, "Command7")
        Call setcolor(vbRed, vbWhite, Command8, Picture1, "Command8")
    End Sub
      

  2.   

    简单点的就用Microsoft Forms 2.0 Object Library控件组里的 CommandButton,设置其 ForeColor属性即可
      

  3.   

    但是Microsoft Forms 2.0 Object Library存在内存泄露问题,这里不推荐使用用api函数解决的方法:模块:Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As LongPrivate 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 LongPrivate 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 LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)'自画常量
    Private Const ODT_BUTTON = 4
    Private Const ODS_SELECTED = &H1
    '系统消息常量
    Private Const WM_DESTROY = &H2
    Private Const WM_DRAWITEM = &H2BPrivate 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 TypePrivate Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate 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 = 1Private Const DT_CENTER = &H1Public Enum TextVAligns
        DT_VCENTER = &H4
        DT_BOTTOM = &H8
    End Enum
    Private Const DT_SINGLELINE = &H20Private 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 SubPublic 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 FunctionPublic 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 SubPublic 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 SubPublic Sub SetButton(ByVal hWnd As Long, ByVal lForeColor As Long, Optional ByVal VAlign As TextVAligns = DT_CENTER)
        
        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 SubPublic Sub RemoveButton(ByVal hWnd As Long)
        RemoveProp hWnd, "VBTCustom"
        RemoveProp hWnd, "VBTForeColor"
        RemoveProp hWnd, "VBTVAlign"
    End Sub
    窗体:一个按钮
    Private Sub Form_Load()
        SetButton Command1.hWnd, vbRed, DT_VCENTER
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        RemoveButton Command1.hWnd
    End Sub呵呵!这也是我从一个本论坛的大侠那里学来的!在这里我要谢谢他!呵呵!为了帮别人小弟在这里引用你的了!