?? winprog.html
字號(hào):
<p>Using resource menus are great if you have a menu that won't change. Some applications
although require menus that can be made on the spot. This might be used to add or
delete items from the menu or to make some items gray.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center">
<tr>
<td nowrap>
<tt>
#define ID_FILE_NEW 1000<br>
#define ID_FILE_OPEN 1001<br>
#define ID_FILE_SAVE 1002<br>
#define ID_FILE_EXIT 1003<br>
#define ID_DO_SOMETHING 1004<br>
#define ID_DO_SOMETHING_ELSE 1005<br>
#define ID_HELP_ABOUT 1006
</tt>
</td>
</tr>
</table><br><br>
Since we are making an identicle program we will use the same header file as above.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center" nowrap>
<tr>
<td nowrap>
<tt>
#include <windows.h><br>
#include "section_1_5_2.h"<br>
<br>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br>
<br>
static char gszClassName[] = "MyWindowClass";<br>
static HINSTANCE ghInstance = NULL;<br>
<br>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {<br>
WNDCLASSEX WndClass;<br>
HWND hwnd;<br>
MSG Msg;<br>
<br>
ghInstance = hInstance;<br>
<br>
WndClass.cbSize = sizeof(WNDCLASSEX);<br>
WndClass.style = NULL;<br>
WndClass.lpfnWndProc = WndProc;<br>
WndClass.cbClsExtra = 0;<br>
WndClass.cbWndExtra = 0;<br>
WndClass.hInstance = ghInstance;<br>
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br>
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);<br>
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);<br>
WndClass.lpszMenuName = NULL;<br>
WndClass.lpszClassName = gszClassName;<br>
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);<br>
<br>
if(!RegisterClassEx(&WndClass)) {<br>
MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);<br>
return 0;<br>
}<br>
<br>
hwnd = CreateWindowEx(<br>
WS_EX_STATICEDGE,<br>
gszClassName,<br>
"Windows Title",<br>
WS_OVERLAPPEDWINDOW,<br>
CW_USEDEFAULT, CW_USEDEFAULT,<br>
320, 240,<br>
NULL, NULL,<br>
ghInstance,<br>
NULL);<br>
<br>
if(hwnd == NULL) {<br>
MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);<br>
return 0;<br>
}<br>
<br>
ShowWindow(hwnd, nCmdShow);<br>
UpdateWindow(hwnd);<br>
<br>
while(GetMessage(&Msg, NULL, 0, 0)) {<br>
TranslateMessage(&Msg);<br>
DispatchMessage(&Msg);<br>
}<br>
return Msg.wParam;<br>
}<br>
<br>
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {<br>
HMENU hMenu, hSubMenu;<br>
<br>
switch(Message) {<br>
case WM_CLOSE:<br>
DestroyWindow(hwnd);<br>
break;<br>
case WM_DESTROY:<br>
PostQuitMessage(0);<br>
break;<br>
case WM_CREATE:<br>
hMenu = CreateMenu();<br>
<br>
hSubMenu = CreatePopupMenu();<br>
AppendMenu(hSubMenu, MF_STRING, ID_FILE_NEW, "&New");<br>
AppendMenu(hSubMenu, MF_STRING, ID_FILE_OPEN, "&Open");<br>
AppendMenu(hSubMenu, MF_STRING, ID_FILE_SAVE, "&Save");<br>
AppendMenu(hSubMenu, MF_SEPARATOR, 0, 0);<br>
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");<br>
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");<br>
<br>
hSubMenu = CreatePopupMenu();<br>
AppendMenu(hSubMenu, MF_STRING, ID_DO_SOMETHING, "&Something");<br>
AppendMenu(hSubMenu, MF_STRING, ID_DO_SOMETHING_ELSE, "Something &Else");<br>
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Do");<br>
<br>
hSubMenu = CreatePopupMenu();<br>
AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About");<br>
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");<br>
<br>
SetMenu(hwnd, hMenu);<br>
break;<br>
case WM_COMMAND:<br>
switch(LOWORD(wParam)) {<br>
case ID_FILE_NEW:<br>
MessageBox(hwnd, "New File", "Menu", 0);<br>
break;<br>
case ID_FILE_OPEN:<br>
MessageBox(hwnd, "Open File", "Menu", 0);<br>
break;<br>
case ID_FILE_SAVE:<br>
MessageBox(hwnd, "Save File", "Menu", 0);<br>
break;<br>
case ID_FILE_EXIT:<br>
PostQuitMessage(0);<br>
case ID_DO_SOMETHING:<br>
MessageBox(hwnd, "Do Something", "Menu", 0);<br>
break;<br>
case ID_DO_SOMETHING_ELSE:<br>
MessageBox(hwnd, "Do Something Else", "Menu", 0);<br>
break;<br>
case ID_HELP_ABOUT:<br>
MessageBox(hwnd, "Written By AZTEK", "About", 0);<br>
break;<br>
}<br>
break;<br>
default:<br>
return DefWindowProc(hwnd, Message, wParam, lParam);<br>
}<br>
return 0;<br>
}
</tt>
</td>
</tr>
</table><br><br>
At the top you may notice I set WndClass.lpszMenuName back to NULL. Thats because
we are making the menu on the spot and don't need the resource file. First you might
notice we added WM_CREATE. This is the message sent when the window is first created.
We also declared two varibales of the type HMENU. You see that we set hMenu to CreateMenu().
This will start the whole menu. Then you see we set hSubMenu to CreatePopupMenu().
This will make a blank individual sub menu. We need to fill it. You see we call
AppendMenu() to add items to our menus. AppendMenu() takes the following parameters:<br>
<br>
<dl>
<dt>hMenu</dt>
<dd>Identifies the menu bar.</dd>
<dt>uFlags</dt>
<dd>Specifies flags to control the appearance and behavior of the new menu item. This parameter can be a combination of values.</dd>
<dt>uIDNewItem</dt>
<dd>Specifies either the identifier of the new menu item or, if
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -