在VB中可以通过程序来改变程序的背景色及字体,但不可以改变其文字的颜色,请问有没有办法可以改变其字体的颜色?

解决方案 »

  1.   

    Option Explicit'==================================================================
    '  此模块可以设置按钮的前景色,调用的时后用SetButton ,例如:
    ' SetButton Command1.hWnd, vbBlue 则是将command1按钮前景色设为vbblue
    '  如果设了前景色,则程序结束时需调用RemoveButton 如:RemoveButton Command1.hWnd
    '  如果按钮中设置了图形,则调用的时后应加上参数DT_BOTTOM,如:
    ' SetButton Command1.hWnd, vbBlue, DT_BOTTOM
    '  注意:调用前应确定按钮的style属性被设为1
    '
    '   modExtButton.bas
    '   From Visual Basic Thunder
    '
    '   This module provides an easy way to change the text color
    '   of a VB CommandButton control. To use the code with a
    '   CommandButton, you should:
    '
    '   - Set the button's Style property to "Graphical" at
    '       design time.
    '
    '   - Optionally set its BackColor and Picture properties.
    '
    '   - Call SetButton in the Form_Load event:
    '       SetButton Command1.hWnd, vbBlue
    '       (You can do this multiple times during your program's
    '       execution, even without calling RemoveButton.)
    '
    '   - Call RemoveButton in the Form_Unload event:
    '       RemoveButton Command1.hWnd
    '==================================================================Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End TypePrivate 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)'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 = &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 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 = 1Private 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, -1, rct, DT_CENTER Or DT_SINGLELINE _
            Or vaEnd SubPublic Function ExtButtonProc(ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As LongDim lOldProc As Long
    Dim di As DRAWITEMSTRUCTlOldProc = 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 IfElseIf wMsg = WM_DESTROY Then
        ExtButtonUnSubclass hWndEnd IfEnd FunctionPublic Sub ExtButtonSubclass(hWndForm As Long)Dim l As Longl = GetProp(hWndForm, "ExtBtnProc")
    If l <> 0 Then
        'Already subclassed
        Exit Sub
    End IfSetProp hWndForm, "ExtBtnProc", _
        GetWindowLong(hWndForm, GWL_WNDPROC)
    SetWindowLong hWndForm, GWL_WNDPROC, AddressOf ExtButtonProcEnd SubPublic Sub ExtButtonUnSubclass(hWndForm As Long)Dim l As Longl = GetProp(hWndForm, "ExtBtnProc")
    If l = 0 Then
        'Isn't subclassed
        Exit Sub
    End IfSetWindowLong 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_VCENTER)Dim hWndParent As LonghWndParent = GetParent(hWnd)
    If GetProp(hWndParent, "ExtBtnProc") = 0 Then
        ExtButtonSubclass hWndParent
    End IfSetProp hWnd, "VBTCustom", 1
    SetProp hWnd, "VBTForeColor", lForeColor
    SetProp hWnd, "VBTVAlign", VAlignEnd SubPublic Sub RemoveButton(ByVal hWnd As Long)RemoveProp hWnd, "VBTCustom"
    RemoveProp hWnd, "VBTForeColor"
    RemoveProp hWnd, "VBTVAlign"End Sub
      

  2.   

    用 microsoft forms 2.0 object library 中的 Commandbutton 控件。
      

  3.   

    引用microsoft forms 2.0 object library 
    里面的按钮就可以,很简单`
      

  4.   

    实在要用command解决,加个pic控件可以了,那有那么复杂啊
    现在外部控件多得很的
      

  5.   

    就像楼上的各位说的,
    换别的可以设置前景色的都可的,
    不一定要button,
      

  6.   

    It's hard right?
    Try to use ImageBox.You will find it easy to work.
      

  7.   

    虽然别的控件可以实现不过还是难为了GGL123提供的方法,也可以参考一下实现的方式
      

  8.   

    非常简单:把 Button 的 Style属性 设为 1-Graphical, 再设置 BackColor 颜色就可以了。
      

  9.   

    是设置前景色诶
    我也搞了半天,想想也只有调用api函数了
      

  10.   

    用LABEL控件把,我一般都用这个来代替标准按钮的,资源占用更小。
      

  11.   

    你可以用check或是option來代替,把style設為1即可