Results 1 to 12 of 12

Thread: MsgBox BackColor !!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    MsgBox BackColor !!!!

    Hi there,

    Is there a way to change the background color of the standard MsgBox without subclassing it ?

    When I use the SetBkColor API without first subclassing the MsgBox, nothing seems to happen !

    I have used the FillRect API with a Windows Brush but this unfortuantly covers up the whole MsgBox Client Area and hides any Buttons on the MsgBox as well as any text !

    Any insights on this ?
    Last edited by BLUE_SEA; Jan 22nd, 2006 at 07:36 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MsgBox BackColor !!!!

    The easiest way to do anything you want with a message box is to make your own from a standard VB form.

    My whole development team uses a custom message that was created a long time ago, and we just pop it into each of our projects when we start them.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: MsgBox BackColor !!!!

    Hack,

    Thanks but I really want to know how this is done for a standard MsgBox.

    I am needing this for learning purposes.

    Any takers ?

    Regards.

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: MsgBox BackColor !!!!

    I don't think you can do this without subclassing it.
    Show us the code you used to try the things you mentioned.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: MsgBox BackColor !!!!

    Thanks moeur,

    Below is the code I am testing . Note that the I commented out the SetBkColor line because it just doesn't work. Instead the FillRect does color the background but covers up all children windows ie(BUTTON & STATIC) Controls nd looks horrible.

    VB Code:
    1. Declare Function FillRect Lib "user32" _
    2. (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    3.  
    4. Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    5.  
    6. Declare Function ReleaseDC Lib "user32" _
    7. (ByVal hwnd As Long, ByVal hdc As Long) As Long
    8.  
    9. Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    10. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    11.  
    12. Declare Function SetTimer Lib "user32" _
    13. (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, _
    14. ByVal lpTimerFunc As Long) As Long
    15.  
    16. Declare Function killtimerAPI Lib "user32" Alias "KillTimer" _
    17. (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    18.  
    19. Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    20.  
    21. Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    22.  
    23. Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    24.  
    25. Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, _
    26. ByVal crColor As Long) As Long
    27.  
    28. Type RECT
    29.     Left As Long
    30.     Top As Long
    31.     Right As Long
    32.     Bottom As Long
    33. End Type
    34.  
    35. Dim udtR As RECT
    36. Dim lngTimerID, lngMsgboxHwnd, lngMsgBoxDC, mBrush As Long
    37.  
    38.  
    39. Sub killTimer()
    40.  
    41.     killtimerAPI 0, lngTimerID
    42.  
    43. End Sub
    44.  
    45. Sub ShowMsgBox()
    46.  
    47.     lngTimerID = SetTimer(0, 0, 100, AddressOf TimerCallBack)
    48.     MsgBox "HELLO!"
    49.     ReleaseDC lngMsgboxHwnd, lngMsgBoxDC
    50.     DeleteObject mBrush
    51.  
    52. End Sub
    53.  
    54. Sub TimerCallBack()
    55.  
    56.     killTimer
    57.     lngMsgboxHwnd = FindWindow("#32770", vbNullString)
    58.     GetClientRect lngMsgboxHwnd, udtR
    59.     lngMsgBoxDC = GetDC(lngMsgboxHwnd)
    60.     mBrush = CreateSolidBrush(vbYellow)
    61.     'SetTextColor lngMsgBoxDC, mBrush '\ this doesn't work !
    62.     FillRect lngMsgBoxDC, udtR, mBrush '\ this covers up the whole client area
    63.                                    '\ including OK button and prompt text !!
    64. End Sub

    Regards.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: MsgBox BackColor !!!!

    If you were willing to do all this, why are you avoiding subclassing?

  7. #7
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: MsgBox BackColor !!!!

    Can someone post up the code for subclassing?
    Im interested in seeing it.

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: MsgBox BackColor !!!!

    Quote Originally Posted by moeur
    If you were willing to do all this, why are you avoiding subclassing?
    moeur,

    I am just experimenting with this API stuff to understand how it all works... So for instance : If the SetBKColor function doesn't work to change the color of the MsgBox in my code, I would like to know why and if I figure out then I know I have learnt something in the process which I can then apply to other different scenarios.

    I have seen this codehttp://64.233.161.104/search?q=cache...ORDLG%3D&hl=en that uses a WH_CALLWNDPROC Hook to detect the creation of the MsgBox by capturing the WM_CREATE message. Once it is captured it subclasses the MsgBox and traps the WM_CTLCOLORDLG message before calling the SetBkColor function . This works fine but I want to know why this SetBKColor works for some windows without the need for subclassing and for some other like in this particular MsgBox scenario subclassing is required !!!

    Regarding the WH_CALLWNDPROC Hook, I thought it worked only for watching messages sent to a window via the SendMessage API function NOT for messages generated by the user through the UI. Can you confirm if this is true ?

    Thanks for your help with this .
    Last edited by BLUE_SEA; Jan 22nd, 2006 at 11:03 AM.

  9. #9
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: MsgBox BackColor !!!!

    Regarding the WH_CALLWNDPROC Hook, I thought it worked only for watching messages sent to a window via the SendMessage API function NOT for messages generated by the user through the UI. Can you confirm if this is true ?
    The WH_CALLWNDPROC hook intercepts messages that were "sent" to a window whether they were sent by a user with sendmessage or sent by the system.

    Another type of useful hook is the WH_GETMESSAGE hook which intercepts "posted" messages.

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: MsgBox BackColor !!!!

    Quote Originally Posted by moeur
    The WH_CALLWNDPROC hook intercepts messages that were "sent" to a window whether they were sent by a user with sendmessage or sent by the system.

    Another type of useful hook is the WH_GETMESSAGE hook which intercepts "posted" messages.
    Thanks.

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: MsgBox BackColor !!!!

    blue sea, I think maybe its because it is being called within the applications 'process memory'; when the application's messages are hooked, SetBkColor is being called within the target application's process memory. I think.

    For instance, if you create an application(vb.net) with a textbox, and if you make the textbox in a way that if you type something on it, it would be "*". Basically, change the passwordchar property to "*". Then you call:

    SendMessage(hwndOfTxtBox, WM_GETTEXT,....) within that same application.

    it will get the actual characters(not the "*") that you typed. But if you call that SendMessage() code from a different application, it will not give you the actual characters. But if you can call that SendMessage() code within the target application's 'process memory' (by hooking for instance), then it will show the actual characters.

    But then again, it may not be the reason for why SetBkColor does not work the way you want it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width