?? winprog.html
字號(hào):
#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>
That is our header file. Here we just define all of the id's we are going to use
so that we can just include this in both the resource file and the source file.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center">
<tr>
<td nowrap>
<tt>
#include "section_1_5_1.h"<br>
<br>
ID_MENU MENU DISCARDABLE<br>
BEGIN<br>
POPUP "&File"<br>
BEGIN<br>
MENUITEM "&New", ID_FILE_NEW<br>
MENUITEM "&Open", ID_FILE_OPEN<br>
MENUITEM "&Save", ID_FILE_SAVE<br>
MENUITEM SEPARATOR<br>
MENUITEM "E&xit", ID_FILE_EXIT<br>
END<br>
POPUP "&Do"<br>
BEGIN<br>
MENUITEM "&Something", ID_DO_SOMETHING<br>
MENUITEM "Something &Else", ID_DO_SOMETHING_ELSE<br>
END<br>
POPUP "&Help"<br>
BEGIN<br>
MENUITEM "&About", ID_HELP_ABOUT<br>
END<br>
END
</tt>
</td>
</tr>
</table><br><br>
This is our resource file. Here we are derfining the layout of the menus and giving
them message id's. These message id's have to be defined in both the resource file
and the source file. This is why we use a header file to define them and then we
just include the header file.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center">
<tr>
<td nowrap>
<tt>
#include <windows.h><br>
#include "section_1_5_1.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 = "ID_MENU";<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>
switch(Message) {<br>
case WM_CLOSE:<br>
DestroyWindow(hwnd);<br>
break;<br>
case WM_DESTROY:<br>
PostQuitMessage(0);<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>
You see we changed the WndClass.lpszMenuName to what the name for our menu is.
Then we added WM_COMMAND. This is the message that is posted when the user clicks
a menu item. The ID of the item is sent with the message. We use LOWORD() to get
the message from the lower owrd of the 32-bit. LOWORD() takes the following parameter:<br>
<br>
<dl>
<dt>dwValue</dt>
<dd>The value to get the low word from.</dd>
</dl>
The next part will look to see what to do from the cases. If the user selects Exit
it tells the program to do a PostQuitMessage(). None of this should be new. We already
went over message boxes and the PostQuitMessage function.</p>
<font size="4"><b><a name="1_5_2">1.5.2 On The Spot</a></b></font><br>
<font size="2"><a href="http://blacksun.box.sk/aztek/winprog/examples/section_1_5_2.zip">Source</a> - <a href="http://blacksun.box.sk/aztek/winprog/images/section_1_5.gif">Screen Shot</a><br><br></font>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -