Window Procedure to draw a rectangle between two click points in VC++

Abhisek
Posted by Abhisek under Others category on | Views : 2667
This is a VC++ Window Procedure which can draw a rectangle between two click points.

HWND hwin;/*HandleToWindow instance */
HDC dc;/*HandleToDeviceContext instance*/
int click=0;
int x1, x2, y1, y2;
/*above is the public section*/
long _stdcall WndProc(HWND hw, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_LBUTTONDOWN:
if(click==0)
{
x1=LOWORD(lp);
y1=HIWORD(lp);
click=1;
}
else
if(click==01)
{
x2=LOWORD(lp);
y2=HIWORD(lp);
Rectangle(dc,x1,y1,x2,y2);
click=0;
}
break;
case WM_DESTROY:
PostQuitMessage(hwin);
break;
default:
return DefWindowProc(hw,msg,wp,lp);
}
return(0L);
}



DESCRIPTION


Here when the left button is clicked first time then we are collecting (x1,y1) coordinates and (x2,y2) coordinates on second click using LOWORD and HIWORD .
x1=LOWORD(lp);
y1=HIWORD(lp);


  x2=LOWORD(lp);
y2=HIWORD(lp);

Then we are passing the coordinates to Rectangle(dc,x1,y1,x2,y2) function where 'dc' is the device context instance.

Comments or Responses

Login to post response