Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

How to get the current dialog background color ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mephisto - 04 Jun 2004 12:56 GMT
How can I get the backgroundcolor of a dialog, or generally a parent window ?
I´ve tried "GetBkColor" on my own window, my parent window, the next parent window, the next and the next, but the return value always claims the background color would be white
I´ve already used all kinds of System Colors (throug GetSysColor) but their are all wrong. Even "CTLCOLOR_DLG" returns the wrong color. Althoug I´m use the default color for my dialog
Searching for help on the internet brought up thousands of examples on how to CHANGE the background color but I found not a single tip how to GET the current background color

Is there any way to solve this problem ??
Is this silly thing realy such a difficult task ???
Jeff Partch [MVP] - 04 Jun 2004 13:41 GMT
> How can I get the backgroundcolor of a dialog, or generally a parent window ??
> I?ve tried "GetBkColor" on my own window, my parent window, the next
parent window, the next and the next, but the return value always claims the
background color would be white.
> I?ve already used all kinds of System Colors (throug GetSysColor) but
their are all wrong. Even "CTLCOLOR_DLG" returns the wrong color. Althoug
I?m use the default color for my dialog.
> Searching for help on the internet brought up thousands of examples on how to CHANGE the background color but I found not a single tip how to GET the
current background color.

> Is there any way to solve this problem ???
> Is this silly thing realy such a difficult task ???

HBRUSH hBrush = (HBRUSH)SendMessage(hWndDialog, WM_CTLCOLORDLG, (WPARAM)hDC,
(LPARAM)hWndDialog);

...possibly followed by a...

LOGBRUSH lb;
GetObject(hBrush, sizeof(lb), &lb);

...ought to work.

For other window types, there is no general mechanism that I'm aware of.
Neither is guaranteed, but it might use the
GetClassLongPtr/GCLP_HBRBACKGROUND or it might paint its background in
response to WM_ERASEBKGND (dialogs do this too). Otherwise, it might support
the HDC-in-the-WPARAM of a WM_PAINT convention or WM_PRINTCLIENT. For these
you can SelectObject a GetClientRect(hWndParent)-sized
CreateCompatibleBitmap into a CreateCompatibleDC and BitBlt your portion, or
GetPixel it to obtain the COLORREF.
Signature

Jeff Partch [VC++ MVP]

Mephisto - 08 Jun 2004 08:51 GMT
I´ve tried both ways, but they didn´t brought the results I hoped for. The SendMessage solution works, but the returned COLORREF for my background is "white" (again). The second way seems to be pretty smart but the returned value is always zero :

I added the code for the second solution here, maybe I got you wrong or made some other kind of mistake

HWND hWndParent = GetParent()
CDC dcTmp
dcTmp.CreateCompatibleDC(HDC(dc))
RECT rc
::GetClientRect(hWndParent,&rc)
HBITMAP bmp = CreateCompatibleBitmap(HDC(dc), rc.right-rc.left,rc.bottom-rc.top)
dcTmp.SelectBitmap( bmp )
m_cBkColor = GetPixel(HDC(dcTmp),1,1);      //GetPixel returns "0"
Jeff Partch [MVP] - 08 Jun 2004 13:33 GMT
> I?ve tried both ways, but they didn?t brought the results I hoped for. The
SendMessage solution works, but the returned COLORREF for my background is
"white" (again). The second way seems to be pretty smart but the returned
value is always zero :(

Both methods work equally well for me in that they return the same COLORREF
and that it is the COLORREF of the dialog background...

   static _inline COLORREF _stdcall GetParentDlgBkColor(
       IN CONST HWND hWnd)
   {
       COLORREF crRet = CLR_INVALID;
       if (hWnd && IsWindow(hWnd))
       {
           HWND hWndParent = GetParent(hWnd);
           if (hWndParent)
           {
               HDC hDC = GetDC(hWndParent);
               if (hDC)
               {
                   HBRUSH hBr = (HBRUSH)SendMessage(
                       hWndParent, WM_CTLCOLORDLG, (WPARAM)hDC,
                           (LPARAM)hWndParent);
                   if (hBr)
                   {
                       LOGBRUSH lb;
                       if ((BOOL)GetObject(hBr, sizeof(lb), &lb))
                       {
                           if (BS_SOLID == lb.lbStyle)
                               crRet = lb.lbColor;
                       }
                   }
                   ReleaseDC(hWndParent, hDC);
               }
           }
       }
       return crRet;
   }

   static _inline COLORREF _stdcall GetParentDlgBkColor2(
       IN CONST HWND hWnd)
   {
       COLORREF crRet = CLR_INVALID;
       if (hWnd && IsWindow(hWnd))
       {
           HWND hWndParent = GetParent(hWnd);
           if (hWndParent)
           {
               RECT rc;
               if (GetClientRect(hWndParent, &rc))
               {
                   HDC hDC = GetDC(hWndParent);
                   if (hDC)
                   {
                       HDC hdcMem = CreateCompatibleDC(hDC);
                       if (hdcMem)
                       {
                           HBITMAP hBmp = CreateCompatibleBitmap(hDC,
                               rc.right, rc.bottom);
                           if (hBmp)
                           {
                               HGDIOBJ hOld = SelectObject(hdcMem, hBmp);
                               if (hOld)
                               {
                                   if (SendMessage(hWndParent,
                                       WM_ERASEBKGND, (WPARAM)hdcMem, 0))
                                   {
                                       crRet = GetPixel(hdcMem, 0, 0);
                                   }
                                   SelectObject(hdcMem, hOld);
                               }
                               DeleteObject(hBmp);
                           }
                           DeleteDC(hdcMem);
                       }
                       ReleaseDC(hWndParent, hDC);
                   }
               }
           }
       }
       return crRet;
   }

Signature

Jeff Partch [VC++ MVP]

Mephisto - 08 Jun 2004 14:41 GMT
Thanks for adding your code !! I´ve tried it again with your implementation, and they both return the same COLORREF as you said. But it´s again the same wrong COLORREF as for example GetSysColor(CTLCOLOR_DLG) returns
The default background and dialog color as it´s defined in my system settings and the color value vc and vb use by default for all of their dialogs are the same. But asking for it I always get a wrong color value returned !? Okay, it looks almost similar, but it´s simply wrong..
AAGGHH...That´s so pesky...
Jeff Partch [MVP] - 08 Jun 2004 15:31 GMT
> Thanks for adding your code !! I?ve tried it again with your
implementation, and they both return the same COLORREF as you said. But it?s
again the same wrong COLORREF as for example GetSysColor(CTLCOLOR_DLG)
returns.
> The default background and dialog color as it?s defined in my system
settings and the color value vc and vb use by default for all of their
dialogs are the same. But asking for it I always get a wrong color value
returned !? Okay, it looks almost similar, but it?s simply wrong...
> AAGGHH...That?s so pesky...

Can I ask what are the values of the COLORREF you get and of the COLORREF
you expect? I might can understand why the first method would differ, but
why the second would is eluding me: It would after all be the actual
COLORREF of the actual background as painted by the actual WindowProc
wouldn't it?
Signature

Jeff Partch [VC++ MVP]

Mephisto - 09 Jun 2004 10:36 GMT
Yes you can !! Finally that was the question that discovered the root of all that evil !!! I´m ashamed to say the returned value was right from the beginning. I only used it the wrong way. In my function to replace the background color with the background value from its parent I only had to invert the Red and the Blue part in the COLORREF and now it´s working. That´s why the color always looked almost similar. That´s the risk when you use code you haven´t written by yourself.

Okay, that´s it. Thank you very much for your help Jeff !!!!!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.