CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    61

    Help me to Create Button

    Hi,
    I need your help. I try dynamicly create a button.
    I use CreateWindow .How can I add function to do OnClick() event.
    Please help me !!!!!
    Thanks a lot.

    Drago


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: Help me to Create Button

    Hi,

    this a example how to create a button dynamically, hope it will help:

    RECT r;
    CButton pNewButton;

    r.bottom = 30;
    r.left = 10;
    r.right = 50;
    r.top = 10;

    pNewButton = new CButton;
    pNewButton->Create("Button",WS_VISIBLE,r,AfxGetMainWnd(),5001);

    it will create your button on your main Window,

    if you want to execute some funciton with this button, you have to add some
    information at your Message_map of your main window,

    <mainWindow.h>

    // Generated message map functions
    //{{AFX_MSG(mainWindow)
    ....
    afx_msg void OnButtonDyn(); // line to add
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()

    <mainWindow.cpp>

    BEGIN_MESSAGE_MAP(mainWindow, CDialog)
    //{{AFX_MSG_MAP(mainWindow)
    ...
    ON_BN_CLICKED(5001, OnButtonDyn) // line to add
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    void mainWindow::OnButtonDyn()
    {
    //do somthing...
    }




  3. #3
    Join Date
    Apr 1999
    Location
    Portland, OR, USA
    Posts
    29

    Re: Help me to Create Button

    Here is another way you can do it. Instead of making a dynamic button, you might want to consider creating the button in the resource editor, and then simply not showing the button until you need it.

    I.e., when you create the button resource, there should be an option in its properties to make it not visible at run time. Then, you add code in your application to make the button visible when its needed.

    ShowWindow(SW_SHOW); will make the button appear.
    ShowWindow(SW_HIDE); will make it disappear.

    I don't know if this is a viable solution for you, but it will solve your problem of handling button clicks... the button exists, you can associate a variable with it, and you can implement all the OnClick functionality you need for it.

    Good luck, and happy coding!

    =================================================
    Valerie L. Bradley
    Software Engineer
    Intel Corporation

    * All opinions expressed are mine and not those of my employer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured