I asked my question yesterday but could not get a correct answer. now ill ask it more clearly.
using win api I created a window and a button. infront of the button there is a circle drawn in green color. once the button is pressed the circle should turn to red color.
(I’m using C++, and mingw compiler.)
here is my code
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
HBRUSH brusha;
brusha=CreateSolidBrush(RGB(0,255,0));
switch(msg)
{
case WM_CREATE:
{
b=CreateWindowEx(WS_EX_CLIENTEDGE,
"BUTTON",
"red",
WS_CHILD|WS_VISIBLE|
BS_DEFPUSHBUTTON,
350,
100,
100,
40,
hWnd,
(HMENU)BUTTON,
GetModuleHandle(NULL),
NULL);
}
break;
case WM_PAINT:
{
hDC=BeginPaint(hWnd,&ps);
SelectObject(hDC,brusha);
Ellipse(hDC, 20, 20, 100, 100);
EndPaint(hWnd, &ps);
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case BUTTON:
{
brusha=CreatSolideBrush(RGB(255,0,0));
InvalidateRect( hWnd,0,false);
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
this compiles without errors. but nothing happens on button click
Well the problem is that you you always draw with a green brush. Look at your code
In this code
brushais always going to be a green brush when you go intoWM_PAINT.You seem to think that just because you assign a red brush to the
brushavariable in theWM_COMMANDpart that somehow that is going to be remembered for the next paint, but that’s not true. Remember that in C++ variables are created afresh each time you enter a function, and destroyed each time you exit a function. So the way you have written the code cannot work.Probably the simplest way is to make the
hbrushavariablestatic. Static variables are not created and destroyed each time you enter and exit a funciton. Something like this