Dynamic Button Creation

I have a dialog box which I would like to create a variable number of buttons at run time. I use the CButton class, and the Create method in it, and it returns a True (for success), but I don't see the button within the dialog box. Also, any ideas how I would handle the message map event lines, since I don't know what buttons will be on the screen until run time? Thanks...

Comments

: I have a dialog box which I would like to create a variable number of buttons at run time. I use the CButton class, and the Create method in it, and it returns a True (for success), but I don't see the button within the dialog box. Also, any ideas how I would handle the message map event lines, since I don't know what buttons will be on the screen until run time? Thanks...: :

After you create the button you have to make it visible cButton->ShowWindow(true); The question about the message mapping I would like answered also.

[italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

[b][red]This message was edited by stober at 2004-1-14 6:54:50[/red][/b][hr]: : [blue]You have to manually set up the message map for these OnButtonClick events. The following code allocates 10 buttons and uses the same event handler for each (see [red]RED[/red] below[/blue]: : Thats neat using the "ON_CONTROL_RANGE" message map Stober. How do you tell which button was clicked in the OnButton1() function?:

: [b][red]This message was edited by stober at 2004-1-14 6:54:50[/red][/b][hr]: : : [blue]You have to manually set up the message map for these OnButtonClick events. The following code allocates 10 buttons and uses the same event handler for each (see [red]RED[/red] below[/blue]: : : : Thats neat using the "ON_CONTROL_RANGE" message map Stober. How do you tell which button was clicked in the OnButton1() function?: : : : call GetFocus() to get HWND, then GetDlgCtrlID() to get the ID.: : : Thanks guys, thats what I needed to know...

: [b][red]This message was edited by stober at 2004-1-14 6:54:50[/red][/b][hr]: : : [blue]You have to manually set up the message map for these OnButtonClick events. The following code allocates 10 buttons and uses the same event handler for each (see [red]RED[/red] below[/blue]: : : : Thats neat using the "ON_CONTROL_RANGE" message map Stober. How do you tell which button was clicked in the OnButton1() function?: : : : call GetFocus() to get HWND, then GetDlgCtrlID() to get the ID.: : :

Digging through MS docs, I found an easier way... declare the handler like this: afx_msg void OnDoSomething( UINT nID ); then you have the ID already in the handler

[italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

: : Digging through MS docs, I found an easier way...: declare the handler like this: afx_msg void OnDoSomething( UINT nID ); then you have the ID already in the handler

[blue]Well, not really. Two reasons(): (1) The OnButtonEvent() does not have a parameter, and (2) If you don't add that function to the message map it will never get called.

If you have a dialog with two buttons, and use the wizard to create the event handler for each, where each button uses the same event, the results are like below, which in the original posters problem, is not what he wants[/blue][code]BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON()[red] ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton1)[/red] //}}AFX_MSG_MAPEND_MESSAGE_MAP()

: : : : Digging through MS docs, I found an easier way...: : declare the handler like this: afx_msg void OnDoSomething( UINT nID ); then you have the ID already in the handler : : : [blue]Well, not really. Two reasons(): (1) The OnButtonEvent() does not have a parameter, and (2) If you don't add that function to the message map it will never get called.: : If you have a dialog with two buttons, and use the wizard to create the event handler for each, where each button uses the same event, the results are like below, which in the original posters problem, is not what he wants[/blue]: [code]: BEGIN_MESSAGE_MAP(CTestDlg, CDialog): //{{AFX_MSG_MAP(CTestDlg): ON_WM_SYSCOMMAND(): ON_WM_PAINT(): ON_WM_QUERYDRAGICON(): [red] ON_BN_CLICKED(IDC_BUTTON1, OnButton1): ON_BN_CLICKED(IDC_BUTTON2, OnButton1)[/red]: //}}AFX_MSG_MAP: END_MESSAGE_MAP(): : : // in the .h file: // Generated message map functions: //{{AFX_MSG(CTestDlg): virtual BOOL OnInitDialog();: afx_msg void OnSysCommand(UINT nID, LPARAM lParam);: afx_msg void OnPaint();: afx_msg HCURSOR OnQueryDragIcon();: virtual void OnOK();: [red] afx_msg void OnButton1();[/red]: //}}AFX_MSG: DECLARE_MESSAGE_MAP(): [/code]:

My reply was in response to getting the ID of the pressed button when you use the ON_CONTROL_RANGE message map.. I've tried it and it works good. Using your previous example:[code]BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP [green] // this has to be outside the wizard created AFX_MSG_MAP (see msdn docs)[/green] [red]ON_CONTROL_RANGE(BN_CLICKED,100,110,OnAnyButton)[/red]END_MESSAGE_MAP()

// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon

I have just one more twist to add to the conversation. Suppose instead of creating a dialog on which to display these buttons, one wants to display them on the main form, a CFameWnd. How would this be accomplished. I've tried the outlined steps, and it works great for a dialog, but not for a main window.