How to create controls at runtime?

How to create dynamic MFC controls and process message cards of controls at runtime?

+6
controls runtime mfc
source share
1 answer

It really depends on which controls you want to create, especially if you want to know which flags you should set. In general, it boils down to the following:

Typically, a CWnd-derived control is created using Create or CreateEx . For CButton, for example:

 CButton button; button.Create("Button text", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | DT_CENTER, CRect(5, 5, 55, 19), this, nID); 

where CRect indicates the position of the button, this is a pointer to the parent window, and nID is the identifier of the control.

If the control does not exit as expected, perhaps because some flags are missing. I suggest you draw a sample control in design mode, check the code for this control in the RC file, and copy the flags to the calling Create .

As for message cards, they are usually routed to the parent window. The nID value that you used in Create is important here because it will be the number that identifies the control on the message map. If you have a fixed number of controls, you can hard-code the nID numbers for your controls (for example, starting at 10000); if not, you need to provide a way for the parent window to identify them. Then you simply add the message map entries.

 ON_BN_CLICKED(10000, OnBnClicked) ON_CONTROL_RANGE(BN_CLICKED, 10010, 10020, OnBtnsClicked) 

You can use the ON_CONTROL_RANGE message ON_CONTROL_RANGE to map a range of identifiers to the same function.

+13
source share

All Articles