📄 winprog.html
字号:
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>
default:<br>
return DefWindowProc(hwnd, Message, wParam, lParam);<br>
}<br>
return 0;<br>
}
</tt>
</td>
</tr>
</table><br><br>
<p>As you can see its a good 70 or so lines to make a simple window.<br>
<br>
Windows Programs unlike DOS or UNIX programs are event driven. Windows programs
generaly stay idle untill they recive a message, they act on that message, and then
wait for the next one. When a windows program does respond to a message its called
handling the message.<br>
<br>
As you see I decalred my WiinProc function so that it can be used later on. I decalerd
my variables (I prfer to use Hungarian Notation, but you can use what you want).<br>
<br>
Inside the function WiNmain() we see some new variables. These are WndClass, hwnd,
and msg. The WndClass is the structure that holds all of the widnows class information
which is later passed to RegisterClassEx(). The hwnd structure identifies the window.
The msg structure contains message information from a thread's message queue. The
next thing we come to is when the hInstance is made global in the variable ghInstance.
After that we come to the defining of each of the WndClass settings:<br>
<br>
<dl>
<dt>cbSize</dt>
<dd>Is the size (in bytes) of this structure. Always set this to sizeof(WNDCLASSEX).</dd>
<dt>style</dt>
<dd>The Class style sof the window. Usually net to NULL.</dd>
<dt>lpfnWndProc</dt>
<dd>This points to the windows callback procedure.</dd>
<dt>cbClsExtra</dt>
<dd>Amount of extra data allocated for this class in memory. Usually 0.</dd>
<dt>cbWndExtra</dt>
<dd>Amount of extra data allocated in memory per window. Usually 0.</dd>
<dt>hInstance</dt>
<dd>This is the handle for the window instance.</dd>
<dt>hIcon</dt>
<dd>The icon shown when the user presses Alt-Tab. We will worry about this more when we get into resources.</dd>
<dt>hCursor</dt>
<dd>The cursor that will be displayed when the mouse is over our window. We will worry about this more when we get into resources.</dd>
<dt>hbrBackground</dt>
<dd>The brush to set the color of our window.</dd>
<dt>lpszMenuName</dt>
<dd>Name of the menu resource to use. We will worry about this more when we get into resources.</dd>
<dt>lpszClassName</dt>
<dd>Name to identify the class with.</dd>
<dt>hIconSm</dt>
<dd>The small icon shown in the taskbar and in the top-left corner. We will worry about this more when we get into resources.</dd>
</dl>
After that the next thing we do is register our window class. The program registers
it and if it tell the user in an message box. Then we have the defineation of hwnd.
This is what we will later pass on to ShowWindow(). Here are what parameters CreateWindowEx()
takes:<br>
<br>
<dl>
<dt>dwExStyle</dt>
<dd>This tells it what style of window we want (In the example I used a plain static window)</dd>
<dt>lpClassName</dt>
<dd>This points to our class name we came up with earlier.</dd>
<dt>lpWindowName</dt>
<dd>This is the text that will apear in the title bar of out window.</dd>
<dt>dwStyle</dt>
<dd>This tells tells what style of window we are creating.</dd>
<dt>x</dt>
<dd>The inital horizontal starting position of the window (Set this to CW_USEDEFAULT if you want windows to pick a place)</dd>
<dt>y</dt>
<dd>The inital verticle starting position of the window (Set this to CW_USEDEFAULT if you want windows to pick a place)</dd>
<dt>nWidth</dt>
<dd>The width of the window (In pixels)</dd>
<dt>nHeight</dt>
<dd>The heighth of the window (In pixels)</dd>
<dt>hWndParent</dt>
<dd>The handle of the parent window (If one does not exist this is NULL)</dd>
<dt>hMenu</dt>
<dd>I dentifies a menu for the window (Only appleys if its a child window)</dd>
<dt>hInstance</dt>
<dd>Points to the hInstance of the window.</dd>
<dt>lpParam</dt>
<dd>Points to a value passed to the window through the CREATESTRUCT structure (I have never found a use for this, but I am sure one exists)</dd>
</dl>
ShowWindow() sets the specified window's show state. UpdateWindow() updates the
client area of the specified window by sending a WM_PAINT message to the window.
The while loop will set our program to loop untill the WM_QUIT Message is recived.
TranslateMessage() translates virtual-key messages into character messages. DispatchMessage()
dispatches a message to a window procedure.<br>
<br>
Now we get to probably the most important part of the program. The CallBack Procedure.
What this is is a function with a giant switch() statment to switch off what each
message should do (Sometimes this is a bunch of if's). Our callback takes the following
parameters:<br>
<br>
<dl>
<dt>hwnd</dt>
<dd>Identifies the window.</dd>
<dt>uMsg</dt>
<dd>Specifies the message.</dd>
<dt>wParam</dt>
<dd>Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter. </dd>
<dt>lParam</dt>
<dd>Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter. </dd>
</dl>
In our switch statment we see the first case is WM_CLOSE. This message tells us
our use tried clicking the X in the corner or they press Alt-F4. Here we can prompt
them to save or any other action we need to do. Then we call DestroyWindow(hwnd).
Destroy Window takes one parameter:<br>
<br>
<dl>
<dt>hWnd</dt>
<dd>Identifies the window to be destroyed.</dd>
</dl>
DestroyWindow() will sent the WM_DESTROY Message to our program, here we call PostQuitMessage(0).
PostQuitMessage() takes one parameter:<br>
<br>
<dl>
<dt>nExitCode</dt>
<dd>Specifies an application exit code. This value is used as the wParam parameter of the WM_QUIT message.</dd>
</dl>
PostQuitMessage() Will then send out the message WM_QUIT, but we will never get
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -