如何修改VB按钮前景色?

a达哥a 2005-09-15 10:47:24
如题
...全文
1146 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
vansoft 2005-09-16
  • 打赏
  • 举报
回复
按鈕字體的顔色要用子類處理的方法。
northwolves 2005-09-16
  • 打赏
  • 举报
回复
我机子上几年前的代码

'Change the ForeColor of the text in a command button.
'bas


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)

'form1

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

province_ 2005-09-16
  • 打赏
  • 举报
回复
替代法是最简单的,否则就要用用户自画的按纽,比较烦,当然效果还是非常好的。
jxgzay 2005-09-16
  • 打赏
  • 举报
回复

Command1.BackColor = vbRed
Command1.Style = 1 (设计期设置)
viena 2005-09-16
  • 打赏
  • 举报
回复
同意用IMAGE,分别得到按钮按下、选中等的图片就可以了
可以用别人的系统中的按钮的截图(用拷屏)
脆皮大雪糕 2005-09-16
  • 打赏
  • 举报
回复
按钮而已,响应click事件而已,没必要倒腾那么复杂吧,拿个别的东西替代一下,按下去的时候一个边框,mouse up的时候换一个外观不就可以了。自己重新写一个控件也没那么复杂
a达哥a 2005-09-16
  • 打赏
  • 举报
回复
谁有相应的API?
a达哥a 2005-09-16
  • 打赏
  • 举报
回复
to : vansoft(Vansoft Workroom)

详细点好吗?
熊孩子开学喽 2005-09-15
  • 打赏
  • 举报
回复
把按钮设置为图形模式只能改变按钮的颜色,而不是字体颜色。
可以用其他控件来实现这个效果比如:
只是文字的按钮可以用LABEL控件来代替(这个比标准按钮的资源占用还小);
图片按钮使用IMAGE控件来代替(PICTURE的资源占用太大,不推荐);
hujiahuwei 2005-09-15
  • 打赏
  • 举报
回复
按钮的前景颜色用一般的方法是改不了的,得调用API函数
MOTA 2005-09-15
  • 打赏
  • 举报
回复

指的是字体颜色吗??

一般来说 把按钮设置为图形模式就可以调试了
此源码包括CSharp、VB.net报表代码示例。 Gscr.Report控件(广源报表控件)实现报表自由定义,报表显示、打印预览、打印、公式、排序、导出等报表常见功能,是一款较好报表控件。 ---以下是引用官方部分简介--- Gscr.Report控件(广源报表控件)是专为.NET For Windows Forms编程者开发的报表平台,具有功能实用、操作简捷的特点。 1.采用行列式二维表格,能够灵活设定单元格属性,操作方式和Excel很相似; a.单元格宽度和高度、背景色、前景色、显示格式、格线设置等; b.字体类型、字体大小、字体粗细、斜体及下划线设置等; c.数据水平对齐、垂直对齐设置等; d.合并单元格功能; 2.友好的表格设计器界面,可以让报表设计工作变得轻松。您可以使用它来制作表格模板,然后将表格保存为.gsc文件。 3.支持数据绑定。用户只需提供一个DataTable对象,因此报表支持目前流行的各种数据库格式,比如SQL Server、Oracle、Access等常用数据库格式。 另外,用户也通过赋值报表的SelectSQL属性(设置针对SQL作为 Gscr.Report 数据源运行的SQL文本命令)来实现数据绑定。 4.可以按照打印效果的需要进行灵活的设置,包括页边距、纸张大小,页眉页脚、打印方向、表格线是否打印、背景是否打印等。 同时,支持表格套打,用户可以对每个单元格的背景颜色、内容、边线等是否打印进行灵活控制。 5.提供丰富的单元格数据类型(或编辑方式):编辑框、下拉列表框、下拉表格、复选框、日期选择框、货币输入框、按钮、进度条、密码等。 6.行列的添加/删除,显示/隐藏、拖动、排序、锁定行列等功能 7.对创建的报表支持包括xls、txt、csv、xml等多种导出文件格式, 同时支持资源本地化。 8 支持单元格公式运算。公式表达式中可以进行算术、关系、逻辑等各种运算,支持对参数、变量、字段及单元格的引用。 同时,提供丰富的内建函数,包括文本函数、日期和时间函数、数学函数、检查函数、程序流函数、类型转换函数、聚合函数及各种特殊处理函数(如金额大写)等。 9.界面美观、大方。

7,762

社区成员

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

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