改变VB 6.0命令按钮的Caption文本颜色

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。


'*******************对命令按钮进行颜色设置**********************
Private Sub Command2_Click() '改为蓝色
cmdButtonColorCaption Command1, vbBlue
End Sub
Private Sub Command3_Click() '使失效
cmdButtonDisenable Command1
End Sub
Private Sub Command4_Click() '使能
cmdButtonEnableColorCaption Command1, vbBlue
End Sub
Private Sub Form_Load()
'注意Command1在编程时一定要将其Style设为1(Graphical)
cmdButtonColorCaption Command1, vbRed '初始设为红色
End Sub
Private Sub FORM_Unload(Cancel As Integer) '释放
cmdButtonApiRemove Command1
End Sub
'*****************************************






'*****************************************
'*
'* 第二部分:cmdButtonColorCaption.bas
'*
'*****************************************
Option Explicit
'=============================================================================================================================
'
' cmdButtonColorCaption.bas
'
' 本模块通过API,可以改变VB 6.0命令按钮的Caption文本颜色。
'
' 使用方法:
'
' 一、在编程时将按钮的Style设为Graphical
'
' 二、在运行时调用以下3个过程
' cmdButtonColorCaption(cbControl As Control, ccColor As Long) '改变按钮的Caption颜色:按钮cbControl,颜色ccColor
' cmdButtonEnableColorCaption(cbControl As Control, ccColor As Long) '“使能”按钮的各项功能:按钮cbControl,颜色ccColor
' cmdButtonDisenable(cbControl As Control) '使按钮的各项功能失效:按钮cbControl
'
' 三、在FORM_Unload中调用以下过程
' cmdButtonApiRemove(cbControl As Control) '移除对按钮的API设置
'
'=============================================================================================================================
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 L

ong) 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.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
DrawText hDC, S, LenB(StrConv(S, vbFromUnicode)), 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 L <> 0 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"
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

'--------------------------------------------------------------------------------------------------------------------------------
'
'各种颜色通用
Public Sub cmdButtonColorCaption(cbControl As Control, ccColor As Long) '改变按钮的Caption颜色:按钮cbControl,颜色ccColor
If cbControl.Style = 0 Then
MsgBox & "的Style属性应设为1(Graphical)"
ElseIf cbControl.Enabled = True Then
SetButton cbControl.hWnd, ccColor
'以下刷新按钮cbControl
cbControl.Visible = False
cbControl.Visible = True
End If
End Sub
Public Sub cmdButtonEnableColorCaption(cbControl As Control, ccColor As Long) '“使能”按钮的各项功能:按钮cbControl,颜色ccColor
cbControl.Enabled = True
cmdButtonColorCaption cbControl, ccColor
End Sub
Public Sub cmdButtonDisenable(cbControl As Control) '使按钮的各项功能失效:按钮cbControl
RemoveButton cbControl.hWnd
cbControl.Enabled = False
End Sub
Public Sub cmdButtonApiRemove(cbControl As Control) '移除对按钮的API设置
RemoveButton cbControl.hWnd
End Sub

相关文档
最新文档