Register window class

Source: Internet
Author: User

One, the difference between MFC application and Win32 application

  Win32 Programming : is called WIN32SDK in the API function to program, Register window class, build window, real window, set message loop ... And so on the way to build Windows programs, the previous Windows programs have developed this way, writing programs to write a lot of code.

  MFC Library : Later, in order to facilitate the development of programmers, the WIN32SDK function in the C/s + + package, so that is the MFC library (architecture), the development process than the previous WIN32SDK way more convenient and fast.

  same : The API that is called is WIN32SDK API

  different : MFC program is slow to execute, and use MFC to write program is not free

WIN32SDK is slow to develop and the code to write is amazing.

1.win32 Application Registration window class

Generally programmed under WIN32, the simple steps are:
(1) Design window class
(2) Registration window class
(3) Create window
(4) Display window
(5) Update window
(6) message loop (key)

When we create a Win32 project with Visual Studio, a registration window class is automatically generated. As follows:

1 ATOM MyRegisterClass (hinstance hinstance)2 {3 wndclassex Wcex;4 5Wcex.cbsize =sizeof(Wndclassex); // UINT cbsize  6 7Wcex.style = Cs_hredraw |Cs_vredraw; // UINT style  8Wcex.lpfnwndproc = WndProc; //WNDPROC Lpfnwndproc 9Wcex.cbclsextra =0; // int Cbclsextra  TenWcex.cbwndextra =0; // int Cbwndextra   OneWcex.hinstance =hinstance; // hinstance hinstance   AWcex.hicon =LoadIcon (hinstance, Makeintresource (IDI_WIN32PROJECT5)); // hicon hicon   -Wcex.hcursor =loadcursor (NULL, Idc_arrow); // hcursor hcursor   -Wcex.hbrbackground = (hbrush) (color_window+1); // hbrush hbrbackground   theWcex.lpszmenuname =Makeintresource (IDC_WIN32PROJECT5); // LPCTSTR lpszmenuname   -Wcex.lpszclassname =Szwindowclass; // LPCTSTR lpszclassname   -WCEX.HICONSM =LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); // hicon HICONSM   -  +     returnRegisterClassEx (&Wcex); -}

In the WNDCLASSEX structure:
  cbsize: Stores the number of bytes representing the size of the structure, usually in the form of sizeof (WNDCLASSEX)

  style : Stores an integer that represents the style of a window class, which determines the appearance and intrinsic characteristics of the window.

  Lpfnwndproc: points to the window handler function (callback function).

  Cbclsextra : Records additional information for the window class, initialized to 0.

  Cbwndextra : record additional information for the window instance, the system is initially 0

  Hicon : Handle to the icon that stores the class window

  hcursor: storage represents a handle to the window class cursor, which must be a handle to a cursor resource.

  hbrbackground: window class background brush, for background brush handle, can also be a system color value, if the color value has been given, then must be converted to the following hbrush value

  lpszmenuname: Stores a string pointer to the specified class menu resource name with a null end, and the Class menu resource name is already defined in the resource file.

  lpszClassName: Stores a pointer to a null-terminated string, or stores an atomic element (Atom).

  HICONSM: The handle that stores the small icon for the class window.

2. MFC Application Registration window class

MFC programming, eliminating the window design steps, directly through the GetClassInfo () to obtain the standard window information.

in MFC, #32770 represents the standard window class .

BOOL Cbbbapp::setregisterclass () {wndclass wndcls; ZeroMemory (&WNDCLS,sizeof(WNDCLASS));//start with NULLhinstance HInst; HInst= AfxGetInstanceHandle(); ASSERT (HInst!=0);
GetClassInfo(HInst, _t ("#32770"), &wndcls); Wndcls.lpszclassname= _t ("YourClassName"); if(FALSE = =AfxRegisterClass(&wndcls)) { afxthrowresourceexception(); returnFALSE; } returnTRUE;}

1)AfxGetInstanceHandle

function :   Gets the current instance handle

prototype :  hinstance AFXAPI afxgetinstancehandle ( );

return value :   returns the handle to the current instance of the application. Returns the HInstance value that represents the DLL if it is called from within a DLL that is connected to the Usrdll version of MFC

2) GetClassInfo

  function : Obtain form class information

  Prototypes :

BOOL GetClassInfo (
HINSTANCE hinstance,
LPCTSTR Lpclassname,
Lpwndclass Lpwndclass
);

  parameter :hinstance, creating an application instance of the class

lpclassname, class name

Lpwndclass, pointer to wndclass struct

  return value : Return not 0 successful, return 0 unsuccessful

3) AfxRegisterClass

  Role : Registration class

  prototype : BOOL AFXAPI afxregisterclass (wndclass* lpwndclass);

  parameters : pointer to wndclass struct body

  return value : Returns zero if the class successfully registers, otherwise nonzero.

4) Afxthrowresourceexception

This function throws a resource exception. This function is usually called when Windows resources are not loaded.

3. Purpose of registering window classes

  

1) Registration window class after the same type of windows are used a set of WindowProc. Have a unified behavior

After we construct a window class structure, we need to add this class structure pointer to the system Atom table, which is the SAT, so that it can find the user-defined window class by looking for this table

2) After registering a good window class, you can use the FindWindow function to find the window class and get its handle, and then you can send a message to that window.

    = :: FindWindow (Mvsock_classname, NULL);    if (NULL = = Hcomm) return FALSE;
if (Hcomm && :: IsWindow (Hcomm)) { 0; 0 0 &dwresult); }

1) FindWindow
   Function: The function obtains a handle to a top-level window that matches the given string with the class name and window name of the window.

  Prototypes :

HWND FindWindow (LPCSTR lpclassname,//{That is, the window's class name}LPCSTR Lpwindowname//{That is, the title of the window} if this parameter is null, all window names are matched. ); return value: If the function executes successfully, the return value is the handle to the window that owns the specified window class name or window name. If the function execution fails, the return value is NULL 2) FindWindowEx//findwindowex Two more handle parameters than FindWindow:
FindWindowEx (
Parent:hwnd;{To find the parent window handle of a child window}
Child:hwnd;{child window handle}
Classname:pchar;{}
Windowname:pchar{}
): HWND;
{
If the parent is 0, the function takes the desktop window as the parent window and finds all the child windows of the desktop window;
In the case of hwnd_message, the function only finds all message windows;
The child window must be a direct child window of the Parent window;
If child is 0, the lookup starts from the first subwindow of the Parent;
If both Parent and child are 0, the function finds all the top-level windows and the message window.
}

2) SendMessageTimeout

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

  Prototypes :

LRESULT SendMessageTimeout (HWND hwnd,//The handle of the window whose window program will receive the message. //If this parameter is Hwnd_broadcast, the message is sent to all top-level windows in the system, including invalid or invisible windows that are not owned by themselves. UINT MSG,//Specifies the message being sentWPARAM wparam,//Specifies additional message-specific information. LPARAM Iparam,//Specifies additional message specified information. UINT Fuflags,//Specifies how messages are sent. //Smto_abortifhung: If the receiving process is in the "Hung" state, do not wait for the timeout period to return.Smto_block: Prevents the calling thread from processing any other requests until the function returns.  Smto_normal: When the calling thread waits for a function to return, it is not blocked from processing other requests.Smto_notimeoutifnothung:windows 95 and later: If the receive thread is not suspended, it does not return when the timeout period ends.Uiut Utimeout,//Specifies the duration in milliseconds for the time-out period. If GetLastError returns zero, it indicates that the function timed outLpdword Lpdwresultult//Specifies the result of message processing, depending on the message being sent);

  return value : If the function call succeeds, it returns a value other than 0 if the failure or timeout returns zero.

Register window class

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.