Beruflich Dokumente
Kultur Dokumente
After each of the following step run your program and explain the programs behavior and
the reasons.
2. In the solution explorer, right click on “Source Files, ” choose “Add,” and “Add
New Item.
#include <windows.h>
return 0;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
6. Create A window:
HWND hwnd1;
hwnd1= CreateWindow( szWinClassName,
"Top Level 1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
8. Add
if (!RegisterClassEx(&wcl)) return 0;
9. Add the following inside the main message loop immediately after
TranslateMessage(&msg):
switch (msg.message) {
case WM_LBUTTONDOWN:
MessageBox(HWND_DESKTOP, "Left Button Down",
"Main", MB_OK);
break;
case WM_CLOSE:
MessageBox(HWND_DESKTOP, "WM_CLOSE",
"Main", MB_OK);
break;
}
10. Change the Window Procedure of “Window Class 1” to WndProc1. Add the
following function:
11. Add the following to the switch statement inside the switch statement in
WndProc1
case WM_DESTROY:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(HWND_DESKTOP, "WM_DESTROY", wndTitle,
MB_OK);
PostQuitMessage(0);
break;
12. Add a WM_MOVE event handlers both to main message loop and WndProc1
13. Remove WM_MOVE event handlers. Create and display another window, titled
“Top Level 2”
14. Modify the program so that the window “Top Level 1”owns the window "Top
Level 2"
15. Modify the program so that "Top Level 1" has one child window: “Child 1 of Top
Level 1.” Create this child window with WS_CHILD style.
WS_CHILD|
WS_THICKFRAME | WS_CAPTION
|WS_SYSMENU |WS_MINIMIZEBOX | WS_MAXIMIZEBOX
17. Add another child window, Child 1 of Top Level 1.” to Top Level 1. Move one of
the child windows to overlap the other child window.
19. Remove all the code for creating and displaying all windows except "Top Level
1"
a. In Solution Explorer, right click on Resource Files and choose “Add new
Item.”
b. Choose “Resource File” Template, enter wind for the name of the file and
click open.
c. Right click on wind.rc and choose “Open With.” Then choose “Source
Code (Text) Editor” and click open. If you get a message box asking
whether you want to close an existing file answer yes.
#include "resource.h"
MyMenu1 Menu
{
POPUP "&Windows"
{
MENUITEM "Add a Child", IDM_ADD_CHILD
MENUITEM "Add a Top Level Window",
IDM_ADD_TOPLEVEL
MENUITEM "Add an owned Top Level window",
IDM_ADD_OWNED
MENUITEM "Add an owner window",
IDM_ADD_OWNER
POPUP "Children"
{
MENUITEM "Minimize All Children",
IDM_MINIMIZE_CHILDREN
MENUITEM "Restore All Children",
IDM_RESTORE_CHILDREN
}
MENUITEM "Minimize owned windows",
IDM_MINIMIZE_OWNED
MENUITEM "Restore owned windows",
IDM_RESTORE_OWNED
MENUITEM "Minimize owner window",
IDM_MINIMIZE_OWNER
}
POPUP "&Help"
{
MENUITEM "Abount Win", IDM_ABOUT
}
}
f. Make the menu declared in part b the menu for instances of "My
Windows" class by setting the class's menu name to MyMenu1
21. Add a WM_COMMAND event handler to both the main message loop and
WndProc1
switch (menuID) {
case IDM_ADD_CHILD:
MessageBox(HWND_DESKTOP, "IDM_ADD_CHILD",
"Menu Selection", MB_OK);
break;
case IDM_ADD_TOPLEVEL:
MessageBox(HWND_DESKTOP, "IDM_ADD_TOPLEVEL",
"Menu Selection", MB_OK);
break;
case IDM_ADD_OWNED:
MessageBox(HWND_DESKTOP, "IDM_ADD_OWNED",
"Menu Selection", MB_OK);
break;
case IDM_ADD_OWNER:
MessageBox(HWND_DESKTOP, "IDM_ADD_OWNER",
"Menu Selection", MB_OK);
break;
case IDM_MINIMIZE_CHILDREN:
MessageBox(HWND_DESKTOP, "IDM_MINIMIZE_CHILDREN",
"Menu Selection", MB_OK);
break;
case IDM_RESTORE_CHILDREN:
MessageBox(HWND_DESKTOP, "IDM_RESTORE_CHILDREN",
"Menu Selection", MB_OK);
break;
case IDM_MINIMIZE_OWNED:
MessageBox(HWND_DESKTOP, "IDM_ADD_MINIMIZE_OWNED",
"Menu Selection", MB_OK);
break;
case IDM_RESTORE_OWNED:
MessageBox(HWND_DESKTOP, "IDM_RESTORE_OWNED",
"Menu Selection", MB_OK);
break;
case IDM_MINIMIZE_OWNER:
MessageBox(HWND_DESKTOP, "IDM_MINIMIZE_OWNER",
"Menu Selection", MB_OK);
break;
case IDM_ABOUT:
char x[256];
GetModuleFileName(NULL, x, 200);
MessageBox(HWND_DESKTOP, x, "About", MB_OK);
MessageBox(HWND_DESKTOP, "Window Program\n Masoud Milani",
"About", MB_OK);
break;
}
25. Change the event handler for WM_COMMAND in the main message loop to:
case WM_COMMAND:
MessageBox(HWND_DESKTOP, "WM_COMMAND",
"MAIN", MB_OK);
DoMenuAction(msg.hwnd, LOWORD(msg.wParam));
break;
26. Remove the event handler for WM_COMMAND from the main message loop
case IDM_ADD_CHILD:
hChildWnd = GetWindow(hWnd, GW_CHILD);
while (hChildWnd) {
noChildren++;
hChildWnd=GetWindow(hChildWnd, GW_HWNDNEXT);
}
if (noChildren == 0) {
hMenu=GetMenu(hWnd);
EnableMenuItem(hMenu, IDM_MINIMIZE_CHILDREN,
MF_ENABLED | MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_RESTORE_CHILDREN,
MF_ENABLED | MF_BYCOMMAND);
}
noChildren++;
GetWindowText(hWnd, parentTitle, 50);
sprintf(childTitle, "Child %d of %s", noChildren, parentTitle);
hChildWnd= CreateWindow(szWinClassName,
childTitle,
WS_OVERLAPPED |
WS_CHILD | WS_CLIPSIBLINGS | WS_THICKFRAME |
WS_CAPTION
|WS_SYSMENU |WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
20,
20,
50,
80,
hWnd,
NULL,
hThisInstance,
NULL
);
ShowWindow(hChildWnd, SW_SHOW);
break;
case IDM_RESTORE_CHILDREN:
hChildWnd = GetWindow(hWnd, GW_CHILD);
while (hChildWnd) {
ShowWindow(hChildWnd, SW_RESTORE);
hChildWnd=GetWindow(hChildWnd,
GW_HWNDNEXT);
}
break;
** Make sure to add a few child windows and minimize them before your test this
case!
case IDM_MINIMIZE_CHILDREN:
hChildWnd = GetWindow(hWnd, GW_CHILD);
while (hChildWnd) {
ShowWindow(hChildWnd, SW_MINIMIZE);
hChildWnd=GetWindow(hChildWnd, GW_HWNDNEXT);
}
break;
** Make sure to add a few child windows before your test this case!