VB icon

Add controls to a CDialog dynamically

Email
Submitted on: 1/1/2015 1:32:00 AM
By: Found on the World Wide Web (from psc cd)  
Level: Not Given
User Rating: By 2 Users
Compatibility: C++ (general)
Views: 1642
 
     Add controls to a CDialog dynamically--instead of using a dialog resource. You can add controls to your dialog dynamically by using methods CWnd::Create() and CWnd::CreateEx() or overridables of CWnd::Create() in control window wrapper classes such as CEdit or CListbox, etc. DEMPSEY@DEMPSEY.COM
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Add controls to a CDialog dynamically
// Description:Add controls to a CDialog dynamically--instead of using a dialog resource. 
You can add controls to your dialog dynamically by using methods CWnd::Create() and CWnd::CreateEx() or overridables of CWnd::Create() in control window wrapper classes such as CEdit or CListbox, etc. 
DEMPSEY@DEMPSEY.COM
// By: Found on the World Wide Web (from psc cd)
//**************************************

. For example, to create CEdit control, you can do the following:
1) add a member variable m_ec_myedit to your dialog .h file; 
2) I assume that your dialog templete has some control with ID = IDC_ABOVE_DYNAMIC_EDIT, and you want your dynamically created edit control to have the same width and be placed under IDC_ABOVE_DYNAMIC_EDIT. Then add the following code under the call to CDialog::OnInitDialog() in your overriden OnInitDialog(): 
GetDlgItem(IDC_ABOVE_DYNAMIC_EDIT)->GetWindowRect(rect); 
ScreenToClient(rect); 
CRect rectNew(rect.left, rect.bottom+5, rect.right, rect.bottom+35); 
m_myEdit.CreateEx(WS_EX_CLIENTEDGE, "EDIT", NULL 
/*lpszWindowName*/, 
WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|WS_BORDER, rectNew.left, rectNew.top, 
rectNew.Width(), rectNew.Height(), this->GetSafeHwnd(), NULL, NULL); 
m_myEdit.ShowWindow(SW_SHOW); 
It's that simple. The only thing that differs for different control classes is window styles. Usually, you can find the most important of style and extended style constants in online help.


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.