Windows Programming Practicals


* Window Program to demonstrate line drawing with right mouse button. The color & width of line should change with every new line.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int f=0;
HPEN hp;
static int x1,y1;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_RBUTTONDOWN:
hdc=GetDC(hWnd);
x1=LOWORD(lParam);
y1=HIWORD(lParam);
f++;
if(f>6)
f=1;
switch(f)
{
case 1: hp=CreatePen(0,1,RGB(rand()%255,rand()%255,rand()%255));
break;
case 2: hp=CreatePen(0,2,RGB(rand()%255,rand()%255,rand()%255));
break;
case 3: hp=CreatePen(0,3,RGB(rand()%255,rand()%255,rand()%255));
break;
case 4: hp=CreatePen(0,4,RGB(rand()%255,rand()%255,rand()%255));
break;
case 5: hp=CreatePen(0,5,RGB(rand()%255,rand()%255,rand()%255));
break;
case 6: hp=CreatePen(0,6,RGB(rand()%255,rand()%255,rand()%255));
break;
}
ROLL NO:-24
2
SelectObject(hdc,hp);
MoveToEx(hdc,x1,y1,0);
LineTo(hdc,x1+100,y1+100);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
****OUTPUT****





Window 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 message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int n,i;
static RECT count[100];
static int x,y,x1,y1;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_LBUTTONDOWN:
hdc=GetDC(hWnd);
x=LOWORD(lParam);
y=HIWORD(lParam);
x1=x+100;
y1=y+100;
Rectangle(hdc,x,y,x1,y1);
count[n].left=x;
count[n].top=y;
count[n].right=x1;
count[n].bottom=y1;
n++;
break;
case WM_LBUTTONDBLCLK:
x=LOWORD(lParam);
y=HIWORD(lParam);
for(i=0;i<n;i++)
{
if(x>count[i].left && x<count[i].right && y<count[i].bottom &&y>count[i].top)
{
InvalidateRect(hWnd,&count[i],true);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
*****OUTPUT*****





Window Program to display size of window and no.of left button clicks, no of right button clicks and no. of double clicks. This data should be displayed on two separate lines. Size should be updated when user resizes the window object. 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int x,y,lb=0,rb=0,dlb=0,drb=0;
char buf[50];
static RECT r;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_SIZE:
ROLL NO:-24
6
x=LOWORD(lParam);
y=HIWORD(lParam);
break;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,50,350,buf,sprintf(buf,"Height of the
window:%d",y));
TextOut(hdc,50,300,buf,sprintf(buf,"Width of the
window:%d",x));
TextOut(hdc,50,50,buf,sprintf(buf,"No of left click:%d",lb));
TextOut(hdc,50,100,buf,sprintf(buf,"No of Right
click:%d",rb));
TextOut(hdc,50,150,buf,sprintf(buf,"No of left dbl
click:%d",dlb));
TextOut(hdc,50,200,buf,sprintf(buf,"No of Rignt dbl
click:%d",drb));
GetClientRect(hWnd,&r);
EndPaint(hWnd,&ps);
break;
case WM_LBUTTONDOWN:
hdc=GetDC(hWnd);
lb++;
InvalidateRect(hWnd,&r,true);
break;
case WM_RBUTTONDOWN:
hdc=GetDC(hWnd);
rb++;
InvalidateRect(hWnd,&r,true);
break;
case WM_LBUTTONDBLCLK:
hdc=GetDC(hWnd);
lb--;
dlb++;
InvalidateRect(hWnd,&r,true);
break;
case WM_RBUTTONDBLCLK:
hdc=GetDC(hWnd);
rb--;
drb++;
InvalidateRect(hWnd,&r,true);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
*****OUTPUT*****





Window Program to draw Sine Wave on client area.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
ROLL NO:-24
8
PAINTSTRUCT ps;
HDC hdc;
static int cx,cy;
int i;
POINT apt[1000];
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_SIZE:
cx=LOWORD(lParam);
cy=HIWORD(lParam);
break;
case WM_RBUTTONDOWN:
hdc=GetDC(hWnd);
MoveToEx(hdc,0,cy/2,0);
LineTo(hdc,cx,cy/2);
for(i=0;i<1000;i++)
{
apt[i].x=i*cx/1000;
apt[i].y=(int)(cy/2*(1-sin(TWOPI*i/1000)));
}
Polyline(hdc,apt,1000);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

****OUTPUT*****




Window program to create filled rectangles and circles on alternate left click. New figure should not erase the previous one.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HBRUSH hbr;
static int x1,y1,f;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_LBUTTONDOWN:
hdc=GetDC(hWnd);
x1=LOWORD(lParam);
y1=HIWORD(lParam);
hbr=CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
SelectObject(hdc,hbr);
switch(f)
{
case 0: Rectangle(hdc,x1,y1,x1+100,y1+100);
f++;
break;
case 1: Ellipse(hdc,x1,y1,x1+100,y1+100);
f=0;
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
*****OUTPUT*******



Window program to change background color of the window after every 2 seconds. And at each mouse click the fill pattern should change.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM
wParam, LPARAM lParam);
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
HBRUSH hbr;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
SetTimer(hWnd,1,1500,NULL);
break;
case WM_TIMER:
InvalidateRect(hWnd,NULL,true);
break;
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
hbr=CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255));
SelectObject(hdc,hbr);
FillRect(hdc,&rect,hbr);
ROLL NO:-24
13
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
****OUTPUT****


Window program to create a window object. Drag the left mouse button & display rectangle for which dragged line is a diagonal. Also demonstrate mouse capturing.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int x,y,x1,y1,f=0;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_LBUTTONDOWN:
if(f==0)
{
x=LOWORD(lParam);
y=HIWORD(lParam);
f=1;
}
break;
case WM_MOUSEMOVE:
if(f==1)
{
hdc=GetDC(hWnd);
x1=LOWORD(lParam);
y1=HIWORD(lParam);
Rectangle(hdc,x,y,x1,y1);
MoveToEx(hdc,x,y,0);
LineTo(hdc,x1,y1);
}
break;
case WM_LBUTTONUP:
f=0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//*****OUTPUT*******


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 Rectangle.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM
wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CHAR:
hdc=GetDC(hWnd);
switch(wParam)
{
case 'r': Rectangle(hdc,100,100,200,200);
break;
case 'e': Ellipse(hdc,20,20,50,50);
break;
case 'g': RoundRect(hdc,50,20,20,100,100,5);
break;
case 'l': MoveToEx(hdc,50,50,0);
LineTo(hdc,200,200);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
*******OUTPUT******

No comments:

Post a Comment

Crud Operation Without Database

Crud Operation Without Database (Using Grid View)    Design Coding- <%@ Page Language="C#" AutoEventWireup=&...