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

    How to control this ?

    Hi,
    I create a button at runtime. When I click it it's not happen.
    I'd like to control this button. When clicked => show any message or others

    How to do this ?


  2. #2
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    667

    Re: How to control this ?

    Hi,
    I suppose you are creating CButton using it's Create member:

    BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );



    Then,take a look at the last parameter of this function: UINT nID. That is what you want.

    So,try this :
    1. Using Resourse->String Table create(Insert) new resource ID (For instance ID_MyButton).
    2.Use ID_MyButton as the last parameter of Create(....,..,..,...,ID_MyButton);
    3. Insert in the parent window class implementation file before line with "END_MESSAGE_MAP()" the code:

    ON_BN_CLICKED(ID_MyButton, OnMyButton)



    4. Insert in the parent window class header file before line with "DECLARE_MESSAGE_MAP()"

    afx_msg void OnMyButton();



    5. Implement OnMyButton() - it should be called each time the button is pressed.

    Hope this helps,
    Oleg.





  3. #3
    Join Date
    Aug 1999
    Posts
    167

    Re: How to control this ?

    Thanks for your help

    but have any method or function to add


    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)




    between BEGIN_MESSAGE_MAP(CMoveButtonView,CFormView)
    and END_MESSAGE_MAP()

    not add by manual
    (Because I don't only have 1 button that create on
    runtime it may be have many buttons depend on input)

    Thanks again


  4. #4
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: How to control this ?

    Hi Kobe,

    then you need to alter Oleg's solution a bit.

    1) Declare a range of button IDs in your resource.h. For instance#define WM_FIRST_BUTTON 0x1000
    #define WM_LAST_BUTTON 0x1100


    It's not really needed, but it's more handy. This will give you a range of 256 dynamically created buttons. Should be enough, shouldn't it?

    2) Add the line ON_CONTROL_RANGE( BN_CLICKED, WM_FIRST_BUTTON, WM_LAST_BUTTON, OnButtonClicked )

    outside of the // {{AFX_MSG_MAP delimiters.

    3) Create up to 256 buttons with CButton::Create, using unique IDs between WM_FIRST_BUTTON and WM_LAST_BUTTON.

    3) Write a handler void CMyDialog::OnButtonClicked( UINT nID )
    {
    // Add your code here
    }




    Martin

  5. #5
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    667

    Re: How to control this ?

    Hi,
    You can use ON_CONTROL_RANGE macro to map a contiguous range of command IDs to a single message handler function. The range of IDs starts with id1 and ends with id2.
    In your case for 20 buttons do this:
    ON_CONTROL_RANGE( BN_CLICKED,IDC_BUTTON1, IDC_BUTTON20, OnDynButton )

    then in OnDynButton(UINT nId) :

    void CMoveButtonView::OnDynButton(UINT nId)
    {
    switch(nId)
    {
    case IDC_BUTTON1:
    //BUTTON1 clicked
    break;
    case IDC_BUTTON3:
    //BUTTON3 clicked
    break;
    //....
    //....
    //....
    case IDC_BUTTON20:
    //BUTTON20 clicked
    break;

    }
    }



    Hope this helps,
    Oleg.


  6. #6
    Join Date
    Aug 1999
    Posts
    167

    Re: How to control this ?

    Good Idea !
    Thank you very much


  7. #7
    Join Date
    Aug 1999
    Posts
    167

    Re: How to control this ?

    Good Idea !
    Thank you very much


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