Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++ 2005
Visual C++ 2005 For discussion of Visual C++ 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 24th, 2006, 05:48 PM
Registered User
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to burtlo Send a message via AIM to burtlo Send a message via MSN to burtlo Send a message via Yahoo to burtlo
Default Adding Controls At Runtime with MFC.

Most every example in the book has the user create a control through the visual editor for MFC.

Every answer I've attempted to google for gives the advive of creating all the controls in the visual editor and hide them until they are needed.

However, my problem is that the number of controls I want is dynamic at run-time. I attempted to add a button to the dialog box through the following code:

class CVnRecoveryDlg : public CDialog
// Construction
    CVnRecoveryDlg(CWnd* pParent = NULL);    // standard constructor
    CButton *pButton;

// Other Code... //


CVnRecoveryDlg::CVnRecoveryDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CVnRecoveryDlg::IDD, pParent)
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    pButton = new CButton();


BOOL CVnRecoveryDlg::OnInitDialog()

    // 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

    // TODO: Add extra initialization here
    pButton->Create(_TEXT("&Button Name"), BS_TEXT | BS_CENTER, CRect(20,20,250,20), this, 0x302);

    return TRUE;  // return TRUE  unless you set the focus to a control
Old October 10th, 2007, 07:06 AM
Registered User
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

But how do you catch messages to the button that you added at runtime? ( for example when it is clicked)

Old October 13th, 2007, 06:36 AM
Registered User
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Here is some snippet of code regarding dynamic creation of buttons (at runtime):

BOOL CSubmarinesGUIDlg::CreateDynamicButtons()
    int numberOfButton = 1100;
    CWnd *w=CWnd::FromHandle(GetSafeHwnd());
    matrix = new CBitmapButton**[maxx];
    for (int k = 0; k < maxx; k++)
        matrix[k] = new CBitmapButton*[maxy];

    for (int i=1;i<=maxx;i++)
        for (int j=1; j<=maxy; j++)
            CPoint p;
            CBitmapButton* btn = new CBitmapButton();
            btn->Create(NULL,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,CRec t(0+p.x,0+p.y,40+p.x,20+p.y),w ,numberOfButton);
            btn->LoadBitmaps(IDB_BITMAP1,IDB_BITMAP1,IDB_BITMAP1 );
            matrix[i-1][j-1] = btn;
    return TRUE;

Here is how to catch the click messages:

LRESULT CSubmarinesGUIDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    if ((message == WM_COMMAND)) {
        if (wParam>=1100) //ids for our buttons
            int id = (int)wParam - 1100;
            int i = id/maxy;
            int j = id%maxy; // compute the coordinates using the id
        matrix[i][j]->LoadBitmaps(IDB_BITMAP2,IDB_BITMAP2,IDB_BITMAP2 );
return CDialog::WindowProc(message, wParam, lParam);

Similar Threads
Thread Thread Starter Forum Replies Last Post
Controls at runtime ITGeek ASP.NET 1.0 and 1.1 Basics 4 July 11th, 2007 05:23 AM
Adding forms at runtime Bhalchandra Pro VB 6 1 April 17th, 2007 12:45 PM
Adding button at runtime ehabinl ASP.NET 1.0 and 1.1 Professional 2 August 7th, 2006 02:46 PM
Adding button at runtime ehabinl BOOK: Professional C#, 2nd and 3rd Editions 0 August 7th, 2006 03:48 AM
Runtime Problems after adding new controls colmtheirishman General .NET 0 January 12th, 2005 12:08 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.