How to create controls at runtime?
How to create dynamic MFC controls and handle message m开发者_如何学Goaps of the controls at runtime?
It really depends on which controls do you want to create, especially if you want to know which flags should you set. In general it goes down to this:
Normally a CWnd-derived control is created using Create
or CreateEx
. For a CButton, for instance:
CButton button;
button.Create("Button text", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | DT_CENTER, CRect(5, 5, 55, 19), this, nID);
where the CRect
specifies the button position, this
is a pointer to the parent window, and nID
is the control ID.
If the control doesn't come out as expected, it's probably because some flags are missing. I suggest you draw a sample control in design mode, check out the code for that control in the RC file, and copy the flags to the Create
caller.
As for the message maps, they are normally routed to the parent window. The nID
value you used in Create
is important here, because it will be the number that identifies the control in the message map. If you have a fixed number of controls, you can hard-code the nID
numbers for your controls (starting at 10000, for instance); if not, you'll have to provide a way for the parent window to identify them. Then you just 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 map to map a range of IDs to the same function.
精彩评论