How to get the current dialog background color ??

G

Guest

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 ???
 
J

Jeff Partch [MVP]

Mephisto said:
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.
 
G

Guest

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"
 
J

Jeff Partch [MVP]

Mephisto said:
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;
}
 
G

Guest

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...
 
J

Jeff Partch [MVP]

Mephisto said:
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?
 
G

Guest

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 !!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top