Write down the pseudo code of the complete application (windowclass registration
ID: 3608350 • Letter: W
Question
Write down the pseudo code of the complete application (windowclass registration, creation and window procedure) that creates anoverlapped window. It has a menu associated with it. Menu has 3menu items named as “First”, “Second” and“Exit”.
When user clicks “First” menu item,“Second” menu item is enabled and “First”is disabled.
When it clicks “Second” then “First” isenabled and “Second” is disabled.
When it clicks “Exit” menu item then the applicationterminates.
Note: Do not write the resource statements for themenu.
Explanation / Answer
#include <windows.h>
#include "resource.h"
HMENU hMenu;
bool Shown;
bool RegisterAppWindow(HINSTANCE hInstance);
bool CreateAppWindow(HINSTANCE hInstance);
LRESULT CALLBACK MyWindowProc (HWND hWnd,UINT message,WPARAMwParam,LPARAM lParam);
void EnableShow(boolShowEnabled);
boolRegisterAppWindow(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style =0;
wc.lpfnWndProc=MyWindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName="MainWClass";
ATOM cAtom=RegisterClass(&wc);
if(!cAtom)
{
MessageBox(NULL, "Error ! Registering Class", "ERROR",MB_OK);
return 0;
}
return 1;
}
boolCreateAppWindow(HINSTANCE hInstance)
{
HWND hwndMain;
hwndMain = CreateWindow("MainWClass","MainWindow",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,50,400,400,(HWND)NULL,(HMENU)NULL,hInstance,NULL);
if(!hwndMain)
{
MessageBox(hwndMain, "Window Couldn't Created /* Youcan change this sentence*/", "ERROR", MB_OK);
return 0;
}
ShowWindow(hwndMain,SW_SHOWNORMAL);
UpdateWindow(hwndMain);
hMenu=GetMenu(hwndMain);
return 1; //ifevery thing is done successfully then return true or 1
}
int RunApplication()
{
MSG msg;
while(GetMessage (&msg, NULL, 0, 0) > 0)
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return(int)msg.wParam;
}
LRESULT CALLBACKMyWindowProc (HWND hWnd,UINT message,WPARAM wParam,LPARAMlParam)
{
switch(message)
{
case WM_COMMAND:
//IF EXIT MENU ITEM IS CLICKED THEN ENDAPPLICATION
if((HIWORD(wParam)==0)&&(LOWORD(wParam)==ID_FILE_EXIT))
PostQuitMessage(0);
//IFFIRST MENU ITEM IS CLICKED
elseif((HIWORD(wParam)==0)&&(LOWORD(wParam)==ID_FILE_FIRST))
{
EnableShow(false); //this will enableclear and disable show
}
//IFSECOND MENU ITEM IS CLICKED
elseif((HIWORD(wParam)==0)&&(LOWORD(wParam)==ID_FILE_SECOND))
{
EnableShow(true); //this will enableshow and disable clear
}
break;
case WM_DESTROY: PostQuitMessage(0); break;
default:return DefWindowProc(hWnd, message, wParam, lParam);
}
DefWindowProc(hWnd,message,wParam,lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCEhPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
if (RegisterAppWindow(hInstance)) //if window isregistered successfully
{
if (CreateAppWindow(hInstance)) //CreateAppWindowcreates registered window
RunApplication(); //if window iscreated successfully then perform
//messagehandling
}
return 0;
}
void EnableShow(bool ShowEnabled)
{
if (ShowEnabled)
{
EnableMenuItem(hMenu,ID_FILE_SECOND,MF_BYCOMMAND|MF_GRAYED);
EnableMenuItem(hMenu,ID_FILE_FIRST,MF_BYCOMMAND|MF_ENABLED);
}
else
{
//THIS WILL DISABLE SHOW MENU ITEM
EnableMenuItem(hMenu,ID_FILE_FIRST,MF_BYCOMMAND|MF_GRAYED);
//THIS WILLENABLE CLEAR MENU ITEM
EnableMenuItem(hMenu,ID_FILE_SECOND,MF_BYCOMMAND|MF_ENABLED);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.