Yonatan
Nov 9th, 1999, 02:24 AM
Why can't I choose the colors of my MsgBox?
I think my code is OK, the only problem is, the WM_CTLCOLORMSGBOX message never reaches my form!
I've also tried the MessageBox API, in suspicion that the MsgBox VB function might be some altered version, but I get the same result.
Here's my code:
Module Code...
Option Explicit
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 Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function OleTranslateColor Lib "olepro32" (ByVal oleColor As OLE_COLOR, ByVal hPalette As Long, pColorRef As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_CTLCOLORMSGBOX = &H132
Private Const WM_DESTROY = &H2
Private Const CLR_INVALID = &HFFFFFFFF
Private lpfnPreviousWindowProcedure As Long
Private m_lhWnd As Long
Private m_lhBrush As Long
Public MsgBoxBackColor As OLE_COLOR
Public MsgBoxForeColor As OLE_COLOR
Private Function TranslateColor(ByVal oleColor As OLE_COLOR, Optional ByVal hPalette As Long = 0) As Long
If OleTranslateColor(oleColor, hPalette, TranslateColor) <> 0 Then TranslateColor = CLR_INVALID
End Function
Private Function WindowProcedure(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lBkColor As Long, lTextColor As Long
If Msg = WM_CTLCOLORMSGBOX Then
lBkColor = TranslateColor(MsgBoxBackColor)
lTextColor = TranslateColor(MsgBoxForeColor)
If lBkColor = CLR_INVALID Then lBkColor = TranslateColor(vbButtonFace)
If lTextColor = CLR_INVALID Then lTextColor = TranslateColor(vbButtonText)
Call DeleteObject(m_lhBrush)
m_lhBrush = CreateSolidBrush(lBkColor)
WindowProcedure = m_lhBrush
Call SetBkColor(wParam, lBkColor)
Call SetTextColor(wParam, lTextColor)
Exit Function
End If
If Msg = WM_DESTROY Then Call DeleteObject(m_lhBrush)
WindowProcedure = CallWindowProc(lpfnPreviousWindowProcedure, hWnd, Msg, wParam, lParam)
End Function
Public Sub Hook(ByVal lhWnd As Long)
If (m_lhWnd <> 0) Or (lhWnd = 0) Then Exit Sub
m_lhWnd = lhWnd
MsgBoxBackColor = vbButtonFace
MsgBoxForeColor = vbButtonText
lpfnPreviousWindowProcedure = SetWindowLong(m_lhWnd, GWL_WNDPROC, AddressOf WindowProcedure)
End Sub
Public Sub Unhook()
Call SetWindowLong(m_lhWnd, GWL_WNDPROC, lpfnPreviousWindowProcedure)
m_lhWnd = 0
End Sub
Form Code...
Option Explicit
Private Sub Form_Load()
Call Hook(hWnd)
MsgBoxBackColor = vbBlue
MsgBoxForeColor = vbYellow
Call MsgBox("Hello, world!") ' Should be yellow text on blue background
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call Unhook
End Sub
Help...?
------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
I think my code is OK, the only problem is, the WM_CTLCOLORMSGBOX message never reaches my form!
I've also tried the MessageBox API, in suspicion that the MsgBox VB function might be some altered version, but I get the same result.
Here's my code:
Module Code...
Option Explicit
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 Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function OleTranslateColor Lib "olepro32" (ByVal oleColor As OLE_COLOR, ByVal hPalette As Long, pColorRef As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_CTLCOLORMSGBOX = &H132
Private Const WM_DESTROY = &H2
Private Const CLR_INVALID = &HFFFFFFFF
Private lpfnPreviousWindowProcedure As Long
Private m_lhWnd As Long
Private m_lhBrush As Long
Public MsgBoxBackColor As OLE_COLOR
Public MsgBoxForeColor As OLE_COLOR
Private Function TranslateColor(ByVal oleColor As OLE_COLOR, Optional ByVal hPalette As Long = 0) As Long
If OleTranslateColor(oleColor, hPalette, TranslateColor) <> 0 Then TranslateColor = CLR_INVALID
End Function
Private Function WindowProcedure(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lBkColor As Long, lTextColor As Long
If Msg = WM_CTLCOLORMSGBOX Then
lBkColor = TranslateColor(MsgBoxBackColor)
lTextColor = TranslateColor(MsgBoxForeColor)
If lBkColor = CLR_INVALID Then lBkColor = TranslateColor(vbButtonFace)
If lTextColor = CLR_INVALID Then lTextColor = TranslateColor(vbButtonText)
Call DeleteObject(m_lhBrush)
m_lhBrush = CreateSolidBrush(lBkColor)
WindowProcedure = m_lhBrush
Call SetBkColor(wParam, lBkColor)
Call SetTextColor(wParam, lTextColor)
Exit Function
End If
If Msg = WM_DESTROY Then Call DeleteObject(m_lhBrush)
WindowProcedure = CallWindowProc(lpfnPreviousWindowProcedure, hWnd, Msg, wParam, lParam)
End Function
Public Sub Hook(ByVal lhWnd As Long)
If (m_lhWnd <> 0) Or (lhWnd = 0) Then Exit Sub
m_lhWnd = lhWnd
MsgBoxBackColor = vbButtonFace
MsgBoxForeColor = vbButtonText
lpfnPreviousWindowProcedure = SetWindowLong(m_lhWnd, GWL_WNDPROC, AddressOf WindowProcedure)
End Sub
Public Sub Unhook()
Call SetWindowLong(m_lhWnd, GWL_WNDPROC, lpfnPreviousWindowProcedure)
m_lhWnd = 0
End Sub
Form Code...
Option Explicit
Private Sub Form_Load()
Call Hook(hWnd)
MsgBoxBackColor = vbBlue
MsgBoxForeColor = vbYellow
Call MsgBox("Hello, world!") ' Should be yellow text on blue background
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call Unhook
End Sub
Help...?
------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)