Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MessageMap with Dynamic Controls

Status
Not open for further replies.

krionic

Programmer
Jun 5, 2004
6
US
What I'm trying to do is handle an event of a control created during runtime.

Example: User does something that makes the app display 1 to 3 buttons. Lets say in this instance it generated 2 CButton

Here is some sample code of what i'm doing. I'm wrapping CButton to make my own button class, but I want it to generate different output on a MessageBox... say, its ID (cbutID)

Header
Code:
#include <afxwin.h>
#include <afxcmn.h>

#define IDB_BUTTON 100

// Declare the application class
class CHelloApp : public CWinApp
{
public:
  virtual BOOL InitInstance();
};

//Declare CButtonMain
class CButtonMain : public CButton
{
   int cbutID;
   public:
   Creater(class CHelloWindow *const obj, CRect butRect, int cbutID);
};



// Declare the main window class
class CHelloWindow : public CFrameWnd
{
  CStatic* cs;
  CMainTabCtrl* ctab;
  CButtonMain* cbut1;
  CButtonMain* cbut2;
  CRect cbut1Rect;
  CRect cbut2Rect;
public:
	
  CHelloWindow();
  afx_msg void HandleButton();
  DECLARE_MESSAGE_MAP()
};


BEGIN_MESSAGE_MAP(CHelloWindow, CFrameWnd)
   ON_BN_CLICKED(IDB_BUTTON, HandleButton)
END_MESSAGE_MAP()

Source

Code:
//hello.cpp


#include "hello.h"


// Create an instance of the application class

CHelloApp HelloApp;





// The InitInstance function is called each
// time the application first executes.
BOOL CHelloApp::InitInstance()
{
 
	m_pMainWnd = new CHelloWindow();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

// The constructor for the window class
CHelloWindow::CHelloWindow()
{
	
	
	// Create the window itself
	Create(NULL, "Hello World!", WS_OVERLAPPEDWINDOW, CRect(100,100,700,500));
	// Create a static label

	//GetClientRect(&r);
	//r.InflateRect(-20,-20);
	CHelloWindow::CenterWindow();

	cs = new CStatic();
	cs->Create("hello world", WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(50,80, 150, 150), this);
	ctab = new CMainTabCtrl();
	ctab->Creater(this);
	cbutID = 100;
	cbut1Rect = CRect(25,25,50,50);
	cbut1 = new CButtonMain();
	cbut1->Creater(this, cbut1Rect, cbutID);
	cbutID = 101;
	cbut2Rect = CRect(0,0,25,25);
	cbut2 = new CButtonMain();
	cbut2->Creater(this, cbut2Rect, cbutID);

}

void CHelloWindow::HandleButton(int cbutID)
{
	
		MessageBox((const char *)cbutID, "Error", 
      MB_ICONERROR | MB_OK);
	
}




CButtonMain::Creater(class CHelloWindow *const obj, CRect butRect, int cbutID)
{
	CButtonMain::cbutID = cbutID;
	Create("Push me", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, butRect, obj, cbutID);
}

How can i associate an instance of the CButtonMain class to the event Handler (Message)

Any push in the right direction, is appreciated.

PS. Is there any way to tell Visual C++ 6.0 to generate source code for controls instead of reference files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top