VB里如何改变命令按钮中文字的颜色?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB里如何改变命令按钮中文字的颜色?相关的知识,希望对你有一定的参考价值。

如题

不能直接通过属性框修改按钮中文本的颜色
根本没有foreColor属性
下边是一个参考方法
首先请把要改的按钮的Style设置为1

在工程中添加以下模块(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 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

然后回到FORM中:
添加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 Sub

For m = 0 To 9
SetButton CmdNum(m).hWnd, vbBlue
Next
For n = 1 To 4
SetButton CmdCal(n).hWnd, vbRed
Next
For l = 2 To 4
SetButton CmdOth(l).hWnd, vbRed
Next

参考资料:www.csdn.net

参考技术A 如果你确实不懂他们说的,教你一招最笨的方法在按钮上面画一个label控件,
设置背景透明
,然后将按钮的文字去掉,在这个label的caption属性里面输入
它的文字
,并设置字体颜色等,然后写单击事件的时候
就写这个label的单击事件
,
参考技术B 点击按钮,右边属性,按分区,最后一项FOUT,设置. 参考技术C 按钮 ... 属性 ... 找 ForeColor ...

修改就可以 ...
参考技术D 只能改变“字体” “大小” “效果” 上面的只能是黑色的,可能要调用API吧
在强的自己些个按钮的类。

如何改变UIDocumentInteractionController完成按钮的文字和背景颜色

如何更改完成按钮的背景颜色和文字颜色?有没有办法,我可以改变颜色导航栏和导航栏标题颜色和底部栏的颜色也是一个办法?参考附截图:enter image description here

答案

我解决了它。这里是为我工作完美的代码:

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    UINavigationBar.appearance().barTintColor = Colors.redColor()
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)]
    return self
}
另一答案

这是一个有点哈克为依托的事实,QLPreviewController是实施UIDocumentInteractionController类,但这样的事情是侵入性最小的解决方案。做到这一点,你显示UIDocumentInteractionController前

import QuickLook

UIBarButtonItem.appearance(whenContainedInInstancesOf [QLPreviewController.self]).tintColor = UIColor.black
另一答案

我有一个IDEAR改变栏的颜色:

let allNavigationBar = UINavigationBar.appearance()
allNavigationBar.barTintColor = UIColor.red  // change the bar background color
allNavigationBar.tintColor = UIColor.black // change the Done button's tintColor

let alloolbar = UIToolbar.appearance()
allToolbar.barTintColor = UIColor.red  // dones't work, try backgroundImage
allToolbar.backgroundColor = UIColor.blue // dones't work
allToolbar.tintColor = UIColor.brown // change the toolbar's item tint color

但这种方法有很大的作用,所有的UINavigationBarand UIToolBar会做出的改变。

希望其他人能够给出一个更好的解决方案。

另一答案

你可以暂时改变窗口的色调颜色。

func presentDocument() {
    //present the controller here
    self.appDelegate.window.tintColor = UIColor.red
}

然后换回来后:

func documentInteractionControllerDidEndPreview(documentInteractionController) { //replace parameter with your uidocumentviewinteractioncontroller
    self.appDelegate.window.tintColor = UIColor.white
}
另一答案

@Dee。我想你问这个部分在您的其他问题之一。在这种情况下,你无法证明预览控制器。在这个问题提出的答案是从委托方法返回的“自我”。如果您实现正确那么你预览将使用相同的导航栏的颜色作为其父控制器使用。我的意思是,如果你有一些视图控制器直接打开UIDocumentInteractionController然后UIDocumentInteractionController将利用其父的viewController的导航栏的颜色。这可以帮助你完成更改按钮颜色

另一答案

试试这个:(您需要实现UIDocumentInteractionControllerDelegate)

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self.navigationController ?? self
}
另一答案
     let QLNavAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self])
    QLNavAppearance.tintColor = UIColor.red // some
    QLNavAppearance.barTintColor =  UIColor.red // some
    QLNavAppearance.backgroundColor =  UIColor.red // some

以上是关于VB里如何改变命令按钮中文字的颜色?的主要内容,如果未能解决你的问题,请参考以下文章

vb中单击命令按钮触发哪些事件

请问Winform里面怎么单击按钮来改变窗体背景图片?(C#)

Flutter中如何改变按钮主题的文字颜色

vb中如何改变对话框的字体

如何改变UIDocumentInteractionController完成按钮的文字和背景颜色

c++如何改变控制台文字颜色