locked
Creating buttons on a dialog (on the fly) in MFC RRS feed

  • Question

  • Hi,

    I was wondering is there a way to create buttons on an MFC dialog on the fly without using the GUI builder. Or would I be better off using the Win32 API?

    For example wrapping the button constructor in a while loop to generate x number of buttons (the number of which will vary from execution to execution).

    Thanks for any help you can offer me.

    Friday, November 19, 2010 5:16 PM

Answers

  • Creating controls on the fly is about the same in MFC and Win32.  Your loop can create a CButton (with 'new') and then call its Create function.

    For a small but variable number of buttons it is easier to put the maximum number needed using the dialog visual editor, then at runtime hide the ones you don't need by calling

    CButton::ShowWindow(SW_HIDE).

     

    • Proposed as answer by «_Superman_» Friday, November 19, 2010 6:56 PM
    • Marked as answer by SillyIntern Friday, November 19, 2010 7:11 PM
    Friday, November 19, 2010 5:28 PM

All replies

  • Creating controls on the fly is about the same in MFC and Win32.  Your loop can create a CButton (with 'new') and then call its Create function.

    For a small but variable number of buttons it is easier to put the maximum number needed using the dialog visual editor, then at runtime hide the ones you don't need by calling

    CButton::ShowWindow(SW_HIDE).

     

    • Proposed as answer by «_Superman_» Friday, November 19, 2010 6:56 PM
    • Marked as answer by SillyIntern Friday, November 19, 2010 7:11 PM
    Friday, November 19, 2010 5:28 PM
  • Creating controls on the fly is about the same in MFC and Win32.  Your loop can create a CButton (with 'new') and then call its Create function.

    For a small but variable number of buttons it is easier to put the maximum number needed using the dialog visual editor, then at runtime hide the ones you don't need by calling

    CButton::ShowWindow(SW_HIDE).

     

    Ahh thanks for the help. I'll do a little bit more reading on the subject.
    Friday, November 19, 2010 5:46 PM
  • For the sake of completeness, controls are create dynamically in Win32 using the CreateWindow API.

    The class name parameter must be given the predefined system class names like BUTTON, COMBOBOX, EDIT etc.


    «_Superman_»
    Microsoft MVP (Visual C++)
    Friday, November 19, 2010 6:56 PM
  • For the sake of completeness, controls are create dynamically in Win32 using the CreateWindow API.

    The class name parameter must be given the predefined system class names like BUTTON, COMBOBOX, EDIT etc.


    «_Superman_»
    Microsoft MVP (Visual C++)
    Thanks.
    Friday, November 19, 2010 7:10 PM