GetNextDlgTabItem用法详解,回车替代Tab键切换控件焦点

清泛原创

GetNextDlgTabItem 函数按指定方向(第二个参数,TRUE往前,默认FALSE往后)检索对话框中有WS_TABSTOP类型的第一个控件的句柄,即按照对话框Tab顺序(rc中Ctrl+D查看、点击修改)检索上一个/下一个控件句柄

函数原型:
HWND GetNextDlgTabltem(HWND hDlg,HWND hCtl,BOOL bPrevious);

The GetNextDlgTabItem function retrieves a handle to the first control that has the WS_TABSTOPstyle that precedes (or follows) the specified control.

Syntax

HWND GetNextDlgTabItem(  HWND hDlg, HWND hCtl, BOOL bPrevious );

Parameters

hDlg
[in] Handle to the dialog box to be searched.
hCtl
[in] Handle to the control to be used as the starting point for the search. If this parameter is NULL, the function uses the last (or first) control in the dialog box as the starting point for the search.
bPrevious
[in] Specifies how the function is to search the dialog box. If this parameter is TRUE, the function searches for the previous control in the dialog box. If this parameter is FALSE, the function searches for the next control in the dialog box.

Return Value

If the function succeeds, the return value is the window handle of the previous (or next) control that has the WS_TABSTOP style set.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.
 

Remarks

The GetNextDlgTabItem function searches controls in the order (or reverse order) they were created in the dialog box template. The function returns the first control it locates that is visible, not disabled, and has the WS_TABSTOP style. If no such control exists, the function returns hCtl.

If the search for the next control with the WS_TABSTOP style encounters a window with the WS_EX_CONTROLPARENT style, the system recursively searches the window's children


回车替代Tab键切换控件焦点,部分代码如下:
BOOL Cxxx::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	UINT nKeyCode = pMsg->wParam;
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
	{
		CWnd *wnd = GetFocus();
		if (wnd != NULL)
		{
			char str[256];
			CString ClassName = _T("Button");
			GetClassName (wnd->m_hWnd, str, 256);
			if (ClassName == str)
			{
				UINT i = wnd->GetDlgCtrlID();
				SendMessage (WM_COMMAND, i, (LPARAM)wnd->m_hWnd);
				CWnd *mwnd = GetNextDlgTabItem (wnd);
				if (mwnd)
					mwnd->SetFocus();
				return TRUE;
			}
		}
		CWnd *mwnd = GetNextDlgTabItem (wnd);
		if (mwnd) 
		{
			mwnd->SetFocus();
			return TRUE;
		}
	}
	return CFormView::PreTranslateMessage(pMsg);
}

(供参考)

GetNextDlgTabItem

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部