VS2008如何用代码改变按钮背景颜色,字体大小和颜色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VS2008如何用代码改变按钮背景颜色,字体大小和颜色相关的知识,希望对你有一定的参考价值。
button1.ForeColor = Color.Red;
这样写为什么不行?
改变前景色:button1.ForeColor = Color.Red;
改变字体: button1.Font = new Font("System", 8);
改变背景色:button1.BackColor = Color.Blue; 参考技术A 可以啊,不过得把语句后面的“;”去掉。
vb中如何改变对话框的字体
鄙人在做VB程序时希望改变Msgbox中显示的字体,问如何实现?
'方法是有,不过不建议这么做.你确定你还要这么做?确定?好吧.那就接着往下看.'只有想不到,没有做不到,看我无敌API......呵呵
'这段代码大部分是别人写的,我只是修改并封装成函数.
'功能可扩展:颜色,背景图案,按钮颜色,按钮图案...(总之,很多很多...)
'函数调用方法:MyMsgBox 显示的信息,按钮,标题,字体名称(默认为宋体),字体大小,斜体,下划线,删除线
'窗体代码
Private Sub Command1_Click()
MyMsgBox "我变,我变,我变变变!!!!", vbYesNo, "- -|||", "黑体"
End Sub
'模块代码
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) 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 EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
Private Const HCBT_ACTIVATE = 5
Private Const WH_CBT = 5
Private Const DEFAULT_CHARSET = 1
Private Const OUT_TT_ONLY_PRECIS = 7
Private Const CLIP_DEFAULT_PRECIS = 0
Private Const DEFAULT_QUALITY = 0
Private Const FF_DONTCARE = 0
Private Const WM_SETFONT = &H30
Private hHook As Long
Private MsgBoxTitle As String
Private hFont As Long
Private Sub EnableHook()
If hHook = 0 Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, 0, App.ThreadID)
End If
End Sub
Private Sub FreeHook()
If hHook <> 0 Then
Call UnhookWindowsHookEx(hHook)
hHook = 0
End If
End Sub
Private Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If nCode < 0 Then
HookProc = CallNextHookEx(hHook, nCode, wParam, lParam)
Exit Function
End If
'当MsgBox Activate之前去更改Button的标题
If nCode = HCBT_ACTIVATE Then
Dim str5 As String
Dim len5 As Long, I As Long
str5 = String(255, 0)
len5 = 256
I = GetWindowText(wParam, str5, len5)
str5 = Left(str5, InStr(1, str5, Chr(0)) - 1)
'如果Active Window上的标题是MsgBox上的标题
'
If str5 = MsgBoxTitle Then
'取得MsgBox上的所有子window
Call EnumChildWindows(wParam, AddressOf ChgButtonTitle, 0)
End If
End If
HookProc = 0 '令待完成的动作继续完成,若为1,则取消原本要完成的动作
End Function
Private Function ChgButtonTitle(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim clsName As String
If hwnd = 0 Then
ChgButtonTitle = 0
Exit Function
End If
clsName = String(255, 0)
Call GetClassName(hwnd, clsName, 256)
clsName = Left(clsName, InStr(1, clsName, Chr(0)) - 1)
'找到Static型态的子Window,并更改其上的标题
If clsName = "Static" Then
SendMessage hwnd, WM_SETFONT, hFont, True
End If
ChgButtonTitle = 1 '表示继续找下一个子Window
End Function
Public Function MyMsgBox(ByVal Prompt As String, Optional ByVal Buttons As Long, Optional ByVal Title As String = " ", Optional ByVal sFontName As String = "宋体", Optional ByVal FontSize As Long, Optional ByVal FontItalic As Boolean = False, Optional ByVal FontUnderline As Boolean = False, Optional ByVal FontStrikethru As Boolean = False) As Long
hFont = CreateFont(FontSize, 0, 0, 0, 0, FontItalic, FontUnderline, FontStrikethru, DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, sFontName)
Call EnableHook
MsgBoxTitle = Title
MyMsgBox = MsgBox(Prompt, Buttons, MsgBoxTitle)
Call FreeHook
End Function 参考技术A 使用时与一般的WinAPI相差不多, 以下有一个包装过的函式.
MsgBox(提示文字, 标题, ID_Flat)
第叁个引数的设定与本函数传回值, 请参阅 WinAPI 中对MessageBox 的说明
本函数传入值为 Object Pascal 式的字串, 如果需要传入 PChar 请直接呼叫 Application.MessageBox
以 PChar(LongStr) 即可传入 Application.MessageBox不一定需要 call 本函数, 只是为了前后版本相容而保留本函数
function MsgBox(const sText, sCaption: string; wFlag: word): integer;
$ifdef Windows
$define __ShortString
$endif
$ifdef Win32
$ifopt H- 参考技术B 想法不错,不过msgbox不是控件,无法改变字体属性,个人看法!!
或许改变系统默认字体可以才行!!
如果非要改变字体,还有按钮的名称等,还是自己设计一个窗体模拟 msgbox 参考技术C 是的,inputbox和msgbox的字体、颜色、大小等都不好改。
如果要美化他们,建议自己制作小窗体来代替他们,也不是很难,并且各种设置随心所欲。。 参考技术D 自己设计个窗体吧
以上是关于VS2008如何用代码改变按钮背景颜色,字体大小和颜色的主要内容,如果未能解决你的问题,请参考以下文章