When I added rectangle and ellipse support the shapes all start with their first X and Y at 0, 0. I don't know why. Also, the rectangles and ellipses disapear after you let the mouse button up. Could someone please help me figure out what's going on?
Some things about this program that might be helpful:
- shape is a global int
- LINE, ELLIPSE, and RECTANGLE are defined at the top of the file
- All the "arraycount"s are set to 0 initially
- shape is set to LINE initially
- In WM_COMMAND the ID_SHAPE_... refers to a menu Shape->... with both "..."s in this bullet referring to one of the shape names
- In WM_COMMAND the clear function draws extra lines (drawn before the clear)
Code:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
static POINT mouse1L[100];
static POINT mouse2L[100];
static POINT mouse1E[100];
static POINT mouse2E[100];
static POINT mouse1R[100];
static POINT mouse2R[100];
switch (message) /* handle the messages */
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_FILE_CLEAR:
arraycountL=0;
arraycountE=0;
arraycountR=0;
mouse1L[0].x=-1;
mouse1L[0].y=-1;
mouse2L[0].x=-1;
mouse2L[0].y=-1;
mouse1R[0].x=-1;
mouse1R[0].y=-1;
mouse2R[0].x=-1;
mouse2R[0].y=-1;
mouse1E[0].x=-1;
mouse1E[0].y=-1;
mouse2E[0].x=-1;
mouse2E[0].y=-1;
InvalidateRect(hwnd,NULL,true); //Force a repaint
UpdateWindow(hwnd);
break;
case ID_SHAPE_LINE:
shape=LINE;
break;
case ID_SHAPE_ELLIPSE:
shape=ELLIPSE;
break;
case ID_SHAPE_RECT:
shape=RECTANGLE;
break;
}
break;
case WM_PAINT:
hDC=BeginPaint(hwnd, &ps);
for(int l=0;l<=arraycountL;l++){
MoveToEx(hDC,mouse1L[l].x,mouse1L[l].y,NULL);
LineTo(hDC,mouse2L[l].x,mouse2L[l].y);}
for(int e=0;e<=arraycountE;e++){
Ellipse(hDC, mouse1E[e].x, mouse1E[e].y, mouse2E[e].x, mouse2E[e].y+);
}
for(int r=0;r<=arraycountR;r++){
Rectangle(hDC, mouse1R[r].x, mouse1R[r].y, mouse2R[r].x, mouse2R[r].y);
}
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_LBUTTONDOWN:
if(shape==LINE){
mouse1L[arraycountL].x=LOWORD(lParam);
mouse1L[arraycountL].y=HIWORD(lParam);
}
else if(shape==ELLIPSE){
mouse1E[arraycountE].x=LOWORD(lParam);
mouse1E[arraycountE].y=HIWORD(lParam);
}
else if(shape==RECTANGLE){
mouse1R[arraycountR].x=LOWORD(lParam);
mouse1R[arraycountR].y=HIWORD(lParam);
}
break;
case WM_LBUTTONUP:
if(shape==LINE){
mouse2L[arraycountL].x=LOWORD(lParam);
mouse2L[arraycountL].y=HIWORD(lParam);
arraycountL++;
InvalidateRect(hwnd,NULL,true); //Force a repaint
UpdateWindow(hwnd);}
if(shape==ELLIPSE){
mouse2E[arraycountE].x=LOWORD(lParam);
mouse2E[arraycountE].y=HIWORD(lParam);
arraycountE+=1;}
if(shape==RECTANGLE){
mouse2R[arraycountR].x=LOWORD(lParam);
mouse2R[arraycountE].y=HIWORD(lParam);
arraycountR+=1;}
InvalidateRect(hwnd,NULL,true); //Force a repaint
UpdateWindow(hwnd);
break;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON){
if(shape==LINE){
mouse2L[arraycountL].x=LOWORD(lParam);
mouse2L[arraycountL].y=HIWORD(lParam);}
if(shape==ELLIPSE){
mouse1E[arraycountE].x=LOWORD(lParam);
mouse1E[arraycountE].y=HIWORD(lParam);}
if(shape==RECTANGLE){
mouse1R[arraycountR].x=LOWORD(lParam);
mouse1R[arraycountR].y=HIWORD(lParam);}
InvalidateRect(hwnd,NULL,true); //Force a repaint
UpdateWindow(hwnd);
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}