当前位置: 首页 > 工具软件 > msgBox > 使用案例 >

vba msgbox html 标签,VBA更改MsgBox中的文本颜色

赵俊远
2023-12-01

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

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

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

下面的代码将自动检测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

 类似资料: