如何从tabControl项调用对话框方法 [英] how to call dialog method from tabControl item

查看:70
本文介绍了如何从tabControl项调用对话框方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框和一个选项卡控件.
对话框的类为MyDlg,选项卡控件的类为MyTabCtr.
MyDlg.h

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


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



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



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


当我单击注册用户"按钮时,它运行MyTabCtrl :: ButtonPressed(WPARAM w,LPARAM l)的代码不会运行MyDlg :: ButtonPressed(WPARAM w,LPARAM l)
但是我想运行MyDlg :: ButtonPressed(WPARAM w,LPARAM l)(How?)


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?)

推荐答案

您可以将消息传递给选项卡的父级控制(对话框窗口):
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;
}


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


这篇关于如何从tabControl项调用对话框方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆