?? simple_window.html
字號:
<HTML><LINK HREF="style.css" REL="STYLESHEET" TYPE="text/css"><HEAD><TITLE>Tutorial: A Simple Window</TITLE></HEAD><BODY><FONT SIZE="-1">[ <A HREF="./index.html">contents</A>| <A HREF="http://www.winprog.org/">#winprog</A>]</FONT><HR><H1>A Simple Window</H1><P>Example: simple_window</P><IMG SRC="images/simple_window.gif" ALT="[images/simple_window.gif]" ALIGN="right">Sometimes people come on IRC and ask "How do I make a window?"...Well it's notentirely that simple I'm afraid. It's not difficult once you know what you'redoing but there are quite a few things you need to do to get a window to show up; Andthey're more than can be simply explained over a chat room, or a quick note.<P>I always liked to do things first and learn them later...so here is the code toa simple window which will be explained shortly.<PRE CLASS="LIST">#include <windows.h>const char g_szClassName[] = "myWindowClass";// Step 4: the Window ProcedureLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){ switch(msg) { case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam;}</PRE>For most part this is the simplest windows program you can write that actuallycreates a functional window, a mere 70 or so lines. If you got the first exampleto compile then this one should work with no problems.<H2>Step 1: Registering the Window Class</H2>A <I>Window Class</I> stores information about a type of window, including it's <I>Window Procedure</I> which controls the window, the small and large icons for the window, and the background color. This way, you can register a class once, and createas many windows as you want from it, without having to specify all those attributes overand over. Most of the attributes you set in the window class can be changed on a per-windowbasis if desired.<P>A Window Class has NOTHING to do with C++ classes.<PRE CLASS="SNIP">const char g_szClassName[] = "myWindowClass";</PRE>The variable above stores the name of our window class, we will use it shortly to register our window class with the system.<PRE CLASS="SNIP"> WNDCLASSEX wc;</PRE><PRE CLASS="SNIP"> wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; }</PRE><P>This is the code we use in <CODE>WinMain()</CODE> to register our window class. We fill out the members of a <CODE>WNDCLASSEX</CODE> structure and call <CODE>RegisterClassEx()</CODE>. <P>The members of the struct affect the window class as follows:<DL><DT><CODE>cbSize</CODE><DD>The size of the structure.<DT><CODE>style</CODE><DD>Class Styles (<CODE>CS_*</CODE>), not to be confused with Window Styles (<CODE>WS_*</CODE>) This can usually be set to <CODE>0</CODE>.<DT><CODE>lpfnWndProc</CODE><DD>Pointer to the window procedure for this window class.<DT><CODE>cbClsExtra</CODE><DD>Amount of extra data allocated for this class in memory. Usually <CODE>0</CODE>.<DT><CODE>cbWndExtra</CODE><DD>Amount of extra data allocated in memory <I>per window</I> of this type. Usually <CODE>0</CODE>.<DT><CODE>hInstance</CODE><DD>Handle to application instance (that we got in the first parameter of <CODE>WinMain()</CODE>).<DT><CODE>hIcon</CODE><DD>Large (usually 32x32) icon shown when the user presses Alt+Tab.<DT><CODE>hCursor</CODE><DD>Cursor that will be displayed over our window.<DT><CODE>hbrBackground</CODE><DD>Background <I>Brush</I> to set the color of our window.<DT><CODE>lpszMenuName</CODE><DD>Name of a menu resource to use for the windows with this class.<DT><CODE>lpszClassName</CODE><DD>Name to identify the class with.<DT><CODE>hIconSm</CODE><DD>Small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.</DL>Don't worry if that doesn't make much sense to you yet, the various parts thatcount will be explained more later. Another thing to remember is to not tryand remember this stuff. I rarely (never) memorize structs, or function parameters,this is a waste of effort and, more importantly, time. If you know the functions you needto call then it is a matter of seconds to look up the exact parameters in yourhelp files. If you don't have help files, get them. You are lost without.Eventually you will come to know the parameters to the functions you use most.<P>We then call <CODE>RegisterClassEx()</CODE> and check for failure, if it fails we pop up a messagewhich says so and abort the program by returning from the <CODE>WinMain()</CODE> function.<H2>Step 2: Creating the Window</H2><P>Once the class is registered, we can create a window with it. You should lookup the paramters for <CODE>CreateWindowEx()</CODE> (as you should ALWAYS do when using a new API call), but I'll explain them briefly here.<PRE CLASS="SNIP"> HWND hwnd;</PRE><PRE CLASS="SNIP"> hwnd = CreateWindowEx(
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -