Click here to Skip to main content
11,434,743 members (47,368 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 MFC VC++
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
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
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
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
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 3-Oct-12 21:55pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may pass the message over to the parent of your tab control (the dialog window):
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:

// 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;
}
  Permalink  
Comments
Espen Harlinn at 4-Oct-12 4:56am
   
5'ed!
Member 7909353 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
}
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150428.2 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100