SetFocus () error with valid window handle

I have a program with several custom controls. One of these custom controls is a text input control. Since the window does not automatically receive keyboard focus when you click on it, I created a mouse hook in my program that calls SetFocus () on the window when the user clicks on this window. However, there is a problem.

If another program has focus, when you click on my program window (or any control in this window), SetFocus () fails. Then I have to click again to succeed. Here is the code:

LRESULT CALLBACK kbfProc(int nCode, WPARAM wParam, LPARAM lParam) // Keyboard focus switching procedure
{
    switch(nCode)
    {
        case HC_ACTION:
        {
            if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
            {
                MOUSEHOOKSTRUCT * mhs = (MOUSEHOOKSTRUCT*) lParam;

                if(SetFocus(mhs->hwnd) == NULL)
                {
                    printf("SetFocus(Hwnd = %.8x) failed. Error code: %lu\n", mhs->hwnd, GetLastError());
                } else {

                    printf("SetFocus(Hwnd = %.8x) returned success.\n", mhs->hwnd);
                }
            }

        }
        break;
    }

    return CallNextHookEx(0, nCode, wParam, lParam);
}

And the output of these printf calls is this:

SetFocus(Hwnd = 00410c06) failed. Error code: 87
SetFocus(Hwnd = 00410c06) returned success.
SetFocus(Hwnd = 01740fc8) failed. Error code: 87
SetFocus(Hwnd = 01740fc8) returned success.

87 - ERROR_INVALID_PARAMETER, , , , ?

+5
2

. , , -. , , . , WM_ACTIVATE , , , . :

:

LRESULT CALLBACK kbfProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    switch(nCode)
    {
        case HC_ACTION:
        {
            if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
            {
                MOUSEHOOKSTRUCT * mhs = (MOUSEHOOKSTRUCT*) lParam;

                BringWindowToTop(MainWindow->t_hwnd);
                SetFocus(mhs->hwnd);
            }

        }
        break;
    }

    return CallNextHookEx(0, nCode, wParam, lParam);
}

WM_ACTIVATE:

    case WM_ACTIVATE:
    {
        unsigned long state = (unsigned long) wParam & 0x0000FFFF;
        unsigned long mState = (unsigned long) wParam & 0xFFFF0000;

        if(state != 0)
        {
            ...[some code here]...

            FocusChildWindow(hwnd);
        }

        ...[some code here]...
    }
    break;

FocusChildWindow():

void FocusChildWindow(HWND hwnd)
{
    POINT mpos;
    GetCursorPos(&mpos);

    RECT wr;
    GetWindowRect(hwnd, &wr);

    mpos.x -= wr.left;
    mpos.y -= wr.top;

    HWND cw = ChildWindowFromPoint(hwnd, mpos);

    if(cw == NULL || cw == hwnd)
    {
        SetFocus(hwnd);
    } else {

        GetCursorPos(&mpos);
        HWND cw2;

        while(1)
        {
            POINT sc = mpos;
            MapWindowPoints(HWND_DESKTOP, cw, &sc, 1);

            cw2 = ChildWindowFromPoint(cw, sc);

            if(cw2 == NULL || cw2 == cw)
            {
                SetFocus(cw);
                break;
            } else {

                cw = cw2;
            }


        }

    }

}

. .

+1

, SetFocus, , SetFocus , . , SetForegroundWindow, SetFocus.

+3

All Articles