Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write a class CTpForm, which is derivated from CWnd, and I use it as a child
dialog. In my doc-view project,i use the view Cnsf2View as a holder, there are
two CTpForm child windows in the view. my question is when i use tab key to navigeate between controls, the result is alway the same, it set foucs to the first
child control in the current child dialog,i don't know the reason. my target is
use tab key to navigate foucs between all the buttons in the two child dialog.
who will help me to analye the problem? I study the code of CPropetyPage and CPropetySheet, but i don't know why my code cannot work. any one who want get my full code, please give your email address, and i will send my project to you. thanks

C++
BOOL CTpForm::Create(CWnd* pParentWnd, LPCTSTR lpszTemplateName, /*UINT nStyle,*/ UINT nID)
{
	ASSERT(pParentWnd != NULL);
	ASSERT(lpszTemplateName != NULL);

#ifdef _DEBUG
	// dialog template must exist and be invisible with WS_CHILD set
	if (!_AfxCheckDialogTemplate(lpszTemplateName, TRUE))
	{
		ASSERT(FALSE);          // invalid dialog template name
		PostNcDestroy();        // cleanup if Create fails too soon
		return FALSE;
	}
#endif //_DEBUG

	// create a modeless dialog
	BOOL bSuccess = CreateDlg(lpszTemplateName, pParentWnd);
	if (!bSuccess)
		return FALSE;

	// dialog template MUST specify that the dialog
	//  is an invisible child window
	SetDlgCtrlID(nID);
	CRect rect;
	GetWindowRect(&rect);
	m_sizeDefault = rect.Size();    // set fixed size

	// force WS_CLIPSIBLINGS
	ModifyStyle(0, WS_CLIPSIBLINGS);

	if (!ExecuteDlgInit(lpszTemplateName))
		return FALSE;

	// force the size to zero - resizing bar will occur later
	SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE/*|SWP_SHOWWINDOW*/);

	ModifyStyle(NULL, WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
	return TRUE;
}

BOOL CTpForm::PreTranslateMessage(MSG* pMsg)
{
	return CWnd::PreTranslateMessage(pMsg);
}



BOOL Cnsf2View::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类

	if ( CView::PreTranslateMessage(pMsg))
		return TRUE;

	return MyPreTranslateInput(pMsg);
}

BOOL Cnsf2View::MyPreTranslateInput(LPMSG lpMsg_)
{
	ASSERT(::IsWindow(m_hWnd));

	if (lpMsg_->message != WM_KEYDOWN)
		return FALSE;
	// only process WM_KEYDOWN ...

	HWND _hwndFocus = ::GetFocus();
	if (!_hwndFocus)
	{
		ASSERT(false);
		return FALSE;
	}


	if (lpMsg_->wParam == VK_RETURN)
	{
		LRESULT _ret = ::SendMessage(_hwndFocus, WM_GETDLGCODE, 0, (LPARAM)lpMsg_);
		if (_ret & DLGC_WANTMESSAGE)
			return FALSE;

		if (_ret & DLGC_BUTTON)
		{
			::SendMessage(_hwndFocus, BM_CLICK, 0, 0);
			return TRUE;
		}
	}
	else if (lpMsg_->wParam == VK_TAB)
	{
		LRESULT _ret = ::SendMessage(_hwndFocus, WM_GETDLGCODE, 0, (LPARAM)lpMsg_);
		if (_ret & (DLGC_WANTALLKEYS | DLGC_WANTMESSAGE | DLGC_WANTTAB))
			return FALSE;

		if (::IsWindowVisible(_hwndFocus) && 
			::IsChild(m_hWnd, _hwndFocus))
		{
			return PreTranslateInput(lpMsg_);
		}
	}

	return FALSE;
}
Posted

1 solution

The Porblem is solved, the TpForm (child dialog) need an ex-style WS_EX_CONTROLPARENT

C++
BOOL CTpForm::Create(CWnd* pParentWnd, LPCTSTR lpszTemplateName, /*UINT nStyle,*/ UINT nID)
{
	ASSERT(pParentWnd != NULL);
	ASSERT(lpszTemplateName != NULL);

#ifdef _DEBUG
	// dialog template must exist and be invisible with WS_CHILD set
	if (!_AfxCheckDialogTemplate(lpszTemplateName, TRUE))
	{
		ASSERT(FALSE);          // invalid dialog template name
		PostNcDestroy();        // cleanup if Create fails too soon
		return FALSE;
	}
#endif //_DEBUG

	// create a modeless dialog
	BOOL bSuccess = CreateDlg(lpszTemplateName, pParentWnd);
	if (!bSuccess)
		return FALSE;

	// dialog template MUST specify that the dialog
	//  is an invisible child window
	SetDlgCtrlID(nID);
	CRect rect;
	GetWindowRect(&rect);
	m_sizeDefault = rect.Size();    // set fixed size

	// force WS_CLIPSIBLINGS
	ModifyStyle(0, WS_CLIPSIBLINGS);

	if (!ExecuteDlgInit(lpszTemplateName))
		return FALSE;

	// force the size to zero - resizing bar will occur later
	SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE/*|SWP_SHOWWINDOW*/);

	ModifyStyle(NULL, WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
	ModifyStyleEx(NULL, WS_EX_CONTROLPARENT);//这句重要,否则无法正常导航
	return TRUE;
}
 
Share this answer
 
Comments
Nelek 12-Mar-12 5:05am    
Thanks for posting your own solution, that is nice for other people that might have the same problem. 5 for it

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