Click to See Complete Forum and Search --> : How to control this ?


Kobe
September 16th, 1999, 05:10 AM
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 ?

Oleg Lobach
September 16th, 1999, 06:17 AM
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.

Kobe
September 16th, 1999, 06:58 AM
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

Martin Speiser
September 16th, 1999, 07:08 AM
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

Oleg Lobach
September 16th, 1999, 07:17 AM
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.

Kobe
September 16th, 1999, 08:00 AM
Good Idea !
Thank you very much

Kobe
September 16th, 1999, 08:07 AM
Good Idea !
Thank you very much