My goal is to bring up the Color Picker dialog and redraw the rectangle to the newly selected color. When I run this it will not work. However, if I set a breakpoint at the DrawRectangle function and step throught the code, it redraws the rectangle like it should.
With my limited knowledge of Win32 API/GDI I can't figure out why it would work stepping through the code and not work if I just execute it.
Here is the code:
Code:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Color picker dialog variables
CHOOSECOLOR cc;
static COLORREF customColor[16];
HBRUSH hbrush;
static DWORD rgbCurrent = RGB(255, 0, 0);
// Initialize CHOOSECOLOR
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = hwnd;
cc.lpCustColors = (LPDWORD) customColor;
cc.rgbResult = rgbCurrent;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
switch (message) /* handle the messages */
{
case WM_PAINT:
DrawRectangle(hwnd, rgbCurrent);
break;
case WM_COMMAND:
switch(wParam)
{
case IDM_COLOR_PICK:
if (ChooseColor(&cc)==TRUE) {
hbrush = CreateSolidBrush(cc.rgbResult);
rgbCurrent = cc.rgbResult;
DrawRectangle(hwnd, rgbCurrent);
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void DrawRectangle(HWND hwnd, DWORD rgbCurrent)
{
// Varialbes for rectangle
HDC hdc;
PAINTSTRUCT ps;
HBRUSH rectBrush;
// color rectangle
hdc = BeginPaint(hwnd, &ps);
rectBrush = CreateSolidBrush(rgbCurrent);
SelectObject(hdc, rectBrush);
Rectangle(hdc, 20, 20, 100, 100);
DeleteObject(rectBrush);
EndPaint(hwnd, &ps);
}