WIN32 SDK 下子窗口VK_TAB键的焦点自动处理

void SetTabFocus(HWND hChildWnd)
{

    HWND hwndFirst = ::GetParent(hChildWnd);

    if (hwndFirst)
    {
        BOOL bFound=FALSE;
        HWND hwndNext = NULL;
        HWND FirstChild=NULL;

        while(hwndNext = ::FindWindowEx(hwndFirst,hwndNext,NULL,NULL))
        {

            DWORD dwStyle = ::GetWindowLong(hwndNext, GWL_STYLE);

            if ( (dwStyle & WS_TABSTOP) && (dwStyle & WS_VISIBLE) && !(dwStyle & WS_DISABLED) )
            {
                if (!bFound)
                {
                    if (FirstChild==NULL) FirstChild=hwndNext;

                    if (GetFocus()==hwndNext)
                    {
                        bFound=TRUE;
                    }

                }else{
                    bFound=FALSE;
                    ::SetFocus(hwndNext);
                    break;
                }

            }

        }

        if (bFound) ::SetFocus(FirstChild);

    }
}

int MessageLoop(UINT AccelID)
{
    HACCEL hAccelTable = LoadAccelerators(::GetModuleHandle(NULL), (LPCTSTR)AccelID);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
            if (msg.message==WM_KEYDOWN && msg.wParam==VK_TAB)
            {
                SetTabFocus(msg.hwnd);
            }

            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

你可能感兴趣的:(null)