Registration window class, registration window

Source: Internet
Author: User

Registration window class, registration window
I. Differences between an MFC application and a win32 Application

  Win32 Programming: It is to call the API functions in Win32SDK for programming, register the window class, create a window, a real window, set a message loop ...... And so on. In the past, all Windows programs were developed in this way. Writing a program requires a lot of code.

  MFC LibraryLater, to facilitate programming staff development, Microsoft encapsulated the functions in Win32SDK in C/C ++, so it was the MFC Library (Architecture). The Development Program was simpler and faster than the previous Win32SDK method.

  Same: All called APIs are Win32SDK APIs.

  Different: The execution of the MFC program is slow, and it is not free to write the program with MFC

Win32SDK development is slow, and the code to be written is amazing

1. win32 Application Registration window class

Generally programming in Win32, the simple steps are as follows:
(1) design window
(2) Registration window class
(3) Create window
(4) display window
(5) Update window
(6) message loop (key)

When Visual Studio is used to create a win32 project, a registration window class is automatically generated. As follows:

 1 ATOM MyRegisterClass(HINSTANCE hInstance) 2 { 3     WNDCLASSEX wcex; 4  5     wcex.cbSize = sizeof(WNDCLASSEX);                    // UINT    cbSize  6  7     wcex.style          = CS_HREDRAW | CS_VREDRAW;       // UINT    style 8     wcex.lpfnWndProc    = WndProc;              // WNDPROC  lpfnWndProc 9     wcex.cbClsExtra     = 0;                  // int     cbClsExtra10     wcex.cbWndExtra     = 0;                  // int     cbWndExtra11     wcex.hInstance      = hInstance;             // HINSTANCE hInstance12     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT5));  // HICON hIcon13     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);  // HCURSOR  hCursor14     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);    // HBRUSH   hbrBackground15     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32PROJECT5); // LPCTSTR lpszMenuName16     wcex.lpszClassName  = szWindowClass;          // LPCTSTR   lpszClassName17     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));     // HICON hIconSm18 19     return RegisterClassEx(&wcex);20 }

In the WNDCLASSEX struct:
  CbSize: The number of bytes that indicate the size of the structure. This field is usually set in the form of sizeof (WNDCLASSEX ).

  Style: An integer that represents the window class style. It determines the appearance and internal features of the window.

  LpfnWndProc: Point to the window processing function (callback function ).

  CbClsExtra: Record the additional information of the window class. The initialization value is 0.

  CbWndExtra: Record the additional information of the window instance. The initial value is 0.

  HIcon: Store the handle of the icon of this type of window

  HCursor: Storage refers to the handle of the window cursor. This field must be a handle of the cursor resource.

  HbrBackground: The background brush of the window class, which is the background brush handle or the system color value. If the color value has been given, it must be converted to the following HBRUSH Value

  LpszMenuName: Stores a string pointer to the name of a specified class menu resource ending with null. The class menu resource name has been defined in the resource file.

  LpszClassName: Stores the pointer of a string ending with null, or an atomic element (ATOM ).

  HIConSm: Store the handle of small icons in the window.

 

2. MFC Application Registration window class

In MFC programming, the window design steps are omitted, and the standard window information is obtained directly through GetClassInfo.

In MFC, #32770 represents the standard window class.

BOOL CBBBApp::SetRegisterClass(){    WNDCLASS wndcls;    ZeroMemory(&wndcls, sizeof(WNDCLASS));   // start with NULL    HINSTANCE hInst;    hInst = AfxGetInstanceHandle();    ASSERT(hInst != 0);
GetClassInfo(hInst, _T("#32770"), &wndcls); wndcls.lpszClassName = _T("YourClassName"); if (FALSE == AfxRegisterClass(&wndcls)) { AfxThrowResourceException(); return FALSE; } return TRUE;}

1) AfxGetInstanceHandle

  Function: Get the current instance handle

  Prototype: Hinstance afxapi AfxGetInstanceHandle ( );

  Return Value: Return the handle of the current application instance. If it is called from the DLL connected to the USRDLL version of MFC, The HINSTANCE value representing the DLL is returned.

 

2) GetClassInfo

  Function: Get form class information

  Prototype:

BOOL GetClassInfo (
HINSTANCEHInstance,
LPCTSTRLpClassName,
LPWNDCLASSLpWndClass
);

  Parameters:HInstance, Create a class application instance

LpClassName, Class Name

LpWndClassPointer to the WNDCLASS struct

  Return Value: A non-zero success is returned, and a zero failure is returned.

 

3) AfxRegisterClass

  Function: Registration type

  Prototype: Bool afxapi AfxRegisterClass (WNDCLASS * lpWndClass );

  Parameters: Pointer to the WNDCLASS struct

  Return Value: If the class is successfully registered, zero is returned. Otherwise, the value is not zero.

 

4) AfxThrowResourceException

This function throws a resource exception. This function is usually called when Windows resources cannot be loaded.

 

3. Purpose of the registration window class

  

1) After registering the window class, the same class window will use a set of WindowProc. Unified Behavior

After we construct a window class structure, we need to add the class structure pointer to the system atom table, that is, SAT, so that the system can find the custom window class by searching this table.

2) After registering the window class, you can use the FindWindow function to locate the window class and obtain its handle. Then, you can send messages to the window.

    HWND hComm = ::FindWindow(_T("YourClassName"), NULL);    if (NULL == hComm)  return FALSE;
if (hComm && ::IsWindow(hComm)) { DWORD dwResult = 0; LRESULT lResult = ::SendMessageTimeout(hComm, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 500, &dwResult); }

1) FindWindow
  Function: This function obtains the handle of a top-level window. The class name and window name of the window match the given string.

  Prototype:

HWND FindWindow (LPCSTR lpClassName, // {that is, the Class Name of the window} LPCSTR lpWindowName // {that is, the title of the window} if this parameter is NULL, all window names are matched. ); Return Value: If the function is successfully executed, the return value is the handle of the window with the specified window class name or window name. If the function fails to be executed, the return value is NULL. 2) find?wex // find=wex has two more handle parameters than FindWindow:
Finddomainwex (
Parent: HWND; {the Parent window handle of the Child window to be searched}
Child: HWND; {sub-window handle}
ClassName: PChar ;{}
WindowName: PChar {}
): HWND;
{
If Parent is 0, the function uses the desktop window as the Parent window to find all child windows in the desktop window;
If it is HWND_MESSAGE, the function only looks for all message windows;
The child window must be a direct Child Window of the Parent window;
If Child is 0, the query starts from the first subwindow of Parent;
If Parent and Child are both 0, the function searches for all top-level windows and message windows.
}

2) SendMessageTimeout

  Function: This function sends the specified message to one or more windows.

  Prototype:

LRESULT SendMessageTimeout (HWND hwnd, // the handle of the window whose window program will receive the message.// If this parameter is HWND_BROADCAST, the message will be sent to all top-level windows in the system, including invalid or invisible non-self-owned windows.UINT Msg, // specify the message to be sentWPARAM wParam, // specify the specified information of the attached message.LPARAM IParam, // specify the specified information of the attached message.UINT fuFlags, // specifies how to send messages.// SMTO_ABORTIFHUNG: if the receiving process is in the "hung" status, it will be returned without waiting for the end of the timeout period.// SMTO_BLOCK: prevents the calling thread from processing any other requests until the function returns.  // SMTO_NORMAL: when the call thread waits for the function to return, other requests are not blocked.// SMTO_NOTIMEOUTIFNOTHUNG: Windows 95 and later: if the receiving thread is not suspended, no response is returned when the timeout period ends.UIUT uTimeout, // specify the duration in milliseconds for the timeout period. If GetLastError returns zero, the function times out.LPDWORD lpdwResultult // specify the message processing result, depending on the sent message);

  Return Value: If the function is successfully called, a non-zero value is returned. If the function fails or times out, zero is returned.

 

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.