Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a dialog box and a tab control on it.
class for dialog box is MyDlg and class for tab control is MyTabCtr.
MyDlg.h
C++
class MyDlg: public CDialog
{
enum { IDD = IDD_ZF1SDKLIGHTTUTORIAL_DIALOG };
public:
MyDlg(CWnd* pParent = NULL);// standard constructor
LRESULT ButtonPressed(WPARAM w, LPARAM l);////do this
MyTabCtrl m_cTab; //Reference for tab control class
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support
}

MyDlg.cpp
C++
BEGIN_MESSAGE_MAP(CZF1SDKLightTutorialDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_BUTTONPRESSED,ButtonPressed) ////do this
ON_WM_TIMER()
END_MESSAGE_MAP()

BOOL Mylg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);			// Set big icon
m_cTab.Init();
m_cTab.InsertItem(0,"Register new user");
m_cTab.InsertItem(1,"Identify fingerprints");
m_cTab.CreateButton("Register User",24,0,0,520, 450,80);
return TRUE;  // return TRUE  unless you set the focus to a control
}
LRESULT MyDlg::ButtonPressed(WPARAM w, LPARAM l)////do this
{
int a=3,b=6,c;
c=a+b;
return 0;
}

void MyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TAB1, m_cTab);
}


MyTabCtrl .h
C++
class MyTabCtrl : public CTabCtrl
{
	DECLARE_DYNAMIC(MyTabCtrl)
public:
	int m_DialogID,ControlID;;
	MyTabCtrl();
	virtual ~MyTabCtrl();
LRESULT ButtonPressed(WPARAM w, LPARAM l);	//mouse button clicked on a button control
void CreateButton(LPCTSTR sCaption, int nID, int iTab, UINT uLocation = 0, int iX = 0, int iY = 0, int iLen = 50);


MyTabCtrl .cpp
C++
BEGIN_MESSAGE_MAP(MyTabCtrl, CTabCtrl)
	ON_MESSAGE(WM_BUTTONPRESSED,ButtonPressed)
	END_MESSAGE_MAP()
LRESULT MyTabCtrl::ButtonPressed(WPARAM w, LPARAM l)
{
	//to do
	return 0;
}

When I click the button "Register User" it run the code of MyTabCtrl::ButtonPressed(WPARAM w, LPARAM l) does not run MyDlg::ButtonPressed(WPARAM w, LPARAM l)
But I want to run of MyDlg::ButtonPressed(WPARAM w, LPARAM l)(How?)
Posted

You may pass the message over to the parent of your tab control (the dialog window):
C++
LRESULT MyTabCtrl::ButtonPressed(WPARAM w, LPARAM l)
{
    return GetParent()->GetSafeHwnd() ? 
        ::SendMessage(GetParent()->GetSafeHwnd(), WM_BUTTONPRESSED, w, l) : 0;
}


Another option is to store a pointer to the dialog in your tab control and use this pointer to call functions from the dialog:

C++
// MyTabCtrl.h
class MyDlg;
class MyTabCtrl : public CTabCtrl
{
    MyDlg* m_pDlg;
}

// MyTabCtrl.cpp
MyTabCtrl::MytabCtrl()
{
    m_pDlg = NULL;
}
LRESULT MyTabCtrl::ButtonPressed(WPARAM w, LPARAM l)
{
    return m_pDlg ? m_pDlg->ButtonPressed(w, l) : 0;
}

// MyDlg.cpp
BOOL MyDlg::OnInitDialog()
{
    m_cTab.m_pDlg = this;
}
 
Share this answer
 
Comments
Espen Harlinn 4-Oct-12 4:56am    
5'ed!
Member 7909353 4-Oct-12 5:30am    
When I use option first and I click the button it gives...
Unhandled exception at 0x0044d8ee in ZF1SDKDemo.exe: 0xC0000005: Access violation reading location 0x00000020.

and in 2nd option it gives error number
c2143,c4430,c2065,c2227,c2143
Jochen Arndt 4-Oct-12 5:46am    
I did not know what you have done. But both solutions would work (the Cxxx errors are usually sourced by a typo).

In fact you are using my solution in yours because GetParent()->PostMessage(...) is similar to ::PostMessage(GetParent()->GetSafeHwnd(), ...). I used SendMessage() but this should make no difference in this case.
LRESULT MyTabCtrl::ButtonPressed(WPARAM w, LPARAM l)
{
	// return GetParent()->GetSafeHwnd() ? ::SendMessage(GetParent()->GetSafeHwnd(), WM_BUTTONPRESSED, w, l) : 0;

	//return m_MyDlg?m_MyDlg->ButtonPressed(w,l):0;
	int nID = 0;
	CString s;
	HWND h = (HWND)w;
	BOOL bKeyPress = (BOOL)l;
	if(h == NULL){return 0;}
	CPoint cur;
	CRect rc;
	::GetCursorPos(&cur);	
	::GetWindowRect(h,rc);
	CWnd* pWnd = CWnd::FromHandle(h);
	
	pWnd->GetWindowTextA(s);
	if(s==buttonCaption)
	{
		//do we have a normal push button?
		DWORD dwStyle = WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP;
		DWORD dw = pWnd->GetStyle();
		dw &= ~(dwStyle);//remove all the styles from dw
		if(dw <= 1)//regular pushbutton
		{			
			nID = pWnd->GetDlgCtrlID();
			GetParent()->PostMessage(WM_BUTTONPRESSED,nID,0);
		}
	}
	

	//make sure mouse is inside of button when released
	else if((cur.x > rc.left && cur.x < rc.right && cur.y > rc.top && cur.y < rc.bottom) || bKeyPress)
	{		
		//do we have a normal push button?
		DWORD dwStyle = WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP;
		DWORD dw = pWnd->GetStyle();
		dw &= ~(dwStyle);//remove all the styles from dw
		if(dw <= 1)//regular pushbutton
		{			
			nID = pWnd->GetDlgCtrlID();
			GetParent()->PostMessage(WM_BUTTONPRESSED,nID,0);
		}
	}
	return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900