VBA 更改 MsgBox 中的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39408720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:34:01  来源:igfitidea点击:

VBA Change the text color in MsgBox

excelvba

提问by BOB

I want to change the font color from MsgBox

我想从 MsgBox 更改字体颜色

To understand what I want, I chose this exemple:

为了理解我想要什么,我选择了这个例子:

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim results As String


a = InputBox("Enter your first value:")
b = InputBox("Enter your second value:")
c = InputBox("Enter your third value:")

d = a - b + c

If d = 0 Then
    results = "Correct"
    MsgBox "Your results is: " & results 
Else
    results = "Incorrect"
    MsgBox " Your Results is: " & results 
End If

The "Correct" text I want to be in green when it appears in MsgBox; the "Incorrect" text I want to be in red when it appears in MsgBox

“正确”文本出现在MsgBox; “不正确”的文本出现时我想变成红色MsgBox

I hope what I have requested is possible.

我希望我所要求的是可能的。

回答by Ambie

As Ralph suggests, it'd be better to display your message in a UserFormwhere you'd have easy control over the text characteristics.

正如 Ralph 所建议的那样,最好将您的消息显示UserForm在您可以轻松控制文本特征的地方。

However, it is possible to change the colour of your MessageBox text, using the system color API's. As the MessageBox is a Window, you can alter the colour parameters of it (not just text, but various others too).

但是,可以使用系统颜色 API 更改 MessageBox 文本的颜色。由于 MessageBox 是一个窗口,您可以更改它的颜色参数(不仅是文本,还有其他各种参数)。

You'd want to ensure that you reset the original values immediately afterwards of course otherwise all of your windows will display in the modified colours.

当然,您要确保之后立即重置原始值,否则所有窗口都将以修改后的颜色显示。

The below code will automatically detect 32-bit and 64-bit systems and should work on both equally well:

下面的代码将自动检测 32 位和 64 位系统,并且应该同样适用:

Option Explicit

#If Win64 Then
    Private Declare PtrSafe Function GetSysColor Lib "user32" _
        (ByVal nIndex As Long) As Long
    Private Declare PtrSafe Function SetSysColors Lib "user32" _
        (ByVal nChanges As Long, lpSysColor As Long, lpColorValues As Long) As Long
#Else
    Private Declare Function GetSysColor Lib "user32" _
        (ByVal nIndex As Long) As Long
    Private Declare Function SetSysColors Lib "user32" _
        (ByVal nChanges As Long, lpSysColor As Long, lpColorValues As Long) As Long
#End If

Private Const COLOR_WINDOWTEXT As Long = 8
Private Const CHANGE_INDEX As Long = 1

Public Sub RunMe()
   Dim defaultColour As Long

   'Store the default system colour
   defaultColour = GetSysColor(COLOR_WINDOWTEXT)

   'Set system colour to red
   SetSysColors CHANGE_INDEX, COLOR_WINDOWTEXT, vbRed
   MsgBox "Incorrect", , "Your result is..."

   'Set system colour to green
   SetSysColors CHANGE_INDEX, COLOR_WINDOWTEXT, vbGreen
   MsgBox "Correct", , "Your result is..."

   'Restore default value
   SetSysColors CHANGE_INDEX, COLOR_WINDOWTEXT, defaultColour

End Sub