Sie sind auf Seite 1von 11

Windows Lecture Programs

After each of the following step run your program and explain the programs behavior and
the reasons.

1. Use the wizard to write an empty Win32 project named wind.

2. In the solution explorer, right click on “Source Files, ” choose “Add,” and “Add
New Item.

3. Create a C++ file names wind containing the following code:

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,


HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

return 0;
}

4. Add the following code.

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;

5. Declare a Window Class as follows:

char szWinClassName[] = "My Windows";


WNDCLASSEX wcl;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.hInstance = hInstance;
wcl.lpszClassName = szWinClassName;
wcl.lpfnWndProc = DefWindowProc;
wcl.style = 0;
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);

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
);

7. Display the Window


ShowWindow(hwnd1,SW_SHOW);

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:

LRESULT CALLBACK WndProc1(HWND hwnd, UINT message,


WPARAM wParam, LPARAM lParam)
{
char wndTitle [256];
switch (message) {
case WM_LBUTTONDOWN:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(hwnd, “WM_LUTTONDOWN", wndTitle,
MB_OK);
break;
case WM_KEYDOWN:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(HWND_DESKTOP, "WM_KEYDOWN", wndTitle,
MB_OK);
break;
case WM_CHAR:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(HWND_DESKTOP, "WM_CHAR", wndTitle ,
MB_OK);
break;
case WM_CLOSE:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(HWND_DESKTOP, "WM_CLOSE”, wndTitle,
MB_OK);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);

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.

16. Change the Child Windows style to

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.

18. Add WS_CLIPSIBLINGS to Child Windows Style

19. Remove all the code for creating and displaying all windows except "Top Level
1"

20. Add Menus to the program as follows:

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.

d. Replace the entire content of the file wind.rc with:

#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
}
}

e. Replace the entire content of the header file resourceh with:

#define IDM_ADD_CHILD 100


#define IDM_ADD_OWNED 101
#define IDM_ADD_OWNER 102
#define IDM_MINIMIZE_CHILDREN 103
#define IDM_RESTORE_CHILDREN 104
#define IDM_MINIMIZE_OWNED 105
#define IDM_RESTORE_OWNED 106
#define IDM_MINIMIZE_OWNER 107
#define IDM_ABOUT 108
#define IDM_ADD_TOPLEVEL 109

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

22. Insert #include "resource.h" into wind.cpp


23. Add the following function

void DoMenuAction(HWND hWnd, UINT menuID){

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;
}

24. Change the event handler for WM_COMMAND in WndProc1 to:


case WM_COMMAND:
GetWindowText(hwnd, wndTitle, 256);
MessageBox(HWND_DESKTOP, "WM_COMMAND",
wndTitle, MB_OK);
DoMenuAction(hwnd, LOWORD(wParam));
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

27. Add the following function:


void DisableMenuItems(HWND hwnd){
HMENU hMenu=GetMenu(hwnd);

EnableMenuItem(hMenu, IDM_MINIMIZE_CHILDREN, MF_GRAYED


| MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_RESTORE_CHILDREN, MF_GRAYED
| MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_MINIMIZE_OWNED, MF_GRAYED
| MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_RESTORE_OWNED, MF_GRAYED
| MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_MINIMIZE_OWNER, MF_GRAYED
| MF_BYCOMMAND);
}

28. Add the following line to WinMain after ShowWindow(hwnd1,SW_SHOW);


DisableMenuItems(hwnd1);

29. Insert #include <stdio.h> into wind.cpp


30. Insert the following declarations into DoMenuAction
HWND hChildWnd;
int noChildren=0;
HMENU hMenu;
char parentTitle[50];
char childTitle[50];

31. Move the declaration of szWinClassName to the global declaration area

32. Declare a global varaiable:


HINSTANCE hThisInstance;

33. Add the following line to main:


hThisInstance = hInstance;

34. In DoMenuAction, change the case for IDM_ADD_CHILD to

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;

35. Dcalre a global variable:


int noTopLevels=1;
In DoMenuAction, change the case for IDM_ADD_TOPLEVEL to:
case IDM_ADD_TOPLEVEL:
HWND hwnd1;
char title[50];
noTopLevels++;
sprintf(title, "Top Level %d", noTopLevels);
hwnd1= CreateWindow( szWinClassName,
title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
DisableMenuItems(hwnd1);
ShowWindow(hwnd1,SW_SHOW);
break;

36. In DoMenuAction, declare:


char ownerTitle[50];
and change the case for IDM_ADD_OWNED to:
case IDM_ADD_OWNED:
GetWindowText(hWnd, ownerTitle, 50);
sprintf(title, "MyOwner is: %s", ownerTitle);
hwnd1= CreateWindow( szWinClassName,
title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
hWnd,
NULL,
hThisInstance,
NULL
);
DisableMenuItems(hwnd1);
hMenu=GetMenu(hwnd1);
EnableMenuItem(hMenu, IDM_MINIMIZE_OWNER, MF_ENABLED
| MF_BYCOMMAND);
hMenu=GetMenu(hWnd);
EnableMenuItem(hMenu, IDM_MINIMIZE_OWNED, MF_ENABLED
| MF_BYCOMMAND);
EnableMenuItem(hMenu, IDM_RESTORE_OWNED, MF_ENABLED
| MF_BYCOMMAND);
ShowWindow(hwnd1, SW_SHOW);
break;

37. In DoMenuAction, change the case for IDM_RESTORE_CHILDREN to:

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!

38. In DoMenuAction, change the case for IDM_MINIMIZE_CHILDREN to:

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!

39. In DoMenuAction, change the case for IDM_MINIMIZE_CHILDREN to:


case IDM_MINIMIZE_CHILDREN:
HWND hNextChildWnd;
hChildWnd = GetWindow(hWnd, GW_CHILD);
while (hChildWnd) {
hNextChildWnd=GetWindow(hChildWnd, GW_HWNDNEXT);
ShowWindow(hChildWnd, SW_MINIMIZE);
hChildWnd=hNextChildWnd;
}
break;

40. In DoMenuAction, change the case for IDM_MINIMIZE_OWNER to:


case IDM_MINIMIZE_OWNER:
HWND hOwner;
hOwner=GetWindow(hWnd, GW_OWNER);
ShowWindow(hOwner, SW_MINIMIZE);
break;
41. In DoMenuAction, declare:
HWND hItr, hPrev;
and change the case for IDM_MINIMIZE_OWNED to:
case IDM_MINIMIZE_OWNED:
hItr = GetWindow(hWnd, GW_HWNDFIRST);
while(hItr) {
hPrev=hItr;
hItr=GetWindow(hItr, GW_HWNDNEXT);
if (GetWindow(hPrev, GW_OWNER) == hWnd)
ShowWindow(hPrev, SW_MINIMIZE);
}
break;
42. In DoMenuAction, change the case for IDM_RESTORE_OWNED to:
case IDM_RESTORE_OWNED:
hItr = GetWindow(hWnd, GW_HWNDFIRST);
while(hItr) {
hPrev=hItr;
hItr=GetWindow(hItr, GW_HWNDNEXT);
if (GetWindow(hPrev, GW_OWNER) == hWnd)
ShowWindow(hPrev, SW_RESTORE);
}
break;

Das könnte Ihnen auch gefallen