CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

如何设定按钮上文字的颜色?

楼主ishallwin()2005-08-02 15:49:01 在 VB / 基础类 提问

如何设定按钮上文字的颜色? 问题点数:20、回复次数:6Top

1 楼zq972(最近不想写代码,好累~~~~)回复于 2005-08-02 16:14:20 得分 5

vb本身的command   button没有这个功能  
  1.去网上搜个button替代本身的command   button  
  2.用command   button+label实现Top

2 楼moonz(追梦人:向星星迈进)回复于 2005-08-02 19:32:54 得分 0

ForeColor属性呢?Top

3 楼province_(雍昊)回复于 2005-08-02 19:49:15 得分 0

使用USER   OWNER   BUTTON就可以做你想做的任何效果了。Top

4 楼libralibra(食食物者为俊杰: U don't try, U don't know)回复于 2005-08-02 20:09:46 得分 10

添加模块modExtButton.bas  
   
  '==================================================================  
   
  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   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  
  '--------------------------------------------  
   
  '   使用方法:  
  '  
  '   -   在设计时将文本的Style设为Graphical.  
  '  
  '   -   随意设定背景色和图象属性.  
  '  
  '   -   在Form_Load中调用   SetButton   :  
  '   SetButton   Command1.hWnd,   vbBlue  
  '   (你可以任意次的调用该过程甚至不必先调用   RemoveButton.)  
  '  
  '   -   在Form_Unload中调用   RemoveButton   :  
  '   RemoveButton   Command1.hWnd  
  '===============================================  
   
  将Form命名为frmDemo。添加4个CommandButton,不必更改它们的名称,将它们的Style设为Graphical,给第3个按钮设置一幅图片。  
  CommandButton也可以放置在一个容器如PictureBox或Frame中,模块会判断,如果需要的话将CommandButton的容器也子类化。  
  在Form中加入如下代码:  
  Private   Sub   Form_Load()  
   
  'Initialize   each   button   color.  
  SetButton   Command1.hWnd,   vbRed  
  SetButton   Command2.hWnd,   &H8000&   '深绿色  
  'Assign   this   one   a   DT_BOTTOM   alignment   because  
  SetButton   Command3.hWnd,   vbBlue,   DT_BOTTOM   '含有图片,将文本放置在按钮底部  
  SetButton   Command4.hWnd,   &H8080&   '暗棕黄色  
   
  End   Sub  
   
  Private   Sub   Form_Unload(Cancel   As   Integer)  
   
  '手动解除按钮的子类化  
  '这并不是必须的  
  RemoveButton   Command1.hWnd  
  RemoveButton   Command2.hWnd  
  RemoveButton   Command3.hWnd  
  RemoveButton   Command4.hWnd  
   
  End   SubTop

5 楼kmlxk(xiaoKKKK)回复于 2005-08-02 20:28:46 得分 0

@_@啊哦,代码说明一切,强Top

6 楼WallesCai(女人之美,在于蠢得无怨无悔,男人之美,在于撒谎撒得白日见鬼)回复于 2005-08-03 09:01:52 得分 5

按钮的功能也只是让你可以“按”一下,再触发一个事件而已,我一般都是使用LABEL控件来代替它的,不仅前后景颜色可以调节,资源占用也小。  
  楼主试试吧。Top

相关问题

  • 请问如何设定TabStrip的文字颜色?谢谢
  • 如何设定某 <span> 内的文字颜色﹖
  • 如何设定文本框在enabled=false时的文字颜色?
  • 请问如何实现当鼠标移动到按钮上,按钮上的文字自动变颜色
  • 这里只有文字颜色按钮,没有突出显示按钮,怎么办?[有图]
  • 如何改变按钮上文字的颜色(请用sdk解答,谢谢)
  • 请问如何改变button按钮上文字的颜色!急急急!谢谢
  • VB中按钮的背景色和文字的字体颜色怎么弄?
  • 在内存dc上textout文字,怎样才能设定文字颜色?为什么我用Memdc.SetTextColor毫无效果?
  • 如何改变标题栏的文字及标题栏上的最大化按钮的形状和颜色

关键词

  • hwndform
  • 按钮
  • longprivate
  • hwndremovebutton
  • byval
  • hwnd
  • gwl
  • 颜色
  • commandbutton
  • alias

得分解答快速导航

  • 帖主:ishallwin
  • zq972
  • libralibra
  • WallesCai

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo