Duilib Tutorial-Automatic Layout 2

Source: Internet
Author: User

In the previous section, I briefly described the settings that the control freely moves with the parent layout. In this section, I'll cover a common scenario: embedding Windows.

In the project, we rarely write 100% of a software, especially interface-related, we will use a previously written window, or open source module on the web. To give a simple example, if you want to write a video player, about the video playback window, you do not need to use DUI to achieve, we can fully use the Open Source Library online, embedded in a play wnd (of course, some libraries also support the way of callbacks, Users can freely draw the callback image in their own window).

We need to change the size of the playback window instantly when the window size changes. You might say it's very simple, overload onsize directly, and then get the placeholder control (using the placeholder controls is the most correct choice, if you judge the left margin, right margin in the program, do not have UI, code separated) size, and then set. But when you actually use it, it's not that simple. Look at the code:

UIManager.cpp Line No. 750:

     Casewm_size: {if(M_pfocus! =NULL) {TeventuiEvent= {0 }; Event. Type =uievent_windowsize; Event. Psender =M_pfocus; Event. Dwtimestamp =:: GetTickCount (); M_pfocus->event (Event); }            if(M_proot! = NULL) m_proot->needupdate (); }        return true;

As we can see, the window size changes, root is just a simple needupdate, redraw only, its size is not set to the same size as the window.

In WinImplBase.cpp line No. 214:

LRESULT windowimplbase::onsize (UINT umsg, WPARAM WPARAM, LPARAM LPARAM, bool&bhandled) {SIZE Szroundcorner=M_paintmanager.getroundcorner ();#ifDefined (WIN32) &&!defined (UNDER_CE)if(!::isiconic (* This) && (szroundcorner.cx! =0|| Szroundcorner.cy! =0) {cduirect rcwnd; :: GetWindowRect (* This, &Rcwnd); Rcwnd.offset (-rcwnd.left,-rcwnd.top); Rcwnd.right++; rcwnd.bottom++; HRGN HRGN=:: CreateRoundRectRgn (Rcwnd.left, Rcwnd.top, Rcwnd.right, Rcwnd.bottom, szroundcorner.cx, szRoundCorner.cy); :: SetWindowRgn (* This, HRGN, TRUE);         ::D eleteobject (HRGN); }#endifbhandled=FALSE; return 0;}

And I didn't do anything.

So setting the window position inside the onsize will not achieve the effect.

So where does duilib set the root size? UIManager.cpp the No. 615 line, which is set in WM_PAINT.

M_proot->setpos (rcclient);

1.SetPos

When you see this, I think you already know the first method. That is, in the OnSize,

RECT RC;

GetClientRect (M_hwnd, &RC);

M_paintmanager.setpos (RC);

Const rect& Rc_pos = Targer_ui_->getpos ();

:: MoveWindow (Move_wnd, Rc_pos.left, Rc_pos.top, Rc_pos.right–rc_pos.left, Rc_pos.bottom–rc_pos.top, TRUE);

That is, we set the size of the initiative, root set the POS, it will also set the child control POS, see the source for details. So, we can get the correct location information.

But this is not the best way, the reason is very simple, onsize will be called frequently, especially when the program is initialized, the onsize is called n times, and is also called when minimized. And when you look at 1.SetPos , you also guessed that there would be a second way.

2. entrusted OnSize

Assuming our placeholder control is target_ui_, it has a delegate member variable: OnSize. Look directly at the code:

+= Makedelegate (This, &bool cyourwnd:: ontargetsizechanged (void* param) {  const rect& rc_pos = targer_ui_->GetPos ();:: MoveWindow (Move_wnd, Rc_pos.left, Rc_pos.top, RC _pos.right–rc_pos.left, Rc_pos.bottom–rc_pos.top, TRUE);}

So simple and so graceful code.

Note that the + =is used.

Here, we also see the author's own implementation of the delegation of the writing (I do not know whether to use the Open Source Library), the author's C + + Foundation is quite deep.

Look at the source code of Ccontrolui, you will find the following delegate object:

 Public :    Ceventsource OnInit;    Ceventsource OnDestroy;    Ceventsource OnSize;    Ceventsource OnEvent;    Ceventsource onnotify;

As the name implies, no need to repeat.

Here's the difference between the event and the notify.

Event is a message received by the control itself , such as the left mouse button press, bounce, double-click, etc., Duilib first to the control itself to send an event.

Notify notification, which is a notification message sent to WND , resembles the dialog box in MFC that receives the control's notify (including the button's click), which by default is received by the window and responds in the window's Notify function.

The process of duilib is to send an event to control first and then send a notification to WND.

OnNotify is quite useful because you can customize the response of each control without having to do a lot of if in the Wnd notify. else.. The

OnEvent is also very useful to see the situation used.

Duilib Tutorial-Automatic Layout 2

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.