大家好:
我是初学VB的菜鸟,请教一下如何用程序语言改变按钮属性文字的颜色?
谢谢
大家好:
我是初学VB的菜鸟,请教一下如何用程序语言改变按钮属性文字的颜色?
谢谢
?参考: http://expert.csdn.net/Expert/topic/1481/1481737.xml?temp=.210048 |
[此贴子已经被作者于2006-2-6 21:42:39编辑过]
谢谢了!
就是想改变按钮上的字体颜色的,哪个是不复杂了点啊,都看不懂,有没有简便一点的阿
本代码完全重绘BUTTON,缺点是CAPTION不能随便
改(同一窗体的button的caption全一样),要想不一样复制绿色代码.
模块:
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Public 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 Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public 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
Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const DT_CENTER = &H1
Public Const DT_SINGLELINE = &H20
Public Const BM_SETSTYLE = &HF4
Public Const BS_OWNERDRAW = &HB&
Public Const GWL_STYLE = (-16)
Public Const GWL_WNDPROC = (-4)
Public Const TRANSPARENT = 1
Public Const WM_CTLCOLORSTATIC = &H138
Public Const WM_DRAWITEM = &H2B
Public Const WM_SETFOCUS = &H7
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public 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
Global gHW As Long
Global lpPrevWndProc As Long
Private Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim DI As DRAWITEMSTRUCT
Select Case uMsg&
Case WM_DRAWITEM
Call CopyMemory(DI, ByVal lParam, Len(DI))
Call DrawButton(DI)
Case WM_CTLCOLORSTATIC
Call SendMessage(lParam, BM_SETSTYLE, BS_OWNERDRAW, False)
End Select
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function
Public Sub Hook()
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub
Private Sub DrawButton(DI As DRAWITEMSTRUCT)
Dim hBr As Long, a As Long
If DI.itemState = 16 Or DI.itemState = 0 Then
hBr = CreateSolidBrush(&H0&)
Call FillRect(DI.hdc, DI.rcItem, hBr)
With DI.rcItem
.Left = .Left + 1
.Top = .Top + 1
.Bottom = .Bottom - 1
.Right = .Right - 1
End With
hBr = CreateSolidBrush(&HFFFFFF)
Call FillRect(DI.hdc, DI.rcItem, hBr)
With DI.rcItem
.Left = .Left + 1
.Top = .Top + 1
.Bottom = .Bottom - 1
.Right = .Right - 1
End With
hBr = CreateSolidBrush(&H8000&)
Call FillRect(DI.hdc, DI.rcItem, hBr)
If hBr Then DeleteObject (hBr)
Else
hBr = CreateSolidBrush(&HC0C0C0)
Call FillRect(DI.hdc, DI.rcItem, hBr)
With DI.rcItem
.Left = .Left + 1
.Top = .Top + 1
.Bottom = .Bottom - 1
.Right = .Right - 1
End With
hBr = CreateSolidBrush(&H0&)
Call FillRect(DI.hdc, DI.rcItem, hBr)
With DI.rcItem
.Left = .Left + 1
.Top = .Top + 1
.Bottom = .Bottom - 1
.Right = .Right - 1
End With
hBr = CreateSolidBrush(&H8000&)
Call FillRect(DI.hdc, DI.rcItem, hBr)
If hBr Then DeleteObject (hBr)
End If
DI.rcItem.Top = DI.rcItem.Top + 2
DI.rcItem.Left = DI.rcItem.Left + 2
Call SetBkMode(DI.hdc, TRANSPARENT)
Call SetTextColor(DI.hdc, &HFFFF&) '文字颜色
Call DrawText(DI.hdc, "Command1", 8, DI.rcItem, DT_CENTER Or DT_SINGLELINE)
End Sub
窗体:
Private Sub Form_Load()
Call SendMessage(Command1.hWnd, BM_SETSTYLE, BS_OWNERDRAW, False)
gHW = Me.hWnd
Hook
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook
End Sub