Analyze the hello program of winx

Source: Internet
Author: User
Overview

We have introduced the hello program for Windows SDK. The process is divided into three steps:

  1. Register the registerclass ). In addition, we explain in detail why we need a window class and registerclass.
  2. Create and display a window (createwindow and showwindow ).
  3. Messageloop ). That is, get the message, dispatch the message, and process the message.

Here, we need to combine the hello program of winx to repeat the entire process.

For comparison, I want to learn about the hello program of ATL/wtl. Here we provide several articlesAnalyze the hello program of ATL/wtlGood article:

  • Win32 vs. ATL Windows Programming
  • ATL interface class for wtl Development Guide for MFC programmers
Hello program of winx

 

# Define winx_use_appmodule
# Include <winx. h>

Class chellomainframe: Public winx: mainframe <chellomainframe>
{
Winx_class ("chellomainframe ");
Public:
Void onpaint (hwnd)
{
Winx: paintdc DC (hwnd );
DC. textout (1, 1, _ T ("Hello, winx! "));
}
};

Winx: cappmodule _ module;

Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
Cappmoduleinit module;

Chellomainframe: registerclass ();
Chellomainframe wndmain;
Wndmain. Create (null, _ T ("hello "));

Return Module. Run ();
}

 

Winx Programming Model 1. register the window class (registerclass)

In winx, registerclass needs to be called proactively, which saves the trouble of interpreting it like ATL/wtl for half a day :-)

Unlike all the known C ++ interface libraries (MFC, ATL/wtl, smartwin, wxWidgets, and so on, and even the SW system I wrote earlier), winx tends to tell users the registerclass concept. In addition, I wrote a special article "Windows explanation: Window interpretation" to explain the importance of related concepts. This is related to winx's visualization policy. We will detail this in "How winx achieves visual interface development.

The following macros are related to the registerclass of winx:

  • Winx_class/winx_class_ex
  • Winx_class_style
  • Winx_default_bkgnd/winx_default_color/winx_default_brush
  • Winx_default_cursor/winx_default_syscursor

They correspond to the members in the Windows window class (wndclassex) respectively:

  • Lpszclassname
  • Style
  • Hbrbackground
  • Hcursor

You are familiar with using winx_class to specify the name of the window class. The usage of other macros is exactly the same. For example, if the default mouse cursor is an arrow (idc_arrow), it is easy to use idc_ibeam like the Edit Control:

Class chellomainframe: Public winx: mainframe <chellomainframe>
{
Winx_default_syscursor (idc_ibeam );
...
};

 

2. initclass)

Winx introduces many small initialization classes. There are roughly:

  • Ccomappinit-COM Initialization class, that is, coinitialize/couninitialize pair.
  • Coleappinit-OLE initialization class, that is, oleinitialize/oleuninitialize pair.
  • Cdebugappinit-start Memory Leak debugging (Only debug version, release version is empty class ).
  • Ccommoduleinit-ccommodule init/term.
  • Cappmoduleinit-cappmodule init/term.
  • Gdiplusappinit-gdiplusstartup/gdiplusshutdown.

The initialization class code is simple, but the abstraction is just right. Before winx, I tried to wrap these initialization processes to prevent users from seeing them, but I had to give up.

 

3. messageloop)

Currently, winx does not provide its own message loop. We borrowed wtl'sCmessageloop: Run. You didn't see cmessageloop in the winx example because it was hidden by the cappmoduleinit class.

Class cappmoduleinit: Public wtl: cmessageloop
{
Public:
Cappmoduleinit (
_ Atl_objmap_entry * P = NULL,
Hinstance hinst = getthismodule (),
Const guid * plibid = NULL)
{
_ Module. INIT (p, hinst, plibid );
_ Module. addmessageloop (this );
}
~ Cappmoduleinit ()
{
_ Module. term ();
}
};

 

4. windowproc)

In a message loop, the message is finally sent to the window (windowproc) by windows. So where is the winx Window Process?

Template <class windowclass, class handleclass = defaultwindowhwhandle>
Class window
{
Public:
Static lresult callback windowproc (
Hwnd, uint message, wparam, lparam)
{
Windowclass * pwnd = (windowclass *) windowmap: getwindow (hwnd );
If (pwnd = NULL)
{
If (message! = Wm_nccreate)
Return pwnd-> internaldefault (hwnd, message, wparam, lparam );
Lpcreatestruct lpcs = (lpcreatestruct) lparam;
If (lpcs-> lpcreateparams ){
Pwnd = (windowclass *) lpcs-> lpcreateparams;
Lpcs-> lpcreateparams = NULL;
}
Else {
If (windowclass: stackwindowobject ){
Winx_assert ("windowclass: stackwindowobject-unexpected! ");
Return false;
}
Else {
Pwnd = winx_new (windowclass );
}
}
Windowmap: setwindow (hwnd, pwnd );
}
Return pwnd-> processmessage (hwnd, message, wparam, lparam );
}
};

There are several details to explain:

  • Windowmap: What is getwindow/setwindow? When introducing the "SW System window class", we mentioned:
    -MFC uses a global hashmap to map the window handle (hwnd) to the window object (pwnd.
    -The SW system uses window userdata to establish a ing between window handle (hwnd) and window object (pwnd.

    In winx, the policy for establishing the ing between the two is optional. In addition to the two, there is a third option:
    -Use setprop/getprop to create a ing. This is the default option for winx.
    -Other methods you implement.

    Let's briefly analyze the advantages and disadvantages of these methods.
    -The problem with creating a hashing through hashmap is how can this hashmap object be obtained in other DLL files? This leads to bugs or strongly coupled structures.
    -Create a ing through userdata In the window. The problem is, what if userdata is already in use? This leads to potential security risks.
    -Setprop/getprop is used to create a ing. The ing performance is slower than that of userdata, but it is extremely secure.
     

  • Windowclass: What is stackwindowobject? We know that objects (including c ++ window objects of course) can be created in two ways:
    -Created on the stack. It is created in the form of local automatic variables.
    -Created on heap. It is created through new/Delete.
    Winx allows you to select one of the window classes. We will describe the details in a specific article later.
     
  • Finally, the window message is processed by pwnd-> processmessage (hwnd, message, wparam, lparam. Processmessage finally distributes messages. This is the core of the winx message mechanism. We have carefully explained the above:
    -Winx message distribution mechanism
    -Winx message distribution mechanism (continued)
    -Winx message distribution mechanism (Continued 2)
    -Winx message dispatching mechanism (final)

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.