Click to See Complete Forum and Search --> : How do you get the address of a return variable?


matthew1
March 29th, 2008, 10:54 AM
Compiler: Dev-c++ v4.9.9.2
Operating System: Windows Vista
Environment: win32 (No MFC)

Hello, I am a novice c++/win32 programmer, and I am trying to change the background color of a win32 static control. I am attempting to do this by getting the HBRUSH handle of the static control through the win32 function:

SendMessage(
(HWND) hwnd,
WM_CTLCOLORSTATIC ,
(WPARAM) wParam,
(LPARAM) lParam
);



Now as you know this function returns a handle to the brush of the control. It seems to me that this return is only the value of the brush and not the actual brush, so I think that I would need to get the address of the return brush in order to actually change the background color of the control. However my problem is that, as a novice, I niether know how to get the address of such a return variable or if it would even make sense or help to try to manipulate the brush through the return of SendMessage().

(Note: I know the variable must first exist or be declared outside of the function and be passed to it through a pointer or a reference.)


My questions are:
(1)How do you get the address of a return variable from a "non-application-defined" function such as SendMessage() or any Win32 api for that matter?

(2)Could you explain to me how how to change the background color of a static control?(I really don't know what I'm doing.)


Thank you very much for your help. THANK YOU......

a71104
March 29th, 2008, 12:10 PM
as the documentation states, the WM_CTLCOLORSTATIC message is sent to your parent window (dialog box or whatever) by the static control when it is about to be repainted and it needs to know how to paint the background. in response to that message you must provide the handle to the brush you want to use to paint the control's background.

in order to get that handle create it using one of the brush creation functions (see http://msdn2.microsoft.com/en-us/library/ms532371(VS.85).aspx) such as CreateSolidBrush, or just get a default one from the stock using GetStockObject. if you choose to create it you must also destroy it when you're not using it anymore (i.e. when you destroy either the static control or the whole parent window): for that purpose use DeleteObject.

when you've gotten your HBRUSH you just return it from the parent window's WndProc (or DialogProc if it's a dialog box) as the function's return value in response to the WM_CTLCOLORSTATIC message; example:

LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HBRUSH hStaticBrush;

switch (uMsg)
{
case WM_CREATE:
hStaticBrush = CreateSolidBrush(0x00FF00); // lime green color
if (hStaticBrush == NULL)
{
// error management...
}
break;

case WM_CTLCOLORSTATIC:
if ((HANDLE)lParam == hMyStaticControl) // if it's our static control that's requesting...
{
return (LRESULT)hBrush;
}
break;

.
.
.

case WM_DESTROY:
DeleteObject(hStaticBrush);
break;

}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


not tested, but it should work; hope it helps.