Wednesday, February 20, 2008

Sample code for menu in window

Type the code for handling the menu items by capturing their ID’s in WND PROC
function.


The below is the code which can be used to handle menu items by using their ID’s

#include "resource.h"
#include

long PASCAL WndProc(HWND,UINT,WPARAM,LPARAM);

int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hWnd;
WNDCLASS wc;
MSG msg;
MessageBox(0," Welcome to our Application","Application",MB_OK);

if(!hPrevInstance)
{
wc.style=NULL;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance =hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName="MyClass";


if(!RegisterClass(&wc))
{
MessageBox (0,"Could not register the WNDCLASS wc","Application",MB_OK);
return FALSE;
}

}

hWnd=CreateWindow("MyClass","Helloworld",WS_OVERLAPPEDWINDOW,10,10,1000,1000,NULL,NULL,hInstance,NULL);

if(!hWnd)
{
MessageBox (0,"could not create window","Application",MB_OK);
return FALSE;
}

ShowWindow(hWnd,nCmdShow);

while(GetMessage(&msg,0,0,0))
{
TranslateMessage (&msg);
DispatchMessage(&msg);
}
MessageBox(0,"Application terminating","Application",MB_OK);

return (msg.wParam);

}


long PASCAL WndProc(HWND hWnd ,UINT wMessage,WPARAM wParam,LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
switch(wMessage)
{
case WM_COMMAND:
switch(wParam)
{
case ID_MESSAGE:

MessageBox (hWnd,"Displaying a message","Messagebox",0);
break;

case ID_QUIT:
DestroyWindow(hWnd);
break;
}
break;

case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return defWindowProc(hWnd,wMessage,wParam,lParam);
}
return 0L;
}

No comments: