I took a MFC, although the class of MFC encapsulated was very useful, but always felt that the understanding of MFC can only stay on the surface, it is difficult to further understand MFC. Key reasons are not enough for Windowsapi, now I plan to lose the MFC for a while, or write Windowapi to a convenience and deepen the impression.
Specially under the online master Houjie << Windows program design >>, intended to combine this book while learning the WINDOW basics, how to make multi-threads and DLL. But light reading is not enough, you must do it yourself when reading a book. Design points come out. So I intend to design a small software containing DLL, multi-threading, and other basic technologies. Now the basic idea is to engage in a class QQ communication software. Now it is just an idea, basic framework and function have not yet Designed, I have been designated in a few days. It is relatively difficult to use API, but slowly, I can't eat fat, time long should have no relationship. I believe I can do this software.
Quote the beginning of the teacher's book, it is also a paragraph
#include
Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {static TCHAR szAppName [] = TEXT ( "HelloWin"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (! RegisterClass (& wndclass)) {MessageBox ( "! This program requires Windows NT" NULL, TEXT (), szAppName, MB_ICONERROR); return 0; } Hwnd = createWindow (Szappname, // WINDOW Class Name Text ("The Hello Program"), // WINDOW CAPTION WS_OVERLAPPEDWI NDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (& msg, NULL, 0, 0)) {TranslateMessage (& msg); DispatchMessage (& msg);} return Msg.wparam;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) {case WM_CREATE: PlaySound (TEXT ( "hellowin.mp3"), NULL, SND_FILENAME | SND_ASYNC RETURN 0; CASE WM_PAINT: HDC = Beginpaint (HWND, & PS); getClientRect (hwnd, & rece); DrawText (HDC, Text ("Hello, Windows 2000!"), -1, & Rect, DT_SINGLINE | DT_CENTER | DT_VCENTER) Endpaint (hwnd, & ps); Return 0; Case WM_DESTROY: PostquitMessage (0); Return 0;} Return DEFWINDOWPROC (HWND, Message, WPARAM, LPARAM);}
/// ----------------------------------- ---
For instructions for no detail
The window is not said, I have seen it too familiar with this.
A little mistake to the handle will clarify
I was always unclear before.
HWND and Hinstance
Hinstance is the handle of the process itself, the cow's Windows core programming is the address of the process in the memory and hwnd is a handle pointing to a window.
Three uppercase identifiers for "handles" for different types:
Identifier Allance Hinstance Execute Entity (Program Self) Handle HWND Window Handle HDC Device Content Handle
The following is some of the list of Hungarian representations.
Prefix data type cchar or Wchar or TCHARBYBYTE NSHORTINTX, YINT is used as X coordinates and Y coordinate CX, and Cyint is used as X length and Y length, respectively; C represents "counter" B or FBOOL (int) F Represents "Flag" WWORD LLONG (Long Integer) DWDWORD FNFunction SString (String) SZ The string H ends with byte value 0 Handle P indicator
Call RegisterClass before the window is created
I didn't know very well for RegisterClass, just knowing this step.
MSDN:
The RegisterClass function registers a window class for subsequent use in calls to the tabindex = "0" keywords = "_ win32_CreateWindow_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowEx function (RegisterClass Register function as a window class behind CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns.: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowEx function to call) roughly means if you do not Register so CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns: mshelp = "http: // msdn .microsoft.com / mshelp "> CREATEWINDOWEX function can not create the window you want
Previously for CreateWindow or Tabindex = "0" keywords = "_ win32_createWindowex_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowex is also very familiar, here is not cumbersome.
ShowWindow's "Note" on MSDN is description
To perform certain special effects when showing or hiding a window, use tabindex = "0" keywords = "_ win32_AnimateWindow_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> AnimateWindow.
The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter.
As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in the structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show . the window On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application This behavior is designed for the following situations:. Applications create their main window by calling tabindex = " 0 "keywords =" _ win32_CreateWindow_cpp "xmlns: mshelp =". http://msdn.microsoft.com/mshelp "> CreateWindow with the WS_VISIBLE flag set Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow With the sw_show flag set to make it ..
The RegisterClass function registers a window class for subsequent use in calls to the tabindex = "0" keywords = "_ win32_CreateWindow_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowEx function (RegisterClass Register function as a window class behind CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns.: Mshelp = "http://msdn.microsoft.com/mshelp"> CREATEWINDOWEX function to call)
Register roughly meaning if not then CreateWindow or tabindex = "0" keywords = "_ win32_CreateWindowEx_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowEx function can not create a window before you want to CreateWindow Or TabINDEX = "0" keywords = "_ win32_createWindowex_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindowex is also very familiar, here is not cumbersome.
ShowWindow's "Note" on MSDN is description
To perform certain special effects when showing or hiding a window, use tabindex = "0" keywords = "_ win32_AnimateWindow_cpp" xmlns: mshelp = "http://msdn.microsoft.com/mshelp"> AnimateWindow.
The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter.
As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in the structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show .. the window On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application This behavior is designed for the following situations:
Applications create their main window by calling tabindex = "0" keywords = "_ win32_CreateWindow_cpp" xmlns:. Mshelp = "http://msdn.microsoft.com/mshelp"> CreateWindow with the WS_VISIBLE flag set Applications create their main window by calling CreateWindow With the ws_visible flag cleared, and later call showwindow with the sw_show flag set to make it it.. Here you want to mention UPDATEWINDOW:
The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty. The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If The Update Region IS Empty, No Message Is Sent.
When the window receives the WM_PAINT message, if the window's update area is not an empty window being updated, the WM_PAINT message is sent to the window of the process, put it into the message queue of the process. If the update area is empty, then there will be no news Send, that is, WM_Paint will not be as for message queue .1) WM_PAINT:
Previously, the concept of this WM_Paint was very vague, and now I feel a relatively awake understanding.
MSDN is this stated
The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window. The message is sent when the UpdateWindow or RedrawWindow function is called, or by the tabindex = "0" keywords = "_ win32_dispatchmessage"> DispatchMessage function when the application obtains a WM_PAINT message by using the tabindex = "0" keywords = "_ win32_getmessage"> GetMessage or tabindex = "0" keywords = "_ win32_peekmessage"> PeekMessage function.
When the system or application issues a window to redraw an application, WM_PAINT is called when the UpdateWindow or RedRawwindow function is called.
Then be captured by getMessage or TabINDEX = "0" keywords = "_ win32_peekmessage"> PeekMessage. Then be processed by DispatchMessage and send a message queue.
After calling UpdateWindow, the window appears on the video display. The program must now be prepared to read the user with the user's keyboard and the mouse input. Windows maintains a "message queue" for each Windows program currently executed. After the input event occurs, Windows converts the event into a "message" and puts the message into the program's message queue. While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DISPATCHMESSAGE (& MSG); What is the message that it is to accept the message? Still reference to a sentence: The WM_Paint Message is Sent When The System or Another Application Makes a Request to Paint A Portion of an Application's Window / / When the system or application issues a window to redraw an application, WM_Paint's message is issued next question. How do we know when to re-painch an app's window? This can be seen The first quarter of the output of the << Windows programming >> Output text. The last point does not understand is that the exit function wm_destroy message program responds to the WM_DESTROY message in the program's message queue by postquitMessage This function is inserted into a WM_QUIT in the program's message queue news. As mentioned earlier, GetMessage passed all messages removed from the message queue from WM_QUIT to all messages from the message queue. And when getMessage gets a WM_QUIT message, it passes back 0. This will cause the WinMain exit message loop to return 0 to think that WHILE (GetMessage (& MSG, NULL, 0, 0)) message loop will be stopped. The function call of this program:
Hellowin called at least 18 Windows functions. These functions are listed in the order they have appeared in hellowin and their own concise descriptions:
Loadicon load icon for use. LoadCursor loads the mouse cursor for the process. GetStockObject acquires a graphic object (in this example, it is a brush object to draw the window background). RegisterClass registers the window category for the program window. MessageBox Displays the message box. CREATEWINDOW establishes a window according to the window category. ShowWindow Displays the window on the screen. UpdateWindow Indicates Window Self-Update. GetMessage gets messages from the message queue. TranslateMessage translates some keyboard messages. DispatchMessage sends a message to the window message handler. Plays play a sound file. Beginpaint starts drawing the window. GetClientRect gets the size of the window display area. DrawText Displays a string. Endpaint ends the drawing window. PostquitMessage inserts a "Exit Program" message in the message queue. DEFWINDOWPROC performs a constant message processing.