CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

VB中按钮的背景色和文字的字体颜色怎么弄?

楼主Edison621(重新来过)2005-05-16 08:36:08 在 VB / 基础类 提问

好像不能设置?我想设置一下按钮的背景色和字体颜色等等。  
   
  要是用到一些别人做好的,那是不是直接生成的exe程序copy到别的机器就不能用了?我是菜鸟,请帮忙,谢谢! 问题点数:50、回复次数:9Top

1 楼wzzwwz(皮皮鲁)回复于 2005-05-16 08:39:57 得分 0

字体颜色不能改Top

2 楼wzzwwz(皮皮鲁)回复于 2005-05-16 08:40:57 得分 20

背景颜色:  
  把   Style   属性改成   1  
  然后更改   BackColor   属性Top

3 楼mndsoft(枕善居(http://www.mndsoft.com/)回复于 2005-05-16 08:42:13 得分 10

这里有个“改变CommandButton字体颜色   "  
   
  http://www.mndsoft.com  
   
  个代码演示的是改变CommandButton字体颜色,但程序中的控件不是一个真正的CommandButton,但示例却能达到这样的效果Top

4 楼tanaya(唐博士http://blog.csdn.net/tanaya)回复于 2005-05-16 09:04:08 得分 20

给你一个设置   CommandButton   字体颜色的模块:mSetCommandButtonFontColor  
   
  Option   Explicit  
   
  Private   Declare   Sub   CopyMemory   Lib   "kernel32"   Alias   "RtlMoveMemory"   (Destination   As   Any,   Source   As   Any,   ByVal   Length   As   Long)  
  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   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   Function   GetWindowText   Lib   "user32"   Alias   "GetWindowTextA"   (ByVal   hWnd   As   Long,   ByVal   lpString   As   String,   ByVal   cch   As   Long)   As   Long  
  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       As   Long   =   1  
  Private   Const   GWL_WNDPROC       As   Long   =   -4  
  Private   Const   ODT_BUTTON         As   Long   =   4  
  Private   Const   ODS_SELECTED     As   Long   =   &H1  
  Private   Const   WM_DESTROY         As   Long   =   &H2  
  Private   Const   WM_DRAWITEM       As   Long   =   &H2B  
  Private   Const   DT_HCENTER         As   Long   =   &H1  
  Private   Const   DT_TOP                 As   Long   =   &H0  
  Private   Const   DT_VCENTER         As   Long   =   &H4  
  Private   Const   DT_BOTTOM           As   Long   =   &H8  
  Private   Const   DT_SINGLELINE   As   Long   =   &H20  
   
  Private   Const   DT_WORDBREAK   As   Long   =   &H10  
  Public   Const   DT_CHARSTREAM   =   4                     '     Character-stream,   PLP  
  Public   Const   DT_EXPANDTABS   =   &H40  
  Public   Const   DT_EXTERNALLEADING   =   &H200  
  Public   Const   DT_LEFT   =   &H0  
  Public   Const   DT_NOCLIP   =   &H100  
  Public   Const   DT_CENTER   As   Long   =   &H1  
  Public   Const   DT_CALCRECT   =   &H400  
  Public   Const   DT_INTERNAL   =   &H1000  
   
  Public   Const   TA_CENTER   =   6  
  Public   Const   TA_UPDATECP   =   1  
  Public   Const   TA_BASELINE   =   24  
  Public   Const   DT_METAFILE   =   5                         '     Metafile,   VDM  
  Public   Const   DT_PLOTTER   =   0                           '     Vector   plotter  
  Public   Const   DUPLICATE   =   &H6  
   
  Public   Const   WM_GETTEXT   =   &HD  
  Public   Const   WM_GETMINMAXINFO   =   &H24  
  Public   Const   WM_GETFONT   =   &H31  
  Public   Const   WM_COPY   =   &H301  
  Public   Const   WM_GETTEXTLENGTH   =   &HE  
  Public   Const   WM_COPYDATA   =   &H4A  
  Public   Const   WM_PASTE   =   &H302  
   
  Private   Type   RECT  
          Left                 As   Long  
          Top                   As   Long  
          Right               As   Long  
          Bottom             As   Long  
  End   Type  
   
  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  
   
  Public   Enum   AlignText  
          AlignTop   =   DT_TOP  
          AlignCenter   =   DT_VCENTER  
          AlignBottom   =   DT_BOTTOM  
          ThreeD   =   DT_VCENTER   Or   DT_BOTTOM  
  End   Enum  
   
  Private   Const   PropCustom   =   "UMGCustom"  
  Private   Const   PropForeColor   =   "UMGForeColor"  
  Private   Const   PropAlign   =   "UMGVAlign"  
  Private   Const   PropSubclass   =   "UMGDrawProc"  
   
  Public   Sub   SetForeColor(Button   As   CommandButton,   ByVal   ForeColor   As   OLE_COLOR,   Optional   ByVal   Alignment   As   AlignText   =   AlignCenter)  
          Dim   hWndPnt       As   Long  
          With   Button  
                  hWndPnt   =   GetParent(.hWnd)  
                  If   GetProp(hWndPnt,   PropSubclass)   =   0   Then  
                          SetProp   hWndPnt,   PropSubclass,   GetWindowLong(hWndPnt,   GWL_WNDPROC)  
                          SetWindowLong   hWndPnt,   GWL_WNDPROC,   AddressOf   DrawButtonProc  
                  End   If  
                  SetProp   .hWnd,   PropCustom,   True  
                  SetProp   .hWnd,   PropForeColor,   ForeColor  
                  SetProp   .hWnd,   PropAlign,   Alignment  
                  .Refresh  
          End   With  
  End   Sub  
   
  Public   Sub   UnsetForeColor(Button   As   CommandButton)  
          With   Button  
                  RemoveProp   .hWnd,   PropCustom  
                  RemoveProp   .hWnd,   PropForeColor  
                  RemoveProp   .hWnd,   PropAlign  
                  .Refresh  
          End   With  
  End   Sub  
   
  Private   Function   DrawButtonProc(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  
          Dim   s                   As   String  
          Dim   VA                 As   AlignText  
          lOldProc   =   GetProp(hWnd,   PropSubclass)  
          DrawButtonProc   =   CallWindowProc(lOldProc,   hWnd,   wMsg,   wParam,   lParam)  
          Select   Case   wMsg  
                          Case   WM_DRAWITEM  
                                      CopyMemory   di,   ByVal   lParam,   Len(di)  
                                      With   di  
                                              If   .CtlType   =   ODT_BUTTON   Then  
                                                      If   GetProp(.hWndItem,   PropCustom)   Then  
                                                              VA   =   GetProp(.hWndItem,   PropAlign)  
                                                              With   .rcItem  
                                                                      Select   Case   VA  
                                                                          Case   DT_TOP  
                                                                              .Top   =   .Top   +   4  
                                                                          Case   DT_BOTTOM  
                                                                              .Bottom   =   .Bottom   -   4  
                                                                          Case   ThreeD  
                                                                              .Left   =   .Left   -   1  
                                                                              .Top   =   .Top   -   1  
                                                                              .Right   =   .Right   -   1  
                                                                              .Bottom   =   .Bottom   -   1  
                                                                              VA   =   AlignCenter  
                                                                      End   Select  
                                                                      If   (di.ItemState   And   ODS_SELECTED)   =   ODS_SELECTED   Then  
                                                                              .Left   =   .Left   +   1  
                                                                              .Top   =   .Top   +   1  
                                                                              .Right   =   .Right   +   1  
                                                                              .Bottom   =   .Bottom   +   1  
                                                                      End   If  
                                                              End   With  
                                                              SetBkMode   .hDC,   TRANSPARENT  
                                                              s   =   String$(255,   0)  
                                                              GetWindowText   .hWndItem,   s,   Len(s)  
                                                              s   =   Left$(s,   InStr(s,   Chr$(0))   -   1)  
                                                              SetTextColor   .hDC,   GetProp(.hWndItem,   PropForeColor)  
                                                              If   di.CtlID   <>   2   Then  
                                                                      DrawText   .hDC,   s,   Len(s),   .rcItem,   DT_SINGLELINE   Or   DT_HCENTER   Or   VA  
                                                              Else  
                                                                      With   .rcItem  
                                                                              .Top   =   .Top   +   46  
                                                                      End   With  
                                                                      DrawText   .hDC,   s,   Len(s),   .rcItem,   DT_WORDBREAK   Or   TA_CENTER   Or   DT_HCENTER  
                                                              End   If  
                                                      End   If  
                                              End   If  
                                      End   With  
                          Case   WM_DESTROY  
                                      If   lOldProc   Then  
                                              SetWindowLong   hWnd,   GWL_WNDPROC,   lOldProc  
                                              RemoveProp   hWnd,   PropSubclass  
                                      End   If  
          End   Select  
  End   Function  
   
   
  调用   --->>>   在窗体上加入一个按钮,写入代码:  
   
  'Command1.Style   =   1 这个运行前设置  
   
  Private   Sub   Form_Load()  
          Command1.BackColor   =   &HFFC0C0  
          SetCommandButtonFontColor   Command1,   vbBlue  
  End   Sub  
   
  Private   Sub   Form_Unload(Cancel   As   Integer)  
          UnSetCommandButtonFontColor   Command1  
  End   Sub  
   
   
   
   
  Top

5 楼tanaya(唐博士http://blog.csdn.net/tanaya)回复于 2005-05-16 09:06:06 得分 0

 
  我把调用函数名称改了,上面贴错误了:这确模块:  
   
  Option   Explicit  
   
  Private   Declare   Sub   CopyMemory   Lib   "kernel32"   Alias   "RtlMoveMemory"   (Destination   As   Any,   Source   As   Any,   ByVal   Length   As   Long)  
  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   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   Function   GetWindowText   Lib   "user32"   Alias   "GetWindowTextA"   (ByVal   hWnd   As   Long,   ByVal   lpString   As   String,   ByVal   cch   As   Long)   As   Long  
  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       As   Long   =   1  
  Private   Const   GWL_WNDPROC       As   Long   =   -4  
  Private   Const   ODT_BUTTON         As   Long   =   4  
  Private   Const   ODS_SELECTED     As   Long   =   &H1  
  Private   Const   WM_DESTROY         As   Long   =   &H2  
  Private   Const   WM_DRAWITEM       As   Long   =   &H2B  
  Private   Const   DT_HCENTER         As   Long   =   &H1  
  Private   Const   DT_TOP                 As   Long   =   &H0  
  Private   Const   DT_VCENTER         As   Long   =   &H4  
  Private   Const   DT_BOTTOM           As   Long   =   &H8  
  Private   Const   DT_SINGLELINE   As   Long   =   &H20  
   
  Private   Const   DT_WORDBREAK   As   Long   =   &H10  
  Public   Const   DT_CHARSTREAM   =   4                     '     Character-stream,   PLP  
  Public   Const   DT_EXPANDTABS   =   &H40  
  Public   Const   DT_EXTERNALLEADING   =   &H200  
  Public   Const   DT_LEFT   =   &H0  
  Public   Const   DT_NOCLIP   =   &H100  
  Public   Const   DT_CENTER   As   Long   =   &H1  
  Public   Const   DT_CALCRECT   =   &H400  
  Public   Const   DT_INTERNAL   =   &H1000  
   
  Public   Const   TA_CENTER   =   6  
  Public   Const   TA_UPDATECP   =   1  
  Public   Const   TA_BASELINE   =   24  
  Public   Const   DT_METAFILE   =   5                         '     Metafile,   VDM  
  Public   Const   DT_PLOTTER   =   0                           '     Vector   plotter  
  Public   Const   DUPLICATE   =   &H6  
   
  Public   Const   WM_GETTEXT   =   &HD  
  Public   Const   WM_GETMINMAXINFO   =   &H24  
  Public   Const   WM_GETFONT   =   &H31  
  Public   Const   WM_COPY   =   &H301  
  Public   Const   WM_GETTEXTLENGTH   =   &HE  
  Public   Const   WM_COPYDATA   =   &H4A  
  Public   Const   WM_PASTE   =   &H302  
   
  Private   Type   RECT  
          Left                 As   Long  
          Top                   As   Long  
          Right               As   Long  
          Bottom             As   Long  
  End   Type  
   
  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  
   
  Public   Enum   AlignText  
          AlignTop   =   DT_TOP  
          AlignCenter   =   DT_VCENTER  
          AlignBottom   =   DT_BOTTOM  
          ThreeD   =   DT_VCENTER   Or   DT_BOTTOM  
  End   Enum  
   
  Private   Const   PropCustom   =   "UMGCustom"  
  Private   Const   PropForeColor   =   "UMGForeColor"  
  Private   Const   PropAlign   =   "UMGVAlign"  
  Private   Const   PropSubclass   =   "UMGDrawProc"  
   
  Public   Sub   SetCommandButtonFontColor(Button   As   CommandButton,   ByVal   ForeColor   As   OLE_COLOR,   Optional   ByVal   Alignment   As   AlignText   =   AlignCenter)  
          Dim   hWndPnt       As   Long  
          With   Button  
                  hWndPnt   =   GetParent(.hWnd)  
                  If   GetProp(hWndPnt,   PropSubclass)   =   0   Then  
                          SetProp   hWndPnt,   PropSubclass,   GetWindowLong(hWndPnt,   GWL_WNDPROC)  
                          SetWindowLong   hWndPnt,   GWL_WNDPROC,   AddressOf   DrawButtonProc  
                  End   If  
                  SetProp   .hWnd,   PropCustom,   True  
                  SetProp   .hWnd,   PropForeColor,   ForeColor  
                  SetProp   .hWnd,   PropAlign,   Alignment  
                  .Refresh  
          End   With  
  End   Sub  
   
  Public   Sub   UnSetCommandButtonFontColor(Button   As   CommandButton)  
          With   Button  
                  RemoveProp   .hWnd,   PropCustom  
                  RemoveProp   .hWnd,   PropForeColor  
                  RemoveProp   .hWnd,   PropAlign  
                  .Refresh  
          End   With  
  End   Sub  
   
  Private   Function   DrawButtonProc(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  
          Dim   s                   As   String  
          Dim   VA                 As   AlignText  
          lOldProc   =   GetProp(hWnd,   PropSubclass)  
          DrawButtonProc   =   CallWindowProc(lOldProc,   hWnd,   wMsg,   wParam,   lParam)  
          Select   Case   wMsg  
                          Case   WM_DRAWITEM  
                                      CopyMemory   di,   ByVal   lParam,   Len(di)  
                                      With   di  
                                              If   .CtlType   =   ODT_BUTTON   Then  
                                                      If   GetProp(.hWndItem,   PropCustom)   Then  
                                                              VA   =   GetProp(.hWndItem,   PropAlign)  
                                                              With   .rcItem  
                                                                      Select   Case   VA  
                                                                          Case   DT_TOP  
                                                                              .Top   =   .Top   +   4  
                                                                          Case   DT_BOTTOM  
                                                                              .Bottom   =   .Bottom   -   4  
                                                                          Case   ThreeD  
                                                                              .Left   =   .Left   -   1  
                                                                              .Top   =   .Top   -   1  
                                                                              .Right   =   .Right   -   1  
                                                                              .Bottom   =   .Bottom   -   1  
                                                                              VA   =   AlignCenter  
                                                                      End   Select  
                                                                      If   (di.ItemState   And   ODS_SELECTED)   =   ODS_SELECTED   Then  
                                                                              .Left   =   .Left   +   1  
                                                                              .Top   =   .Top   +   1  
                                                                              .Right   =   .Right   +   1  
                                                                              .Bottom   =   .Bottom   +   1  
                                                                      End   If  
                                                              End   With  
                                                              SetBkMode   .hDC,   TRANSPARENT  
                                                              s   =   String$(255,   0)  
                                                              GetWindowText   .hWndItem,   s,   Len(s)  
                                                              s   =   Left$(s,   InStr(s,   Chr$(0))   -   1)  
                                                              SetTextColor   .hDC,   GetProp(.hWndItem,   PropForeColor)  
                                                              If   di.CtlID   <>   2   Then  
                                                                      DrawText   .hDC,   s,   Len(s),   .rcItem,   DT_SINGLELINE   Or   DT_HCENTER   Or   VA  
                                                              Else  
                                                                      With   .rcItem  
                                                                              .Top   =   .Top   +   46  
                                                                      End   With  
                                                                      DrawText   .hDC,   s,   Len(s),   .rcItem,   DT_WORDBREAK   Or   TA_CENTER   Or   DT_HCENTER  
                                                              End   If  
                                                      End   If  
                                              End   If  
                                      End   With  
                          Case   WM_DESTROY  
                                      If   lOldProc   Then  
                                              SetWindowLong   hWnd,   GWL_WNDPROC,   lOldProc  
                                              RemoveProp   hWnd,   PropSubclass  
                                      End   If  
          End   Select  
  End   Function  
   
  Top

6 楼wzzwwz(皮皮鲁)回复于 2005-05-16 09:06:15 得分 0

晕~Top

7 楼ling242a()回复于 2005-05-16 09:13:26 得分 0

我可以一个设置字体颜色的方法给你,但我没代码,就是这样,用image控件先设置好以后然后把image赋给command就行了。你可以上网搜搜。Top

8 楼Edison621(重新来过)回复于 2005-05-16 14:21:41 得分 0

请问怎样设置窗体运行时最大化显示?Top

9 楼wzzwwz(皮皮鲁)回复于 2005-05-16 16:38:15 得分 0

Option   Explicit  
   
  Private   Sub   Form_Load()  
          Me.WindowState   =   vbMaximized  
  End   Sub  
  Top

相关问题

  • 按钮上的中文字体太大,显示不全,怎么让字体变小啊?
  • 谁知道vb中的按钮的字体的颜色能改吗?
  • 请问如何改变按钮上文字的属性?(比如字体和大小等)
  • 急!急!急!如何改变表单的最上面的操作栏(Action bar)上按钮文字的字体?
  • 在VB中如何修改command按钮上的字体颜色(不要用Label代替)
  • VB的命令按钮上怎样同时显示图标和文字?
  • 网页文字字体
  • visio 中文字体问题
  • visio 中文字体问题
  • 改变按钮字体的方法.

关键词

  • 字体
  • longpriv
  • 字体颜色
  • commandbutton
  • byval
  • 背景
  • alias
  • 设置
  • hwnd
  • long

得分解答快速导航

  • 帖主:Edison621
  • wzzwwz
  • mndsoft
  • tanaya

相关链接

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

广告也精彩

反馈

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