DuiVision development tutorial (9)-dynamically create controls and duivision controls
In most cases, DuiVision-based interface programs define the interface through xml in advance, but sometimes you need to dynamically create controls through code. You can refer to the following example of dynamically creating controls:
CDuiButton * pToolBtn = static_cast <CDuiButton *> (DuiSystem: CreateControlByName (L "button", m_pDlg-> GetSafeHwnd (), m_pDuiObject); if (pToolBtn) {(CControlBase *) m_pDuiObject)-> AddControl (pToolBtn); pToolBtn-> SetName (L "btnName"); pToolBtn-> SetTitle (L "Dynamic Button "); pToolBtn-> SetPosStr (L "10, 10, 110,35"); pToolBtn-> OnAttributeSkin (L "skin: IDB_BT_ICON", FALSE); pToolBtn-> trim (L "skin: trim ", FALSE); pToolBtn-> SetMaxIndex (4); pToolBtn-> SetAction (L "dlg: dlg_login"); pToolBtn-> OnPositionChange ();}
This code first uses the CreateControlByName function provided by DuiSystem to dynamically create a control (the control type name must be specified during creation, for example, a button control is a button, a handle to the window, and a pointer to the parent control ).
After the widget is created, call its function to set some properties of the widget. You must pay special attention to the settings of the widget's location information, you must set the position string (corresponding to the pos attribute of the control), because the position string can calculate the actual position of the control based on the location information of the parent control, if you do not use this method to directly specify the position of the control, the actual position may not be calculated because there is no relevant information during Automatic Location calculation.
You need to call the SetPosStr function to set the position string. Note that the OnPositionChange function must be called at the end. This function will actually perform a location calculation so that the control can be displayed in the correct position.
The following code is an example of dynamically creating controls in the demo program:
// Demonstrate how to dynamically Add a child control to the div (to the edit box page of the basic control) CControlBase * pDiv = (CControlBase *) GetControl (_ T ("tab. control.4 "); if (pDiv) {CDuiEdit * pControlEdit = static_cast <CDuiEdit *> (DuiSystem: CreateControlByName (L" edit ", m_pDlg-> GetSafeHwnd (), pDiv); if (pControlEdit) {pDiv-> AddControl (pControlEdit); pControlEdit-> SetName (L "btnName "); pControlEdit-> SetTitle (L "dynamically added edit box control"); pControlEdit-> SetPosStr (L "50,220,250,250"); pControlEdit-> OnAttributeSkin (L "skin: IDB_EDIT ", FALSE); pControlEdit-> OnAttributeLeftImage (L "skin: IDB_COMPUTER", FALSE); pControlEdit-> OnPositionChange ();}}
DuiVision open source code (github): https://github.com/blueantst/DuiVision
Ant financial studio home page: http://www.blueantstudio.net
DuiVision QQ group: 325880743
Public Account: blueantstudio or search for blue ant Studio