Hello everyone,
I'm studying an Object-Oriented Programming course and I'm supposed to write a small app in C++ which draws shapes based on given coordinates. I'm trying to do it in WINAPI (I use Dev C++) and I have a problem with the WM_PAINT message, I don't think I know exactly how it works.
I have a main window which is empty and serves as a canvas for drawing shapes, and a dialog box with boxes and buttons where you input data and initiate drawing. I completed the application and it works but it unfortunately uses up 25% of my CPU all the time. I noticed that if I remove one thing from the code, it then doesn't hog the resources but the shapes aren't drawn.
This is my message loop:
Code:
while ((bRet=GetMessage (&messages, NULL, 0, 0))!=0)
{
if (bRet==-1) return 0;
else if (!IsWindow(toolbox) || !IsDialogMessage(toolbox, &messages))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
}
This is the WinProc function:
Code:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_PAINT: // this case uses 25% CPU if it's there, but doesn't draw shapes if I delete it
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;
}
I noticed that if i declare PAINTSTRUCT ps and do:
case WM_PAINT:
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
break;
then it doesn't hog resources but no shapes can be drawn (canvas window remains clear);
this is my dialog box callback function(simplified):
Code:
INT_PTR CALLBACK toolboxProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HWND wzium; //not important
PAINTSTRUCT ps;
char* color=NULL;
char colortemp[10];
HBRUSH brush;
HPEN hpen=CreatePen(PS_NULL,1,RGB(0,0,0));
RECT rect;
switch (message) /* handle the messages */
{
case WM_INITDIALOG:
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"triangle");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"circle");
SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_ADDSTRING,0,(LPARAM)"rectangle"); // add strings to combobox
return TRUE;
case WM_COMMAND:
if(HIWORD(wParam)==BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1: // the "draw" button
// here is code (i cut it out) which checks for different cases of strings in editbox and sets the color of the brush accordingly
hdc=BeginPaint(GetParent(hwndDlg),&ps);
SelectObject(hdc,brush);
SelectObject(hdc,hpen);
if (SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_GETCURSEL,0,0)==0) //if triangle radio button is chosen
{
//put coordinates in a new triangle object
triangle tr(GetDlgItemInt(hwndDlg,IDC_EDIT4,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT6,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT2,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT5,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT3,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT7,NULL,FALSE));
tr.draw(hdc); //draws triangle (polygon)
}
else if (SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_GETCURSEL,0,0)==1) //if circle is chosen
{
circle ci(GetDlgItemInt(hwndDlg,IDC_EDIT4,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT6,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT10,NULL,FALSE));
ci.draw(hdc);
}
else if (SendDlgItemMessage(hwndDlg,IDC_COMBO1,CB_GETCURSEL,0,0)==2) //if rectangle is chosen
{
rectangle re(GetDlgItemInt(hwndDlg,IDC_EDIT4,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT6,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT8,NULL,FALSE),
GetDlgItemInt(hwndDlg,IDC_EDIT9,NULL,FALSE));
re.draw(hdc);
}
DeleteObject(brush);
DeleteObject(hpen);
EndPaint(GetParent(hwndDlg),&ps);
InvalidateRect ( GetParent(hwndDlg), NULL, TRUE ); //i use this so that newly drawn shapes overwrite previous ones
return TRUE;
case IDC_BUTTON2: //exit button
//stuff here
return TRUE;
break;
case IDOK1: //clear screen button
//stuff here
return TRUE;
}
}
break;
}
return FALSE;
}
can someone explain to me how the WM_PAINT message works in this app and what can I do to make it draw shapes at 0% CPU use? thanks!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.