Creating a Dialog with a Variable Number of Buttons


Click here for a larger image.

Environment: VC6

I was faced with a requirement for a dialog to display text retrieved from a DB table. Rather than showing this in a list control, the user wanted a button for each DB record, of which there would be an unknown number at runtime.

Creating buttons dynamically is straightforward, but to handle ON_BN_CLICKED in the dialog's message map we would need to know each button's control ID in advance. Therefore, I created a CButton-derived class (CMyButton) and handled the button click message there.

To avoid CMyButton needing to have knowledge of its parent dialog class, and to make it generic, registered window messages are used to inform the parent dialog of a button click event. The declaration of CMyButton is:

class CMyButton : public CButton
{
public:
  CMyButton();
  virtual ~CMyButton();

  int m_index;            // member used to identify this button
  UINT m_uBtnClickMsg;    // an app-defined message created with
                          // RegisterWindowMessage(LPCTSTR)

protected:
  afx_msg void OnClicked();

  DECLARE_MESSAGE_MAP()
};


void CMyButton::OnClicked() 
{
  ::PostMessage(GetSafeOwner()->GetSafeHwnd(), m_uBtnClickMsg,
                                (WPARAM)m_index, 0);
}

The parent dialog (CVarButtonsDlg) handles messages using the ON_REGISTERED_MESSAGE macro:

BEGIN_MESSAGE_MAP(CVarButtonsDlg, CDialog)
  ...
  ON_REGISTERED_MESSAGE(g_uBtnClickMsg, OnMsgBtnClick)
END_MESSAGE_MAP()

// declaration of OnMsgBtnClick :
LRESULT OnMsgBtnClick(WPARAM wParam, LPARAM lParam);

The buttons are constructed dynamically by the dialog class:

CVarButtonsDlg::OnInitDialog() {

  ...
  g_uBtnClickMsg = RegisterWindowMessage(BTN_MSG_STRING);
  // BTN_MSG_STRING is a unique string; I used guidgen to
  // create it

  CString csTemp;
  CRect rBtn;

  // say we need 10 buttons...
  for (int i = 0; i<10; i++) {
    CMyButton* pBtn = new CMyButton();

    m_arrayBtns.Add(pBtn);       // CTypedPtrArray storing all
                                 // buttons dynamically created
    GetButtonRect(i, rBtn);      // will populate rBtn given
                                 // button number

    csTemp.Format("Button%d", i);
    pBtn->Create(csTemp, WS_CHILD|WS_VISIBLE|WS_TABSTOP, rBtn,
                 this, 9001+i);  // give a unique ID
                                 // (not strictly necessary)
    pBtn->m_index = i;
    pBtn->m_uBtnClickMsg = g_uBtnClickMsg;
  }

  // size the dialog box to the number of buttons
  int width = 680;
  int height = ((i/3)*60) + 100;    // on my form, the buttons
                                    // are in rows of three
  SetWindowPos(NULL,                // pWndAfter (z-order)
               0,                   // x pos
               0,                   // y pos
               width,
               height,
               SWP_NOZORDER|SWP_NOMOVE);
  ...
}

CVarButtonsDlg::GetButtonRect(int i, CRect& r)
{
  // in this example, the buttons are arranged in three columns
  if (i%3 == 0) {         // first column
    r.left = 20;
    r.right = 220;
  }
  else if (i%3 == 1) {    // second column
    r.left = 240;
    r.right = 440;
  }
  else {                  // third column
    r.left = 460;
    r.right = 660;
  }
  // now get top & bottom positions
  int row = (i/3);
  r.top = 20 + (row*60);
  r.bottom = 60 + (row*60);
}


LRESULT CVarButtonsDlg::OnMsgBtnClick(WPARAM wParam,
                                      LPARAM lParam)
{
  CString csTemp;
  csTemp.Format("You pressed button %d", int(wParam));
  MessageBox(csTemp);
  return 0;
  // in a real app. I use wParam as the index for a data array
  // and do something more meaningful...
}

Why have I used registered window messages and not something like WM_APP+100? Because it is possible for another app to broadcast the same carefully chosen WM_APP value to our app. Registered messages are therefore safer.

Downloads

Download source - 12 Kb


Comments

  • Dialog Control Limitations

    Posted by Legacy on 05/06/2003 12:00am

    Originally posted by: Blake Miller

    I hope there will not be more than 256 records.
    I seem to recall a documented limitation of 256 controls in each window.
    It would have seemed a virtual listbox might have been better, or a grid control or something, at least for your specific application.

    Reply
  • ON_COMMAND_RANGE()

    Posted by Legacy on 05/02/2003 12:00am

    Originally posted by: Peter Mares

    Why didn't you just use a ON_COMMAND_RANGE() macro?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds