Write a simple program to create the Welcome window.

#include<windows.h> LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInsc,HANDLE hPreInsc,LPSTR lpszCmdLine,int ncmd) {HWND hwnd; MSG msg; hwnd=CreateWindow("BUTTON","WELCOME TO APPLICATION",BS_PUSHBUTTON|WS_OVERLAPPEDWINDOW,10,10,300,300,NULL, NULL,hInsc,NULL); ShowWindow(hwnd,ncmd); while(GetMessage(&msg,NULL,0,0)) { DispatchMessage(&msg); TranslateMessage(&msg); } return(0); }

1

OUTPUT:-

2

Write a program for graphical output of the screen.
#include<windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdline,int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wnd;

wnd.style=CS_HREDRAW|CS_VREDRAW; wnd.lpfnWndProc=WndProc; wnd.cbClsExtra=0; wnd.cbWndExtra=0; wnd.hInstance=hInstance; wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION); wnd.hCursor=LoadCursor(NULL,IDC_ARROW); wnd.hbrBackground=GetStockObject(WHITE_BRUSH); wnd.lpszMenuName=NULL; wnd.lpszClassName="MyClass"; RegisterClass(&wnd); hWnd=CreateWindow("MyClass","Device Context Example",WS_OVERLAPPEDWINDOW,10,10,200,150,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while(GetMessage(&msg,NULL,0,0)) {

3

TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

{ HDC hdc; PAINTSTRUCT ps; HPEN hpen,holdpen; HBRUSH hbrush,holdbrush; switch(msg) { case WM_PAINT: hdc = BeginPaint(hWnd,&ps); hpen = CreatePen(PS_SOLID,3,RGB(255,0,0)); hbrush= CreateSolidBrush(RGB(255,0,0)); holdpen = SelectObject(hdc,hpen); holdbrush=SelectObject(hdc,hbrush); Rectangle(hdc,10,10,80,60); Ellipse(hdc,100,30,180,90); hbrush=SelectObject(hdc,holdbrush); DeleteObject(hbrush); hpen=SelectObject(hdc,holdpen); DeleteObject(hpen);

4

EndPaint(hWnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); } return(0L);}

5

OUTPUT:-

6

Write a program to create a child window.
#include <windows.h> #include"child.h" #define Static1 "Simple Static Application"

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { static BOOL bFirstTime=TRUE; static HANDLE hStaticText,hStaticRect; HANDLE hInstance;

switch(msg) { case WM_CREATE: { if(bFirstTime) { bFirstTime=FALSE; hStaticText=CreateWindow("STATIC","THIS IS STATIC TEXT",WS_CHILD|SS_CENTER,10,10,130,60,hWnd,NULL,hInstance,NULL);

hStaticRect=CreateWindow("STATIC","",WS_CHILD|SS_GRAYRECT,10,80,230,20,h Wnd,NULL,hInstance,NULL); ShowWindow(hStaticText,SW_SHOWNORMAL); ShowWindow(hStaticRect,SW_SHOWNORMAL);

7

} }break;

case WM_COMMAND: { switch(wParam) case IDM_QUIT: DestroyWindow(hWnd); break; } break; case WM_DESTROY: { PostQuitMessage(0); break; } default:

return DefWindowProc(hWnd, msg, wParam, lParam); } return(0L); }

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance , LPSTR lpCmdLine, int Cmd) {

8

MSG msg;

HWND hWnd; WNDCLASS wClass; wClass.style =CS_HREDRAW|CS_VREDRAW;

wClass.lpfnWndProc =WindowProcedure; // callback-function; wClass.cbClsExtra wClass.cbWndExtra wClass.hInstance wClass.hIcon wClass.hCursor =0; =0; =hInstance; =LoadIcon(hInstance,IDI_APPLICATION); =LoadCursor(NULL,IDC_ARROW);

wClass.hbrBackground =(HBRUSH)(COLOR_WINDOW+3); wClass.lpszMenuName ="MYMENU"; wClass.lpszClassName ="my_class";

RegisterClass(&wClass);

hWnd = CreateWindow("my_class", "Static Control Example", WS_OVERLAPPEDWINDOW,

// class name; // window name; // window style;

CW_USEDEFAULT, CW_USEDEFAULT, // starting position (x,y); CW_USEDEFAULT, CW_USEDEFAULT, height; 9 // width and

NULL, NULL,

// parent handle; // menu handle;

hInstance,

// instance hand le; NULL); // other parameters;

ShowWindow (hWnd, Cmd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage (&msg); }

return msg.wParam; }

10

OUTPUT:-

11

Write a program to create menu without resource file.
#include<windows.h> #define IDM_BEEP 1 #define IDM_QUIT 2 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS wc; wc.style=CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=GetStockObject(WHITE_BRUSH); wc.cbClsExtra=0; wc.cbWndExtra=0; wc.lpszMenuName=NULL; wc.lpszClassName="myclass"; RegisterClass(&wc);

hwnd=CreateWindow("myclass","hello",WS_OVERLAPPEDWINDOW,30,30,200,200,NULL ,NULL,hInstance,NULL); ShowWindow(hwnd,nCmdShow); while(GetMessage(&msg,NULL,0,0)) 12

{

TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); } LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { HMENU hmenu; switch(msg) { case WM_CREATE: hmenu=CreateMenu(); AppendMenu(hmenu,MF_STRING,IDM_BEEP,"&Beep"); AppendMenu(hmenu,MF_STRING,IDM_QUIT,"&Quit"); SetMenu(hwnd,hmenu); break; case WM_COMMAND: switch(wParam) { case IDM_BEEP: MessageBeep(0); break; case IDM_QUIT: DestroyWindow(hwnd);

13

break; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return(0L); }

14

OUTPUT:-

15

WAP TO DEMONSTRATE THE CONCEPT OF LOGICAL FONT.
#include<windows.h> #define IDM_FONT 7 #define IDM_ANSI 1 #define IDM_OEM 2 #define IDM_ROMAN 3 #define IDM_QUIT 4 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow) { MSG msg; HWND hWnd; WNDCLASS wnd; wnd.style =CS_HREDRAW|CS_VREDRAW; wnd.lpfnWndProc =WndProc; wnd.cbClsExtra wnd.cbWndExtra wnd.hInstance wnd.hIcon wnd.hCursor =0; =0; =hInstance; =LoadIcon(hInstance,IDI_APPLICATION); =LoadCursor(NULL,IDC_ARROW);

wnd.hbrBackground =GetStockObject(WHITE_BRUSH); wnd.lpszMenuName =NULL; wnd.lpszClassName ="myclass"; 16

RegisterClass(&wnd); hWnd=CreateWindow("myclass","TYPE SOMETHING",

WS_OVERLAPPEDWINDOW,20,20,200,150,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); }

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { HMENU hMenu,hfont; HDC hDC; PAINTSTRUCT ps; static char cBuf[128]; static int nFontChoice=IDM_ROMAN; static int nCurPos=0; switch(msg) {case WM_CREATE: 17

hMenu=CreateMenu(); AppendMenu(hMenu,MF_STRING,IDM_ROMAN,"&Roman"); AppendMenu(hMenu,MF_STRING,IDM_ANSI,"&ANSI_VAR"); AppendMenu(hMenu,MF_STRING,IDM_OEM,"&OEM_FIXED"); AppendMenu(hMenu,MF_STRING,IDM_QUIT,"&Quit");

SetMenu(hWnd,hMenu); hfont=CreateFont(45,0,0,0,FW_NORMAL,1,0,0,ANSI_CHARSET,OUT_DEFAULT_P RECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH|FF_ROMAN, "Roman"); break; case WM_COMMAND: switch(wParam) { case IDM_ANSI: nFontChoice=IDM_ANSI; InvalidateRect(hWnd,NULL,TRUE); break; case IDM_OEM: nFontChoice=IDM_OEM; InvalidateRect(hWnd,NULL,TRUE); break; case IDM_ROMAN: nFontChoice=IDM_ROMAN; 18

InvalidateRect(hWnd,NULL,TRUE); break; case IDM_QUIT: DestroyWindow(hWnd); break; } break; case WM_PAINT: hDC=BeginPaint(hWnd,&ps); SetTextColor(hDC,RGB(0,0,255)); switch(nFontChoice) { case IDM_ANSI: SelectObject(hDC,GetStockObject(ANSI_VAR_FONT)); break; case IDM_OEM: SelectObject(hDC,GetStockObject(OEM_FIXED_FONT)); break; case IDM_ROMAN: SelectObject(hDC,hfont); break; } TextOut(hDC,0,0,cBuf,lstrlen(cBuf)); 19

EndPaint(hWnd,&ps); break; case WM_CHAR: if(nCurPos<127&&IsCharAlphaNumeric((char)wParam)) { cBuf[nCurPos++]=(char)wParam; cBuf[nCurPos]=0; InvalidateRect(hWnd,NULL,TRUE); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); } return(0L);

20

OUTPUT:-

21

WAP TO DEMONSTRATE POPUP WINDOW
#include<windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wnd; wnd.style=CS_HREDRAW|CS_VREDRAW; wnd.hInstance=hInstance; wnd.lpfnWndProc=WndProc; wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION); wnd.hCursor=LoadCursor(NULL,IDC_ARROW); wnd.hbrBackground=GetStockObject(WHITE_BRUSH); wnd.cbClsExtra=0; wnd.cbWndExtra=0; wnd.lpszMenuName=NULL; wnd.lpszClassName="myclass"; RegisterClass(&wnd);

hWnd=CreateWindow("myclass","Menu Example",WS_OVERLAPPEDWINDOW,30,30,200,200,NULL,NULL,hI nstance,NULL); ShowWindow(hWnd,nCmdShow);while(GetMessage(&msg,NULL,0,0)) 22

{ TranslateMessage(&msg);

DispatchMessage(&msg); } return(msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch(msg) { case WM_MOVE: MessageBox(hWnd,"WM_MOVE: The Window Moved","Move.....",MB_OK); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam,lParam); } return(0L); } 23

OUTPUT:-

24

WAP TO OUTPUT SOME TEXT ON SCREEN BY USING THE CONCEPT OF DC.
#include<windows.h> #define IDM_SHOW 1 #define IDM_QUIT 2 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdline,int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wnd; wnd.style=CS_HREDRAW|CS_VREDRAW; wnd.lpfnWndProc=WndProc; wnd.cbClsExtra=0; wnd.cbWndExtra=0; wnd.hInstance=hInstance; wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION); wnd.hCursor=LoadCursor(NULL,IDC_ARROW); wnd.hbrBackground=GetStockObject(WHITE_BRUSH); wnd.lpszMenuName=NULL; wnd.lpszClassName="MyClass"; RegisterClass(&wnd); 25

hWnd=CreateWindow("MyClass","Device Context Example",WS_OVERLAPPEDWINDOW,10,10,250,150,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while(GetMessage(&msg,NULL,0,0))

{ TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { HMENU hmenu; HDC hdc; switch(msg) { case WM_CREATE: hmenu= CreateMenu(); AppendMenu(hmenu,MF_STRING,IDM_SHOW,"&ShowText"); AppendMenu(hmenu,MF_STRING,IDM_QUIT,"&Quit"); SetMenu(hWnd,hmenu); break; 26

case WM_COMMAND: switch(wParam) {

case IDM_SHOW: hdc=GetDC(hWnd); TextOut(hdc,10,10,"Welcome to this Window Application",30);

ReleaseDC(hWnd,hdc); break; case IDM_QUIT: DestroyWindow(hWnd); break; }

break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); } return(0L);} 27

OUTPUT:-

28

WAP TO CHANGE THE CARET SHAPE & CURSOR SHAPE.
#include<windows.h> #define IDM_ARROW 1 #define IDM_CROSS 2 #define IDM_WAIT 3 # define IDM_QUIT 4 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain (HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wnd;

wnd.style=CS_HREDRAW|CS_VREDRAW; wnd.lpfnWndProc=WndProc; wnd.cbClsExtra=0; wnd.cbWndExtra=0; wnd.hInstance=hInstance; wnd.hIcon=LoadIcon(hInstance,"MyIcon"); wnd.hCursor=LoadCursor(NULL,IDC_ARROW); wnd.hbrBackground=GetStockObject(WHITE_BRUSH); wnd.lpszMenuName=NULL; wnd.lpszClassName="MyClass"; RegisterClass(&wnd); 29

hWnd=CreateWindow("MyClass","CHANGING MOUSE CURSOR SHAPE EXAMPLE",

WS_OVERLAPPEDWINDOW,10,10,400,400,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { static HCURSOR hcursor,hMenu; static int nxpos=0,nypos=0; switch (msg) { case WM_CREATE: hMenu=CreateMenu(); AppendMenu(hMenu,MF_STRING,IDM_ARROW,"&Arrow"); AppendMenu(hMenu,MF_STRING,IDM_CROSS,"&CROSS"); AppendMenu(hMenu,MF_STRING,IDM_WAIT,"&Wait"); AppendMenu(hMenu,MF_STRING,IDM_QUIT,"&Quit"); 30

SetMenu(hWnd,hMenu); hcursor=LoadCursor(NULL,IDC_ARROW); break; case WM_COMMAND: switch(wParam)

{ case IDM_ARROW: hcursor=LoadCursor(NULL,IDC_ARROW); break; case IDM_CROSS: hcursor=LoadCursor(NULL,IDC_CROSS);; break; case IDM_WAIT: hcursor=LoadCursor(NULL,IDC_WAIT); break; case IDM_QUIT: DestroyWindow(hWnd); break; } case WM_SETCURSOR: SetCursor(hcursor); break; case WM_SETFOCUS: 31

CreateCaret(hWnd,NULL,2,10); SetCaretPos(nxpos,nypos); ShowCaret(hWnd); break; case WM_LBUTTONDOWN: nxpos=LOWORD(lParam); nypos=HIWORD(lParam);

SetCaretPos(nxpos,nypos); break; case WM_KILLFOCUS: DestroyCaret(); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); } return(0L); }

32

OUTPUT:-

33

Write a Window Program to create a Notepad editor.
#include<windows.h> #define IDM_FILE 1 #define IDM_QUIT 5 #define IDM_EDIT 6 #define IDM_VIEW 10 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS wnd; wnd.style=CS_HREDRAW|CS_VREDRAW; wnd.hInstance=hInstance; wnd.lpfnWndProc=WndProc; wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION); wnd.hCursor=LoadCursor(NULL,IDC_ARROW); wnd.hbrBackground=GetStockObject(WHITE_BRUSH); wnd.cbClsExtra=0; wnd.cbWndExtra=0; wnd.lpszMenuName=NULL; wnd.lpszClassName="myclass"; RegisterClass(&wnd);

34

hWnd=CreateWindow("myclass","Notepad",WS_OVERLAPPEDWINDOW,30,30,200,200,N ULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return(msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { HMENU hmenu,hpopupmenu; switch(msg) { case WM_CREATE: hmenu= CreateMenu(); AppendMenu(hmenu,MF_POPUP,IDM_FILE,"&File"); AppendMenu(hmenu,MF_POPUP,IDM_EDIT,"&Edit"); AppendMenu(hmenu,MF_STRING,IDM_VIEW,"&View"); AppendMenu(hmenu,MF_STRING,IDM_QUIT,"&Quit"); SetMenu(hWnd,hmenu); break; case WM_COMMAND: 35

switch(wParam) { case IDM_QUIT: DestroyWindow(hWnd); break; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam,lParam); } return(0L); }

36

OUTPUT:-

37

Sign up to vote on this title
UsefulNot useful