关于command按钮的问题!

ynhgk 2003-01-29 10:52:09
请问如何改变connand按钮字体的颜色!谢谢!
...全文
210 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfsteven1 2003-01-30
  • 打赏
  • 举报
回复
呵呵,还是用背景比较好。至于在背景上写字还是画画就随意了。
holydiablo 2003-01-29
  • 打赏
  • 举报
回复
我给的代码就是
change forecolor啊
lxcc 2003-01-29
  • 打赏
  • 举报
回复
我觉得 还不如作一图片,上面写你的按钮文字,爱怎么折腾都可以!
ynhgk 2003-01-29
  • 打赏
  • 举报
回复
改变背景色我知道,可是我要改变字体颜色啊!
有什么方法如用API什么的来做到啊!

holydiablo 2003-01-29
  • 打赏
  • 举报
回复
Change the ForeColor of the text in a command button.
Option Explicit

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 Long) 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

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 SetButtonForecolor(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


To use this, set the target command button's Style property to 1 - Graphical

EXAMPLES:

To set command button forecolor(doesn't have to be in form_load)

Private Sub Form_Load()
SetButtonForecolor Command1.Hwnd, vbBlue
End Sub

That will set Command1's Forecolor to Blue

To remove the color(can be put anywhere)

Private Sub Command2_Click()
RemoveButton Command1.Hwnd
Command1.Refresh
End Sub
griefforyou 2003-01-29
  • 打赏
  • 举报
回复
把Style设为1 ,可以改背景色
zw_yu 2003-01-29
  • 打赏
  • 举报
回复
好像不能改变吧,要么用图片
northwolves 2003-01-29
  • 打赏
  • 举报
回复
1个LABEL4根LINE 或 1个PICTURE 4 根线 画之
Sean918 2003-01-29
  • 打赏
  • 举报
回复
回楼上,最好的办法就是自己用一个 picturebox 或者 image 加上commandbutton 组合的自画控件
wtk13 2003-01-29
  • 打赏
  • 举报
回复
鱼头兄,如何做一个象WINDOWS开始按钮那样的,图片在左边,文字在右边?
Sean918 2003-01-29
  • 打赏
  • 举报
回复
http://www.easthot.net/topic.asp?TOPIC_ID=215&FORUM_ID=2&CAT_ID=2&Topic_Title=%B8%C4%B1%E4%B0%B4%C5%A5%B5%C4%CE%C4%B1%BE%D1%D5%C9%AB&Forum_Title=%A1%BEVB%2CVB%2Enet+%BF%AA%B7%A2%C2%DB%CC%B3%A1%BF

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧