Sie sind auf Seite 1von 33

/*

Assignment No.:- 01
Title:- Write a Windows program to demonstrate Line Drawing with left
mouse
button. The color and width of Line should change with every
new Line.
*/

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)


{
HDC hdc;
HPEN hpen;
PAINTSTRUCT ps;
static int x,y;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN :
hdc=GetDC(hwnd);
x=LOWORD(lParam);
y=HIWORD(lParam);

hpen=CreatePen(PS_SOLID,rand()%10,RGB(rand()%255,rand()%255,rand()%
255));
SelectObject(hdc,hpen);
MoveToEx(hdc,x,y,0);
LineTo(hdc,x+50,y+100);
DeleteObject(hpen);
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/*
Assignment No.:-02
Title:- Write aWindows program that display small rectangles with every
left mouse click, double clicking on existing rectangle should erase the
rectangle.
*/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
POINT pt;
static int cx,cy;
rect.left=100;
rect.top=100;
rect.right=200;
rect.bottom=200;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN :
hdc=GetDC(hwnd);

Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
ReleaseDC(hwnd,hdc);
return 0;

case WM_LBUTTONDBLCLK :
hdc=GetDC(hwnd);
cx=LOWORD(lParam);
cy=HIWORD(lParam);
pt.x=cx;
pt.y=cy;
if(PtInRect(&rect,pt))
{
InvalidateRect(hwnd,&rect,true);
}
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:- 03
Title:- Write a Windows program to display size of window and no. of Left
button
clicks, no. of Right button clicks, no. of Doubble clicks. This
data
should be displayed on two seperete lines. Size should be
updated when
user resizes the window object.
*************************************************************************
********/
LRESULT CALLBACK WndProc( HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam
)
{
HDC hdc;
PAINTSTRUCT ps;
static RECT rect;

static bool ldbclk,rdbclk;


static int lcnt,rcnt,ldbcnt,rdbcnt,wsize,hsize;
static char str[80];

switch(iMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);

sprintf(str,"No. of Left Button Clicks : %d",lcnt);


TextOut(hdc,10,20,str,strlen(str));
sprintf(str,"No. of Right Button Clicks : %d",rcnt);
TextOut(hdc,10,40,str,strlen(str));
sprintf(str,"No. of L-Doubble Button Clicks :
%d",ldbcnt);
TextOut(hdc,10,60,str,strlen(str));
sprintf(str,"No. of R-Double Button Clicks :
%d",rdbcnt);
TextOut(hdc,10,80,str,strlen(str));
sprintf(str,"Window Width & Height : %d,
%d",wsize,hsize);
TextOut(hdc,10,100,str,strlen(str));
EndPaint(hwnd,&ps);
return 0;

case WM_SIZE:
wsize=LOWORD(lParam);
hsize=HIWORD(lParam);
InvalidateRect(hwnd,NULL,true);
return 0;

case WM_LBUTTONDOWN:
SetTimer(hwnd,1,GetDoubleClickTime(),NULL);
return 0;

case WM_RBUTTONDOWN:
SetTimer(hwnd,2,GetDoubleClickTime(),NULL);
return 0;

case WM_LBUTTONDBLCLK:
ldbclk=true;
return 0;
case WM_RBUTTONDBLCLK:
rdbclk=true;
return 0;

case WM_TIMER:
switch(wParam)
{
case 1:
if(ldbclk==true)
{
ldbcnt++;
ldbclk=false;
}
else
lcnt++;

InvalidateRect(hwnd,NULL,true);
KillTimer(hwnd,1);
return 0;

case 2:
if(rdbclk==true)
{
rdbcnt++;
rdbclk=false;
}
else
rcnt++;

InvalidateRect(hwnd,NULL,true);
KillTimer(hwnd,2);
return 0;
}

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*******************
Assignment No.:- 04
Title:- Write a window program to change background of client area on
left click.
And change pattern on each right click
*************************************************************************
*****************/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HBRUSH hbr;
PAINTSTRUCT ps;
RECT rect;
static flag=0,cnt=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
flag=0;
SetTimer(hwnd,1,500,NULL);
return 0;

case WM_RBUTTONDOWN:
flag=1;
cnt++;
return 0;

case WM_TIMER:
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rect);
if(flag==1)
{

hbr=CreateHatchBrush(cnt,RGB(rand()%255,rand()%255,rand()%255));
if(cnt==6)
cnt=0;
}
else
{

hbr=CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
}
SelectObject(hdc,hbr);
FillRect(hdc,&rect,hbr);
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-05
Title:- Write a Window program to display the characters entered by user
from
keyboard. Consider alphabets and number only.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static char str[10];

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,50,50,str, strlen(str));
ReleaseDC(hwnd,hdc);
EndPaint(hwnd,&ps);
return 0;

case WM_CHAR:
sprintf(str,"You Pressed = %c",wParam);
InvalidateRect(hwnd,NULL,wParam);
return 0;

case WM_TIMER:
KillTimer(hwnd,1);
sprintf(str,"You Pressed = %c",wParam);
InvalidateRect(hwnd,NULL,true);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-06
Title:- Write a window program to demonstrate various styles of pen.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HPEN hpen;
PAINTSTRUCT ps;
static int x,y,cnt=-1;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
cnt++;
hdc=GetDC(hwnd);
x=LOWORD(lParam);
y=HIWORD(lParam);
if(cnt==5)
cnt=0;

hpen=CreatePen(cnt,1,RGB(255,0,0));//rand()%255,rand()%255,rand()%2
55));
SelectObject(hdc,hpen);
MoveToEx(hdc,x,y,0);
LineTo(hdc,x+50,y+100);
DeleteObject(hpen);
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

/************************************************************************
*********
Assignment No.:-07
Title:- Write a window program to demonstrate freehand drawing.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int flag=0;
static int x1,y1,x2,y2;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_LBUTTONDOWN:
if(flag==0)
{
x1=LOWORD(lParam);
y1=HIWORD(lParam);
flag=1;
}
return 0;

case WM_MOUSEMOVE :
if(flag==1)
{
hdc=GetDC(hwnd);
x2=LOWORD(lParam);
y2=HIWORD(lParam);
MoveToEx(hdc,x1,y1,0);
LineTo(hdc,x2,y2);
ReleaseDC(hwnd,hdc);
x1=x2;
y1=y2;
}
return 0;
case WM_LBUTTONUP :
flag=0;
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-08
Title:- Write a window program to display random rectangles, with random
colors
at random position of random size on random clicks
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbr;
HPEN hpen;
static int cx,cy,cnt;
static POINT pt1[500],pt2[500];
int i=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<cnt;i++)
{

Rectangle(hdc,pt1[i].x,pt1[i].y,pt2[i].x,pt2[i].y);
}
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN :
hdc=GetDC(hwnd);
cnt++;
pt1[cnt].x=LOWORD(lParam)-rand()%100;
pt1[cnt].y=HIWORD(lParam)-rand()%100;
pt2[cnt].x=LOWORD(lParam)+rand()%100;
pt2[cnt].y=HIWORD(lParam)+rand()%100;
ReleaseDC(hwnd,hdc);
SetTimer(hwnd,1,200,NULL);
return 0;

case WM_TIMER:
KillTimer(hwnd,1);
hdc=GetDC(hwnd);

hpen=CreatePen(PS_SOLID,1,RGB(rand()%255,rand()%255,rand()%255));
SelectObject(hdc,hpen);

Rectangle(hdc,pt1[cnt].x,pt1[cnt].y,pt2[cnt].x,pt2[cnt].y);
cnt++;
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:- 09
Title:- Write a window that capture Home, Page Up, Page Down, End and all
arrow keys as user presses these keys. Program should display
appropriate message in the client area.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYDOWN:

switch(wParam)
{
case VK_HOME:
MessageBox(hwnd,"Home Key Pressed","Key",1);
break;
case VK_NEXT:
MessageBox(hwnd,"PageDown Key
Pressed","Key",1);
break;
case VK_PRIOR:
MessageBox(hwnd,"PageUp Key
Pressed","Key",1);
break;
case VK_END:
MessageBox(hwnd,"End Key Pressed","Key",1);
break;
case VK_LEFT:
MessageBox(hwnd,"Left Arrow Key
Pressed","Key",1);
break;
case VK_RIGHT:
MessageBox(hwnd,"Right Arrow Key
Pressed","Key",1);
break;
case VK_UP:
MessageBox(hwnd,"Up Arrow Key
Pressed","Key",1);
break;
case VK_DOWN:
MessageBox(hwnd,"Down Arrow Key
Pressed","Key",1);
break;
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-10
Title:- Write a Window program to display sine wave.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wparam,LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;

static int x,y;


static int i;
static POINT pt[500];

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_SIZE:
x=LOWORD(lparam);
y=HIWORD(lparam);
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MoveToEx(hdc,0,y/2,NULL);
LineTo(hdc,x,y/2);

for(i=0;i<500;i++)
{
pt[i].x=i*x/500;
pt[i].y=(int)(y/2*(1-sin(2*3.14159*i/500)));
}
Polyline(hdc,pt,500);

EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wparam,lparam);
}
/************************************************************************
*********
Assignment No.:-11
Title:- Write a window program to draw the lines using arrow keys.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int static cx=50,cy=50;
static int cnt;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYDOWN:
hdc=GetDC(hwnd);
switch(wParam)
{
case VK_DOWN:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx,cy+5);
cy=cy+5;
break;

case VK_UP:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx,cy-5);
cy=cy-5;
break;

case VK_LEFT:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx-5,cy);
cx=cx-5;
break;

case VK_RIGHT:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx+5,cy);
cx=cx+5;
break;
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-12
Title:- Write a window program to create filled rectangle and circles on
alternate left click. New figure should not erase the previous
one.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbr;
HRGN hrgn;
RECT rect;
static int cx,cy,flag=1;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
cx=LOWORD(lParam);
cy=HIWORD(lParam);
hbr=CreateSolidBrush(RGB(150,0,0));

if(flag==1)
{
flag=0;
rect.left=cx;
rect.top=cy;
rect.right=cx+100;
rect.bottom=cy+100;
FillRect(hdc,&rect,hbr);
}
else
{
flag=1;
hrgn=CreateEllipticRgn(cx,cy,cx+100,cy+100);
SelectObject(hdc,hbr);
Ellipse(hdc,cx,cy,cx+100,cy+100);
FillRgn(hdc,hrgn,hbr);
DeleteObject(hbr);
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-13
Title:- Write a window program to create various brushes with different
patterns
and change the background color and fill pattern on each left
mouse clicks.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HBRUSH hbr;
static int cnt=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
cnt++;
if(cnt==7)
cnt=1;
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rect);
switch(cnt)
{
case 1:
hbr=CreateHatchBrush(HS_HORIZONTAL,rand());
FillRect(hdc,&rect,hbr);
break;
case 2 :
hbr=CreateHatchBrush(HS_VERTICAL,rand());
FillRect(hdc,&rect,hbr);
break;
case 3 :
hbr=CreateHatchBrush(HS_BDIAGONAL,rand());
FillRect(hdc,&rect,hbr);
break;
case 4 :
hbr=CreateHatchBrush(HS_CROSS,rand());
FillRect(hdc,&rect,hbr);
break;
case 5 :
hbr=CreateHatchBrush(HS_FDIAGONAL,rand());
FillRect(hdc,&rect,hbr);
break;
case 6 :
hbr=CreateHatchBrush(HS_DIAGCROSS,rand());
FillRect(hdc,&rect,hbr);
break;
case 7:
hbr=CreateSolidBrush(rand());
FillRect(hdc,&rect,hbr);
break;
}
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-14
Title:- Write a window program to change the background color of the
window
after every 2 seconds. And at each mouse click fill pattern
should change.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HBRUSH hbr;
static flag=0,cnt=0;

switch(iMsg)
{
case WM_CREATE:
SetTimer(hwnd,1,2000,NULL);
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
flag=1;
cnt++;
return 0;
case WM_TIMER:
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rect);
if(flag==1)
{

hbr=CreateHatchBrush(cnt,RGB(rand()%255,rand()%255,rand()%255));
if(cnt==6)
cnt=0;
}
else
{

hbr=CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
}
SelectObject(hdc,hbr);
FillRect(hdc,&rect,hbr);
ReleaseDC(hwnd,hdc);
return 0;

case WM_RBUTTONDOWN:
KillTimer(hwnd,1);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-15
Title:- Write a window program to display a rectangle with a diagonal
line in it
while dragging the left mouse button.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int x,y,x2,y2,flag=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_LBUTTONDOWN:
SetCapture(hwnd);
InvalidateRect(hwnd,NULL,true);
x=LOWORD(lParam);
y=HIWORD(lParam);
flag=1;
return 0;

case WM_MOUSEMOVE:
hdc=GetDC(hwnd);
if(flag==1)
{
x2=LOWORD(lParam);
y2=HIWORD(lParam);
Rectangle(hdc,x,y,x2,y2);
MoveToEx(hdc,x,y,0);
LineTo(hdc,x2,y2);
}
return 0;

case WM_LBUTTONUP:
hdc=GetDC(hwnd);
x2=LOWORD(lParam);
y2=HIWORD(lParam);
Rectangle(hdc,x,y,x2,y2);
MoveToEx(hdc,x,y,0);
LineTo(hdc,x2,y2);
flag=0;
ReleaseCapture();
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-16
Title:- Write a window program for stretching the line with mouse.
The color of line should change each time.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HPEN hpen;
RECT rect;
PAINTSTRUCT ps;
static int x1,y1,x2,y2;
static int flag=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN :
hdc=GetDC(hwnd);
x1=LOWORD(lParam);
y1=HIWORD(lParam);
flag=1;
return 0;

case WM_MOUSEMOVE :

hdc=GetDC(hwnd);
if(flag)//or u can use if(wParam==MK_LBUTTON)
{
x2=LOWORD(lParam);
y2=HIWORD(lParam);
GetClientRect(hwnd,&rect);
InvalidateRect(hwnd,&rect,true);
}
ReleaseDC(hwnd,hdc);
return 0;

case WM_LBUTTONUP:
flag=0;
hdc=GetDC(hwnd);
x2=LOWORD(lParam);
y2=HIWORD(lParam);

hpen=CreatePen(PS_SOLID,1,RGB(rand()%255,rand()%255,rand()%255));
SelectObject(hdc,hpen);
MoveToEx(hdc,x1,y1,0);
LineTo(hdc,x2,y2);
DeleteObject(hpen);

ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-17
Title:- Write a window program to draw following shapes on client area
when the
user presses the keys:
C: Circle, R: Rectangle, E: Ellipse, L: Line, G: Round Rect.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
char ch;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYUP:
hdc=GetDC(hwnd);
ch=(char)wParam;
if(ch=='R')
Rectangle(hdc,50,50,150,150);
if(ch=='G')
RoundRect(hdc,350,200,400,400,30,30);
if(ch=='C')
Ellipse(hdc,100,100,150,150);
if(ch=='E')
Ellipse(hdc,300,300,100,200);
if(ch=='L')
{
MoveToEx(hdc,20,20,0);
LineTo(hdc,250,400);
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-18
Title:- Write a window program that display a small rectangle with every
left
mouse click and when the user clicks inside the rectangle it
should erase.
(Don't use PtInRect function).
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int cx,cy,cnt;
static POINT pt1[100],pt2[100];
int i=0;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<cnt;i++)
{

Rectangle(hdc,pt1[i].x,pt1[i].y,pt2[i].x,pt2[i].y);
}
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN :
hdc=GetDC(hwnd);
pt1[cnt].x=LOWORD(lParam)-50;
pt1[cnt].y=HIWORD(lParam)-50;
pt2[cnt].x=LOWORD(lParam)+50;
pt2[cnt].y=HIWORD(lParam)+50;
ReleaseDC(hwnd,hdc);
SetTimer(hwnd,1,200,NULL);
return 0;

case WM_LBUTTONDBLCLK :
KillTimer(hwnd,1);
hdc=GetDC(hwnd);
cx=LOWORD(lParam);
cy=HIWORD(lParam);

for(i=0;i<cnt;i++)
{
if(cx>pt1[i].x && cx <pt2[i].x && cy>pt1[i].y &&
cy<pt2[i].y)
{
pt1[i].x=0;
pt1[i].y=0;
pt2[i].x=0;
pt2[i].y=0;
}
}
InvalidateRect(hwnd,NULL,true);
ReleaseDC(hwnd,hdc);
return 0;

case WM_TIMER:
KillTimer(hwnd,1);
hdc=GetDC(hwnd);

Rectangle(hdc,pt1[cnt].x,pt1[cnt].y,pt2[cnt].x,pt2[cnt].y);
cnt++;
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-19
Title:- Write a window program to display a text at center of the client
area.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
RECT rect;
PAINTSTRUCT ps;
char str[50]="S";
static int x,y;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
x=(rect.right-rect.left)/2+rect.left;
y=(rect.bottom-rect.top)/2+rect.top;
TextOut(hdc,x,y,str,strlen(str));
EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-20
Title:- Write a program to display rectangle by pressing CONTROL + F1
keys
and ellipse by pressing SHIFT + F1 keys.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYDOWN:
hdc=GetDC(hwnd);
if(GetKeyState(VK_SHIFT)<0 && GetKeyState(VK_F1)<0)
{
Ellipse(hdc,300,300,100,200);
}
if(GetKeyState(VK_CONTROL)<0 && GetKeyState(VK_F1)<0)
{
Rectangle(hdc,100,100,200,200);
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

/************************************************************************
*********
Assignment No.:-21
Title:- Write a window program to draw two intersected regions with
different
colors, after performing combine operation the combined region
should
display in different color.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect1,rect2,rect3;
HBRUSH hbr;

rect1.left=100;
rect1.top =100;
rect1.right=300;
rect1.bottom =300;

rect2.left=200;
rect2.top =200;
rect2.right=400;
rect2.bottom =400;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
hbr=CreateSolidBrush(RGB(255,0,0));
FillRect(hdc,&rect1,hbr);

hbr=CreateSolidBrush(RGB(0,255,0));
FillRect(hdc,&rect2,hbr);

hbr=CreateSolidBrush(RGB(0,0,255));
IntersectRect(&rect3,&rect1,&rect2);
FillRect(hdc,&rect3,hbr);

EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-22
Title:-Write a window program to display following shapes in successive
mouse
clicks: Lines, Rectangle, Circle, Ellipse, Erase.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbr;
HRGN hrgn;
RECT rect;
static int cx,cy,cnt=0;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
cnt++;
hdc=GetDC(hwnd);
cx=LOWORD(lParam);
cy=HIWORD(lParam);
if(cnt==6)
cnt=1;
switch(cnt)
{
case 1:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx+100,cy+100);
break;
case 2:
Rectangle(hdc,cx,cy,cx+100,cy+200);
break;
case 3:
Ellipse(hdc,100,100,200,200);
break;
case 4:
Ellipse(hdc,200,200,150,300);
break;
case 5:
InvalidateRect(hwnd,NULL,true);
break;
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-23
Title:- Write a program to display your name with address on client area
using key messages.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static char ch[1];
static char str[100];

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYUP:
hdc=GetDC(hwnd);
// ch=(char)wParam;
sprintf(ch,"%c",wParam);
strcat(str,ch);
TextOut(hdc,10,50,str,strlen(str));
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-24
Title:-Write a window program that display a rectangle with when the user
clicks inside the rectangle should display two crossed lines as
diagonal
of the rectangle. And should erase when clicked outside.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
POINT pt;
static int cx,cy,
flag=0;

rect.left=100;
rect.top=100;
rect.right=200;
rect.bottom=200;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
if(flag==0)
{

Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
flag=1;
}
else
{
cx=LOWORD(lParam);
cy=HIWORD(lParam);
pt.x=cx;
pt.y=cy;

if(PtInRect(&rect,pt))
{
MoveToEx(hdc,rect.top,rect.left,0);
LineTo(hdc,rect.right,rect.bottom);

MoveToEx(hdc,rect.top,rect.right,0);
LineTo(hdc,rect.bottom,rect.left);
}
else
{
InvalidateRect(hwnd,&rect,true);
flag=0;
}
}
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-25
Title:- Write a window program to draw rectangle by pressing left mouse
button
and the color of rectangle should change by pressing right
mouse button
inside rectangle.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HBRUSH hbr;
PAINTSTRUCT ps;
RECT rect;
POINT pt;
static int cx,cy;

rect.left=100;
rect.top=100;
rect.right=300;
rect.bottom=300;

switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);

Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
ReleaseDC(hwnd,hdc);
return 0;

case WM_RBUTTONDOWN:
hdc=GetDC(hwnd);
cx=LOWORD(lParam);
cy=HIWORD(lParam);
pt.x=cx;
pt.y=cy;

if(PtInRect(&rect,pt))
{

hbr=CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
FillRect(hdc,&rect,hbr);
}
ReleaseDC(hwnd,hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-26
Title:- Write a window program to display string with different width on
each mouse
click. The string should be displayed at the point where the
user clicks.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HFONT hf;
static int x,y;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
x=LOWORD(lParam);
y=HIWORD(lParam);
hf=CreateFont(rand()%50,0,0,0,0,700,0,0,0,0,0,0,0,"Times
New Roman");
SelectObject(hdc,hf);
//SetBkColor(hdc,RGB(rand()%255,rand()%255,rand()%255));
SetTextColor(hdc,RGB(rand()%255,rand()%255,rand()%255));
TextOut(hdc,x,y,"SANJAY",6);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-27
Title:- Write a Window program to display random string 20 times with
random
colors at random position.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HFONT hf;
static int x,y,i;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<20;i++)
{

hf=CreateFont(rand()%50,0,0,0,0,700,0,0,0,0,0,0,0,"Times New
Roman");
SelectObject(hdc,hf);

SetBkColor(hdc,RGB(rand()%255,rand()%255,rand()%255));

SetTextColor(hdc,RGB(rand()%255,rand()%255,rand()%255));
TextOut(hdc,rand()%600,rand()%460,"SANJAY",6);
}
EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-28
Title:- Write a Window program to draw line between two points , the end
of
first line should be the first point of next line.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int x1,y1,x2,y2;
static int flag=1;

switch(iMsg)
{
case WM_CREATE:
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_LBUTTONDOWN:
hdc=GetDC(hwnd);
if(flag==1)
{
x1=LOWORD(lParam);
y1=HIWORD(lParam);
flag=0;
}
else
{
x2=LOWORD(lParam);
y2=HIWORD(lParam);
MoveToEx(hdc,x1,y1,0);
LineTo(hdc,x2,y2);
ReleaseDC(hwnd,hdc);
x1=x2;
y1=y2;
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-29
Title:- Write a window program to draw rectangle using arrow keys.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int static cx=50,cy=50;
switch(iMsg)
{
case WM_CREATE:
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;

case WM_KEYDOWN:
hdc=GetDC(hwnd);
switch(wParam)
{
case VK_DOWN:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx,cy+5);
cy=cy+5;
break;

case VK_UP:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx,cy-5);
cy=cy-5;
break;

case VK_LEFT:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx-5,cy);
cx=cx-5;
break;

case VK_RIGHT:
MoveToEx(hdc,cx,cy,0);
LineTo(hdc,cx+5,cy);
cx=cx+5;
break;
}
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
/************************************************************************
*********
Assignment No.:-30
Title:- Write a window program to move the ball horizontally on client
area.
*************************************************************************
********/
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hbr;
static int cx,cy,x,y;
static bool flag=1;

switch(iMsg)
{
case WM_CREATE:
SetTimer(hwnd,1,500,NULL);
return 0;
case WM_PAINT:
y=cy/2;
hdc=BeginPaint(hwnd,&ps);
hbr=CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc,hbr);
Ellipse(hdc,x,y,x+20,y+20);
EndPaint(hwnd,&ps);
return 0;

case WM_TIMER :
if((x+30)<cx &&flag==1)
{
x=x+20;
}
else
{
x=x-10;
flag=0;
if(x==0)
flag=1;
}
InvalidateRect(hwnd,NULL,true);
return 0;

/*Use it for verticle ball moving


case WM_PAINT:
x=cx/2;
hdc=BeginPaint(hwnd,&ps);
hbr=CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc,hbr);
Ellipse(hdc,x,y,x+20,y+20);
EndPaint(hwnd,&ps);
return 0;

case WM_TIMER :
if((y+30)<cy &&flag==1)
{
y=y+20;
}
else
{
y=y-10;
flag=0;
if(y==0)
flag=1;
}
InvalidateRect(hwnd,NULL,true);
return 0;
*/
case WM_SIZE :
cx=LOWORD(lParam);
cy=HIWORD(lParam);
InvalidateRect(hwnd,NULL,true);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

Das könnte Ihnen auch gefallen