Article 13th: Use a subwindow with a window handle in souI

Source: Internet
Author: User
Preface:

No matter how many DUI controls a directui system provides, you may need to place a subwindow with a window handle in some cases.

To distinguish it from a subwindow without a window handle, A subwindow with a window handle is called a real window.

Every interface created using souI is derived from shostwnd. Shostwnd itself is a real window with a window handle.

Therefore, like General Win32 programming, you can simply create various real child windows using shostwnd. m_hwnd as the parent window. Then, like Win32, the system manages the position and display of subwindows in response to messages such as resize.

Obviously, this processing will not be able to effectively use the powerful layout and subwindow management functions provided by souI.

To manage real Windows more effectively, a control is provided in the souI system: srealwnd.

Srealwnd is derived from swindow, so it can implement the same layout function as swindow and be managed by the souI system in various statuses such as size and visible.

To use sreawnd to manage subwindows, we need to first implement an interface: irealwndhandler

Irealwndhandler definition:
/*** @ Struct irealwndhandler * @ brief ** describe */struct irealwndhandler: Public iobjref {/*** srealwnd :: onrealwndcreate * @ brief window creation * @ Param srealwnd * prealwnd -- window pointer ** describe window creation */virtual hwnd onrealwndcreate (srealwnd * prealwnd) = NULL;/*** srealwnd :: onrealwnddestroy * @ brief destruction window * @ Param srealwnd * prealwnd -- window pointer ** describe destruction window */virtual void destroy (srealwnd * prealwnd) = NULL;/*** srealwnd :: onrealwndinit * @ brief initialization window * @ Param srealwnd * prealwnd -- window pointer * @ return bool -- false: handled by the system, true: user processing ** describe initialization window */virtual bool onrealwndinit (srealwnd * prealwnd) = NULL;/*** srealwnd :: onrealwndsize * @ brief adjust the window size * @ Param srealwnd * prealwnd -- window pointer * @ return bool -- false: handled by souI; true: move the user management window ** describe to adjust the window size */virtual bool onrealwndsize (srealwnd * prealwnd) = NULL ;};

Here we can see that there are a total of four interfaces, of which onrealwndinit is the onrealwndsize callback for true window initialization and location adjustment, which can be skipped, the other two interfaces are used to manage the creation and destruction of real windows. Therefore, they must be implemented.

Interface implementation example:

For details about how to use the real window, refer to the MFC. Demo under the samples directory in the souI code.

Here we post the code implementation:

Souirealwndhandler. h

# Pragma once # include <Unknown/obj-ref-impl.hpp> namespace souI {class csouirealwndhandler: Public tobjrefimpl2 <irealwndhandler, listener> {public: csouirealwndhandler (void );~ Csouirealwndhandler (void);/*** srealwnd :: onrealwndcreate * @ brief create a real window * @ Param srealwnd * prealwnd -- window pointer * @ return hwnd -- the created real window handle * describe */virtual hwnd onrealwndcreate (srealwnd * prealwnd ); /*** srealwnd: onrealwnddestroy * @ brief destruction window * @ Param srealwnd * prealwnd -- window pointer ** describe destruction window */virtual void onrealwnddestroy (srealwnd * prealwnd ); /*** srealwnd: onrealwndinit * @ brief Initialization Window * @ Param srealwnd * prealwnd -- window pointer ** describe initialization window */virtual bool onrealwndinit (srealwnd * prealwnd);/*** srealwnd :: onrealwndsize * @ brief adjust the window size * @ Param srealwnd * prealwnd -- window pointer * @ return bool -- true: Move the user management window; false: it is managed by souI. * Describe: Adjust the window size and obtain the window position from prealwnd. */Virtual bool onrealwndsize (srealwnd * prealwnd );};}

Souirealwndhandler. cpp:

# Include "stdafx. H" # include "souirealwndhandler. H" namespace souI {csouirealwndhandler: csouirealwndhandler (void) {} csouirealwndhandler ::~ Callback (void) {} hwnd csouirealwndhandler: onrealwndcreate (srealwnd * prealwnd) {const srealwndparam & Param = prealwnd-> getrealwndparam (); If (Param. m_strclassname = _ T ("button") {// only the button is created // an MFC cbutton object cbutton * pbtn = new cbutton is allocated; // The cbutton window is created, use prealwnd-> getcontainer ()-> gethosthwnd () as the parent window of cbutton // use prealwnd-> GETID () as the ID of the real window pbtn-> Create (Param. m_strwindowname, ws_child | WS _ Visible | bs_pushbutton,: crect (,), cwnd: fromhandle (prealwnd-> getcontainer ()-> gethosthwnd (), prealwnd-> GETID ()); // put the pointer of pbtn in the data of srealwnd to save the pbtn object in the destroy window. Prealwnd-> setdata (pbtn); // return the window handle after successful creation return pbtn-> m_hwnd;} else {return 0 ;}} void csouirealwndhandler :: onrealwnddestroy (srealwnd * prealwnd) {const srealwndparam & Param = prealwnd-> getrealwndparam (); If (Param. m_strclassname = _ T ("button") {// destroy the real window and release the memory occupied by the window cbutton * pbtn = (cbutton *) prealwnd-> getdata (); if (pbtn) {pbtn-> destroywindow (); Delete pbtn ;}}// if no processing is performed, false bool csouirealwndhandler: onrealwndsize (srealwnd * prealwnd) {return false;} is returned ;} // if this parameter is not processed, false bool csouirealwndhandler: onrealwndinit (srealwnd * prealwnd) {return false;} is returned ;}}

The code as a whole is very simple. With Comments, you should understand it at a glance.

Xml configuration:
<SOUI title="DUI-DEMO" width="600" height="400" appwin="0" ncRect="5,5,5,5"  resize="1" translucent="0">  <root skin="skin.bkframe" cache="1">    <caption pos="0,0,-0,29">      <text pos="11,9" >%title% ver:%ver%</text>          </caption>    <window pos="0,29,-0,-0">      <realwnd pos="10,10,-10,-10" name="mfcbtn" wndclass="button" id="100" wndname="MFC Button"/>    </window>  </root></SOUI>

In XML, we use a realwnd tag, which has an important attribute: wndclass, irealwndhandler, which determines what kind of real window should be created.

Running effect:

The buttons in the red box above are the MFC buttons created using the realwnd tag.

Message response in the real window:

Because a real window is a subwindow of the main souI window, messages of the real window can be processed in the message ing table of the main souI window (note: this is not the event ing table of the souI control ).

For example:

# Pragma onceclass crealwnddlg: Public souI: shostdialog {public: crealwnddlg (void );~ Crealwnddlg (void); // in response to the pressing message of MFC. Button, nid = 100 is the ID attribute of realwnd specified in XML. Void onbtnclick (uint unotifycode, int NID, hwnd wndctl) {If (unotifycode = bn_clicked & nid = 100) {souI: smessagebox (m_hwnd, _ T ("the real MFC button is clicked! "), _ T (" MFC. demo "), mb_ OK | mb_iconexclamation) ;}/// message ing table example (cmaindlg) msg_wm_command (onbtnclick) chain_msg_map (souI: shostdialog) Example () end_msg_map ()};
Conclusion:

Obviously, this method can also easily create various types of other windows.

After the window is created, the system automatically manages the window status.

Finally, remember that when a real window exists, the translucent = "1" attribute cannot be set in the souI main window. This is because any subwindow cannot be normally displayed in a translucent window. This entry also applies to Windows containing the IE control.

Article 13th: Use a subwindow with a window handle in souI

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.