当前位置:编程学习 > VB >>

改变按钮的文本颜色

 

 

Visual Basic允许程序员改变一个CommandButton的背景色--简单的将样式设为Graphical然后改变BackColor就行了。但却没有提供一个简单的方法去改变CommandButton的文字的颜色。本文将告诉你怎样根据你的意愿改变CommandButton的文本颜色,而且如果按钮上有图片的话,还可以将文本显示在按钮的底部。

在工程中添加以下模块(Module):

Module modExtButton.bas

 

Option Explicit

 

==================================================================

modExtButton.bas

本模块可让你改变命令按钮的文本颜色。

使用方法:

- 在设计时将文本的Style设为Graphical.

- 随意设定背景色和图象属性.

- 在Form_Load中调用 SetButton :

SetButton Command1.hWnd, vbBlue

(你可以任意次的调用该过程甚至不必先调用 RemoveButton.)

- 在Form_Unload中调用 RemoveButton :

RemoveButton Command1.hWnd

==================================================================

 

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 were 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 Se

补充:软件开发 , Vb ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,