Establishment of Win32 Multi-window

Source: Internet
Author: User

This paper mainly makes some changes to the Win32 program, so as to explore the relationship between window class and window. Let's talk about the relationship between the window and the class.

Any window must belong to a window class, and a window class can be shared, that is, multiple windows can belong to that window class. So the relationship between the window and the window class is one to the other, and the window class and window relationship is one-to-many.

Using VS to build a Win32 program, you will find that vs automatically helps you to write the framework, we create an additional window class WXCE2A, the original window class set up 2 windows, a new window class to create a window.
2Project3.cpp: Defines the entry point for the application.//#include "stdafx.h"#include "Win32Project3.h"#define max_loadstring//global variable:HINSTANCE HInst;//Current instanceTCHAR sztitle[max_loadstring];//title bar textTCHAR szwindowclass[max_loadstring];//Main window class name//The forward declaration of the function contained in this code module:HWND Hwnd,hwnd1,hwnd2;//tchar La=tchar ("Love");TCHAR Szname[]=text ("ClassName");//  ////////// /Second class name/////////// ATOM MyRegisterClass (hinstance hinstance); BOOL InitInstance (HINSTANCE,int); LRESULT CALLBACK WndProc1 (HWND, UINT, WPARAM, LPARAM);///  //**** window processing function for the second class * *LRESULT CALLBACK WndProc (hwnd, UINT, WPARAM, LPARAM); Int_ptr CALLBACK About (hwnd, uint, WPARAM, LPARAM);intApientry _tWinMain (_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in _ LPTSTR lpCmdLine, _in_intnCmdShow) {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine);//TODO: Place the code here. MSG msg; Haccel hacceltable;//Initialize global stringLoadString (HInstance, Ids_app_title, SzTitle, max_loadstring);    LoadString (HInstance, Idc_win32project3, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization:    if(! InitInstance (HINSTANCE, ncmdshow)) {returnFALSE; } hacceltable = Loadaccelerators (hinstance, Makeintresource (IDC_WIN32PROJECT3));//main message loop:     while(GetMessage (&msg, NULL,0,0))    {if(!            TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {translatemessage (&msg);        DispatchMessage (&MSG); }    }return(int) Msg.wparam;}////function: MyRegisterClass ()////Purpose: Register window class. //ATOM MyRegisterClass (hinstance hinstance) {wndclassex Wcex; Wcex.cbsize =sizeof(Wndclassex); Wcex.style = Cs_hredraw |    Cs_vredraw;    Wcex.lpfnwndproc = WndProc; Wcex.cbclsextra =0; Wcex.cbwndextra =0;    Wcex.hinstance = hinstance;    Wcex.hicon = LoadIcon (hinstance, Makeintresource (IDI_WIN32PROJECT3));    Wcex.hcursor = LoadCursor (NULL, Idc_arrow); Wcex.hbrbackground = (hbrush) (color_window+1);    Wcex.lpszmenuname = Makeintresource (IDC_WIN32PROJECT3);    Wcex.lpszclassname = Szwindowclass; WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (Idi_small));////////////////////////    //wcex2.cbsize = sizeof (wndclassex);Wndclassex wcex2a; Wcex2a.cbsize =sizeof(Wndclassex); Wcex2a.style = Cs_hredraw |    Cs_vredraw;    Wcex2a.lpfnwndproc = WndProc1; Wcex2a.cbclsextra =0; Wcex2a.cbwndextra =0;    Wcex2a.hinstance = hinstance;    Wcex2a.hicon = LoadIcon (hinstance, Makeintresource (IDI_WIN32PROJECT3));    Wcex2a.hcursor = LoadCursor (NULL, Idc_arrow); Wcex2a.hbrbackground = (hbrush) (color_window+4); ///////paint brushes are different hereWcex2a.lpszmenuname = Makeintresource (IDC_WIN32PROJECT3); Wcex2a.lpszclassname = szname; ////// //////// WCEX2A.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (Idi_small));if(! RegisterClassEx (&WCEX2A)) /////////If registration fails, a dialog box pops up{MessageBox (hwnd,l"Why?" ", L"", MB_OK); }return(RegisterClassEx (&wcex));//&&registerclassex (&wcex2a));}////function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////Notes:////In this function, we save the instance handle in the global variable and//Create and display the main program window. //BOOL InitInstance (HInstance hinstance,intnCmdShow) {HWND hwnd,hwnd1,hwnd2; HInst = hinstance;//Store The instance handle in a global variableHWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, NULL, HINSTANCE, NULL);///   the original windowHWnd1 = CreateWindow (szname, L"Different Classes", Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, NULL, HINSTANCE, NULL);//New class of WindowsHWnd2 = CreateWindow (Szwindowclass, L"Same Class", Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, NULL, HINSTANCE, NULL);///   Old Class No. 2nd window   if(!hwnd| |! HWND2)//||! HWND1){returnFALSE;   } ShowWindow (HWnd, ncmdshow);   UpdateWindow (HWND);   ShowWindow (HWnd1, ncmdshow);   UpdateWindow (HWND1);   ShowWindow (HWnd2, ncmdshow); UpdateWindow (HWND2);returnTRUE;}////function: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Processes the message of the main window. ////WM_COMMAND-processing Application Menu//WM_PAINT-Draw main window//Wm_destroy-sends an exit message and returns////LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {intWmid, Wmevent;    Paintstruct PS; HDC hdc;Switch(message) { CaseWm_command:wmid = LoWord (WParam); Wmevent = HiWord (WParam);//Analysis Menu selection:        Switch(Wmid) { CaseIdm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about); Break; CaseIdm_exit:destroywindow (HWND); Break;default:returnDefWindowProc (hWnd, message, WParam, LParam); } Break; CaseWM_PAINT:HDC = BeginPaint (hWnd, &ps);//TODO: Add any drawing code here ...EndPaint (HWnd, &ps); Break; CaseWm_destroy:postquitmessage (0); Break;default:returnDefWindowProc (hWnd, message, WParam, LParam); }return 0;}Message handler for the//"about" box. INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM);Switch(message) { CaseWm_initdialog:return(INT_PTR) TRUE; CaseWM_COMMAND:if(LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL) {EndDialog (hdlg, LoWord (WParam));return(INT_PTR)        TRUE; } Break; }return(INT_PTR) FALSE;} LRESULT CALLBACK WndProc1 (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {intWmid, Wmevent;    Paintstruct PS; HDC hdc;Switch(message) { CaseWm_command:wmid = LoWord (WParam); Wmevent = HiWord (WParam);//Analysis Menu selection:        Switch(Wmid) { CaseIdm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about); Break; CaseIdm_exit:destroywindow (HWND); Break;default:returnDefWindowProc (hWnd, message, WParam, LParam); } Break; CaseWM_PAINT:HDC = BeginPaint (hWnd, &ps);//TODO: Add any drawing code here ...EndPaint (HWnd, &ps); Break; CaseWm_destroy:////////   to make a difference, our team changed the news.        //postquitmessage (0);MessageBox (hwnd,l"I went first, not the same as you.", L"NOTE", MB_OK); Break;default:returnDefWindowProc (hWnd, message, WParam, LParam); }return 0;}

Run results

The following analysis
  1. There are altogether 3 windows running the program, for the simple reason, we call 3 times CreateWindow function, the corresponding window handle is hwnd,hwnd1,hwnd2. The first of the third is from the same window class, so the appearance is identical, only the title is different, they share the same message processing function, so close any window, the other one will be closed.
  2. And the second window is the use of the new class, we mainly change the color of the brush, so it runs black, we also made a little change to the window function of the second class, that is, the Wm_destroy message, we commented the postqiutmessage message, so when the second window is closed, There will be no WM_QIUT messages entering the message queue, so the message loop will not close and the entire WinMain will not end, so the other 2 windows are not closed.
  3. When we click the Close button in the upper-right corner, we first send the WM_CLOSE message, which calls the DestroyWindow function in this message processing, sends the WM_DESTROY message, and the message processing calls the Postqiutmessage (0) function to send wm_ Qiut the message to the message queue, GetMessage captures the Wm_qiut, jumps directly out of the message loop, and the application main function WinMain ends.
  4. Although 3 windows are from 2 classes, all of them belong to a main function WinMain, so once the WinMain is finished, all windows must be closed.
  5. MFC应用程序的完整退出过程:点击窗口右上角的关闭按钮,或选择【File/Close】,发出 WM_CLOSE消息。CMyFrameWnd 并没有设置WM_CLOSE 处理常式,于是交给预设之处理常式。预设函数对于WM_CLOSE 的处理方式是呼叫 ::DestroyWindow, 并因而发出WM_DESTROY。预设之WM_DESTROY 处理方式是呼叫::PostQuitMessage,因此发出WM_QUIT。CWinApp::Run 收到WM_QUIT 后会结束其内部之讯息回路, 然后呼叫ExitInstance,这是CWinApp 的一个虚拟函数。如果自己应用程序类CMyWinApp 改写了ExitInstance , 那么CWinApp::Run 所呼叫的就是CMyWinApp::ExitInstance,否则就是 CWinApp::ExitInstance。最后回到 AfxWinMain,执行 AfxWinTerm,结束程序。
  6. Wm_quit is the only message that enables GetMessage (&msg,null,0,0) to return false values.
  7. SendMessage: Sends a message to the specified window procedure, and returns until the window procedure processes the message.
  8. PostMessage: Puts the message in the message queue (related to the thread created by the specified window), without waiting for message processing, to return immediately. Cannot send Wm_quit message, this message can only be sent by the PostQuitMessage function.
  9. PostThreadMessage: Sends a message to the specified thread's message queue, without waiting for the thread to process the message and returns immediately. The message and window sent by this function are irrelevant. We just need to specify the thread ID to be OK, but to ensure that the thread has been created, it will fail.
  10. GetMessage: Takes a message from the calling thread's message queue, and when the second argument is NULL, it retrieves the following two messages: A, a message that belongs to any window of the calling thread, and B, a message that is posted by Postthreadmessag to the calling thread.
  11. PeekMessage: function with GetMessage. The difference is:
    GetMessage: Until a message that matches the filter condition is placed in the message queue to return. PeekMessage: Returns immediately regardless of whether the message is placed in a queue

Establishment of Win32 Multi-window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.