22nd: Inserting child windows into a window using code in Soui

Source: Internet
Author: User

Using Soui to develop client-side UI programs, it is also often recommended to use XML code to create Windows, which makes it easy to create windows, and when the window size changes, the internal sub-windows are more likely to co-change.

But recently there have been a number of users consulting how to use the code to create Soui sub-window, hereby unified solution here.

To answer this question, first understand the process of Soui window creation and layout.

Let's start with the swnd.cpp. The code that creates the Subwindow:

1 BOOL Swindow::createchildren (pugi::xml_node xmlNode)2     {3 Testmainthread ();4          for(Pugi::xml_node xmlchild=xmlnode.first_child (); xmlchild; xmlchild=xmlchild.next_sibling ())5         {6             if(Xmlchild.type ()! = pugi::node_element)Continue;7 8             if(_WCSICMP (Xmlchild.name (), klabelinclude) = =0)9{//support for include tags in window layoutsTenSstringw strsrc = s_cw2t (Xmlchild.attribute (L"src"). Value ()); One pugi::xml_document xmldoc; A sstringtlist Strlst; -  -                 if(2==Parseresid (strsrc,strlst)) the                 { -LOADXML (xmldoc,strlst[1],strlst[0]); -}Else -                 { +LOADXML (xmldoc,strlst[0],rt_layout); -                 } +                 if(xmldoc) A                 { at Createchildren (Xmldoc.child (Klabelinclude)); -}Else -                 { - Sassert (FALSE); -                 } -}Else if(!xmlchild.get_userdata ())//flag whether a node can be ignored by UserData in             { -                 Swindow *pchild = Sapplication::getsingleton (). Createwindowbyname (Xmlchild.name ()); to                 if (pchild) +                 { - Insertchild (pchild); the                     Pchild->initfromxml (xmlchild); *                 } $             }Panax Notoginseng         } -         returnTRUE; the}

The function's function is to create its child window for this from XML, with the main attention to the red part of the code.

The 30th line is a window object from the Soui window class factory According to the XML node name new.

The 33rd line inserts the created window into the child window link of this.

The function of line 34th is to initialize the child window properties from the XML node corresponding to the child window.

Many netizens think that as long as the completion of the above steps should be able to display the window, but the results are indeed disappointing.

Soui an important feature is the ability to automatically layout, the secret of this process lies in the Swindow::onrelayout method.

1     voidSwindow::onrelayout (ConstCRect &rcold,ConstCRect &rcnew)2     {3Swindow *pparent=GetParent ();4         if(pparent)5         {6Pparent->InvalidateRect (rcold);7Pparent->InvalidateRect (rcnew);8}Else9         {Ten InvalidateRect (M_rcwindow); One         } A  -Ssendmessage (wm_nccalcsize);//Calculate non-client area size -  the         updatechildrenposition ();   //Update child window position -  - CRect rcclient; -GetClientRect (&rcclient); +Ssendmessage (Wm_size,0, Makelparam (Rcclient.width (), Rcclient.height ())); -}

The Onrelayout method is entered when this window position is changed.

Note the function line 15th: updatechildrenposition (); The function of this call is to automatically lay out all the child controls of this as the layout properties defined in the control.

To implement the layout of a window, in addition to calling the parent window's Updatechildrenposition () method, you can, of course, modify the window position directly using the Swindow::move method.

See here people should already understand what is the reason.

Of course, the above method is the window creation and layout method used in Soui, and there is a place to be aware of the use of code to create controls in the application.

The key problem is that compiling in the Soui system uses the MT (static link) method to link the CRT by default.

When the Mt method compiles using the CRT to allocate memory, the memory is the module (DLL) of the property invocation, and the release of the memory must therefore be performed within that module.

If the user assigns a Window object directly using new , and inserts it into the Soui window list, the time when the window is released is in Swindow::onfinalrelease () (actually the base class tobjrefimpl2< Method of iobjref,swindow>).

The code snippet for Swindow is compiled in Soui.dll, so the code that performs memory deallocation by default is in Soui.dll, which causes the program to crash.

There are two ways to solve this problem:

For system controls, the user should use Sapplication::getsingleton (). Createwindowbyname (Xmlchild.name ()); To create a Window object.

For extended window classes that are derived from the user's own implementation and are not registered with the Soui window class factory, only the new method can be used to create the Window object. Note the base class of Swindow:tobjrefimpl2<iobjref,swindow>

1template<classT>2 classTobjrefimpl: PublicT3 {4  Public:5Tobjrefimpl (): M_cref (1)6     {7     }8 9     Virtual~Tobjrefimpl () {Ten     } One  A     //! Add Reference -     /*! -     */ the     Virtual LongAddRef () -     { -         returnInterlockedIncrement (&m_cref); -     } +  -     //! Release References +     /*! A     */ at     Virtual LongRelease () -     { -         LongLret = InterlockedDecrement (&m_cref); -         if(lret==0) -         { - onfinalrelease (); in         } -         returnLret; to     } +  -     //! Releasing objects the     /*! *     */ $     Virtual void onfinalrelease()Panax Notoginseng     { -Delete This; the     } + protected: A     volatileLONG m_cref; the }; +  -template<classTclassT2> $ classTOBJREFIMPL2: PublicTobjrefimpl<t> $ { -  Public: -     Virtual void onfinalrelease() the     { -Delete Static_cast<t2*> ( This);Wuyi     } the};

Note The onfinalrelease in the code, which is a virtual method. So for a window object created with new, simply copy a snippet of code in the window class as follows:

1 classMyctrl: PublicSwindow2 {3Soui_class_name (myctrl,l"Myctrl")4  Public:5 //...6 Virtual voidOnFinalRelease () {Delete This;}7 //...8};

Thank you for your support!

22nd: Inserting child windows into a window using code 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.