标题:求助,请教一个问题
只看楼主
dongdong12
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-2-6
 问题点数:0 回复次数:4 
求助,请教一个问题

大家好:


我是初学VB的菜鸟,请教一下如何用程序语言改变按钮属性文字的颜色?


谢谢

搜索更多相关主题的帖子: 如何 程序语言 
2006-02-06 09:58
weizheng
Rank: 1
等 级:新手上路
威 望:2
帖 子:286
专家分:0
注 册:2005-4-21
得分:0 
你是不是想改变按钮上的字体颜色?
如果是的话,试试这个:
?参考:
http://expert.csdn.net/Expert/topic/1481/1481737.xml?temp=.210048
 

[此贴子已经被作者于2006-2-6 21:42:39编辑过]


温和如玉,完美纯正。
2006-02-06 21:41
dongdong12
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-2-6
得分:0 

谢谢了!

就是想改变按钮上的字体颜色的,哪个是不复杂了点啊,都看不懂,有没有简便一点的阿

2006-02-07 10:04
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
得分:0 

本代码完全重绘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


本帖版权归ryu所有.如果引用本帖,请注明帖子的出处和作者;本帖如系引用,其版权归原作者所有.
2006-02-12 18:55
dongdong12
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-2-6
得分:0 
谢谢了!我明白该咋么作了。
2006-02-13 16:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-44544-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026146 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved