
how to add button, edit control and spin button dynamically
Quote:
Eugene Ng wrote...
+AD4-Dear all :
+AD4- I'm trying to a dialog whihc allow me to button, spin and edit control
+AD4-dynamically when user choose Add control, can anybody tell me how to
+AD4-accomplish this or where can I find such kind of example. Thanks
+AD4-
Hi, here's an example code that will do the trick. It's all created within a
MFC project where I created a dialog box called 'DynsmicControlsDialog
(x-cuse the misspelling of Dynamic).
// DynsmicControlsDialog.h : header file
//
////////////////////////////////////////////////////////////////////////////
/
// CDynsmicControlsDialog dialog
// ID:s for dynamically created controls:
+ACM-define IDC+AF8-DYNAMIC+AF8-BUTTON 1001
+ACM-define IDC+AF8-BUDDY 1002
+ACM-define IDC+AF8-DYNAMIC+AF8-SPINBUTTON 1003
+ACM-define IDC+AF8-DYNAMIC+AF8-EDIT 1004
class CDynsmicControlsDialog : public CDialog
+AHs-
// Construction
public:
CDynsmicControlsDialog(CWnd+ACo- pParent +AD0- NULL)+ADs- // standard constructor
// Dialog Data
//+AHsAew-AFX+AF8-DATA(CDynsmicControlsDialog)
enum +AHs- IDD +AD0- IDD+AF8-DYNAMIC+AF8-CONTROLS+AF8-DIALOG +AH0AOw-
// NOTE: the ClassWizard will add data members here
//+AH0AfQ-AFX+AF8-DATA
// Overrides
// ClassWizard generated virtual function overrides
//+AHsAew-AFX+AF8-VIRTUAL(CDynsmicControlsDialog)
protected:
virtual void DoDataExchange(CDataExchange+ACo- pDX)+ADs- // DDX/DDV support
//+AH0AfQ-AFX+AF8-VIRTUAL
// Control pointers:
public:
CButton +ACo-m+AF8-pButton+ADs-
CEdit +ACo-m+AF8-pBuddy+ADs-
CSpinButtonCtrl +ACo-m+AF8-pSpinButton+ADs-
CEdit +ACo-m+AF8-pEdit+ADs-
// Implementation
protected:
// Note manually added handlers below:
// Generated message map functions
//+AHsAew-AFX+AF8-MSG(CDynsmicControlsDialog)
afx+AF8-msg void OnAddcontrol()+ADs-
afx+AF8-msg void OnMyDynamicButton()+ADs-
afx+AF8-msg void OnMyDynamicEdit()+ADs-
//+AH0AfQ-AFX+AF8-MSG
DECLARE+AF8-MESSAGE+AF8-MAP()
+AH0AOw-
// DynsmicControlsDialog.cpp : implementation file
//
+ACM-include +ACI-stdafx.h+ACI-
+ACM-include +ACI-DynamicDialogControls.h+ACI-
+ACM-include +ACI-DynsmicControlsDialog.h+ACI-
+ACM-ifdef +AF8-DEBUG
+ACM-define new DEBUG+AF8-NEW
+ACM-undef THIS+AF8-FILE
static char THIS+AF8-FILE+AFsAXQ- +AD0- +AF8AXw-FILE+AF8AXwA7-
+ACM-endif
////////////////////////////////////////////////////////////////////////////
/
// CDynsmicControlsDialog dialog
CDynsmicControlsDialog::CDynsmicControlsDialog(CWnd+ACo- pParent /+ACoAPQ-NULL+ACo-/)
: CDialog(CDynsmicControlsDialog::IDD, pParent)
+AHs-
//+AHsAew-AFX+AF8-DATA+AF8-INIT(CDynsmicControlsDialog)
// NOTE: the ClassWizard will add member initialization here
//+AH0AfQ-AFX+AF8-DATA+AF8-INIT
+AH0-
void CDynsmicControlsDialog::DoDataExchange(CDataExchange+ACo- pDX)
+AHs-
CDialog::DoDataExchange(pDX)+ADs-
//+AHsAew-AFX+AF8-DATA+AF8-MAP(CDynsmicControlsDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//+AH0AfQ-AFX+AF8-DATA+AF8-MAP
+AH0-
// Note the messages manually added here for the controls:
BEGIN+AF8-MESSAGE+AF8-MAP(CDynsmicControlsDialog, CDialog)
//+AHsAew-AFX+AF8-MSG+AF8-MAP(CDynsmicControlsDialog)
ON+AF8-BN+AF8-CLICKED(IDC+AF8-ADDCONTROL, OnAddcontrol)
ON+AF8-BN+AF8-CLICKED(IDC+AF8-DYNAMIC+AF8-BUTTON, OnMyDynamicButton)
ON+AF8-EN+AF8-CHANGE(IDC+AF8-DYNAMIC+AF8-EDIT, OnMyDynamicEdit)
//+AH0AfQ-AFX+AF8-MSG+AF8-MAP
END+AF8-MESSAGE+AF8-MAP()
////////////////////////////////////////////////////////////////////////////
/
// CDynsmicControlsDialog message handlers
// This function responds to the command that adds the control:
void CDynsmicControlsDialog::OnAddcontrol()
+AHs-
// A rect used to size the controls:
RECT rect+ADs-
// Create a button:
m+AF8-pButton +AD0- new CButton+ADs-
rect.left +AD0- 10+ADs-
rect.top +AD0- 10+ADs-
rect.right +AD0- 140+ADs-
rect.bottom +AD0- 34+ADs-
m+AF8-pButton-+AD4-Create(+ACI-My Dynamic Button+ACI-, WS+AF8-CHILD+AHw-WS+AF8-VISIBLE+AHw-WS+AF8-TABSTOP,
rect, this, IDC+AF8-DYNAMIC+AF8-BUTTON)+ADs-
// Create a 'buddy' control for the spinbutton:
m+AF8-pBuddy +AD0- new CEdit+ADs-
rect.left +AD0- 10+ADs-
rect.top +AD0- 40+ADs-
rect.right +AD0- 140+ADs-
rect.bottom +AD0- 64+ADs-
m+AF8-pBuddy-+AD4-Create(WS+AF8-BORDER+AHw-WS+AF8-CHILD+AHw-WS+AF8-VISIBLE+AHw-WS+AF8-TABSTOP,
rect, this, IDC+AF8-BUDDY)+ADs-
// Create the spinbutton:
m+AF8-pSpinButton +AD0- new CSpinButtonCtrl+ADs-
rect.left +AD0- 116+ADs-
rect.top +AD0- 40+ADs-
rect.right +AD0- 140+ADs-
rect.bottom +AD0- 64+ADs-
m+AF8-pSpinButton-+AD4-Create(WS+AF8-CHILD+AHw-WS+AF8-VISIBLE+AHw-WS+AF8-TABSTOP+AHw-UDS+AF8-ARROWKEYS+AHw-UDS+AF8-SETBU
DDYINT+AHw-UDS+AF8-AUTOBUDDY+AHw-UDS+AF8-ALIGNRIGHT,
rect, this, IDC+AF8-DYNAMIC+AF8-SPINBUTTON)+ADs-
m+AF8-pSpinButton-+AD4-SetRange(1, 10)+ADs-
m+AF8-pSpinButton-+AD4-SetPos(1)+ADs-
// Cretate the textcontrol:
m+AF8-pEdit +AD0- new CEdit+ADs-
rect.left +AD0- 10+ADs-
rect.top +AD0- 70+ADs-
rect.right +AD0- 140+ADs-
rect.bottom +AD0- 94+ADs-
m+AF8-pEdit-+AD4-Create(WS+AF8-BORDER+AHw-WS+AF8-CHILD+AHw-WS+AF8-VISIBLE+AHw-WS+AF8-TABSTOP,
rect, this, IDC+AF8-DYNAMIC+AF8-EDIT)+ADs-
+AH0-
// Responses. Note that the spinbutton is automatically connected via the
flag UDS+AF8-AUTOBUDDY to update the buddy control.
// Respons to the dynamic button clicked:
void CDynsmicControlsDialog::OnMyDynamicButton()
+AHs-
AfxMessageBox(+ACI-You have clicked My Dynamic Button+ACI-)+ADs-
+AH0-
// Response to edition in the edit box:
void CDynsmicControlsDialog::OnMyDynamicEdit()
+AHs-
AfxMessageBox(+ACI-You have edited My Dynamic Edit+ACI-)+ADs-
+AH0-
+ADw-hpmberg+AEA-algonet.se+AD4-