Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:38:01+00:00 2026-05-31T03:38:01+00:00

I have a program with several custom controls. One of these custom controls is

  • 0

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

If another program has focus when you click on my program’s window (or any of the controls in that window) SetFocus() fails. I then have to click again for it to succeed. Here’s 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 those 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.

Error code 87 is ERROR_INVALID_PARAMETER, but i’m obviously passing a valid window handle to the function, so why is it failing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:38:04+00:00Added an answer on May 31, 2026 at 3:38 am

    I’ve found a solution. After a lot of googling and trial & error I eventually came across this webpage (backup link). It goes over the behavior of window focus and activation in detail.

    I ended up adding some code to the WM_ACTIVATE handler of my main window that searches for the child window that was clicked when the window is activated. Here’s all the code:

    Here’s the hook procedure:

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

    Here’s the code i put in the WM_ACTIVATE handler:

        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;
    

    And here’s the FocusChildWindow() function:

    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;
                }
                
                
            }
            
        }
        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to program several different types of binary trees. However, I'm not allowed
I have a c# program with several forms and labels. Some of these labels
I have a program that needs several third-party libraries, and at the moment it
I have a python program that opens several urls in seperate tabs in a
I have a fairly large program written in C. It spans several files, and
I'm working on a java program, and I have several vectors defined and filled
In my program I have several activities. When I run the program, I have
In my program I have several sockets on the server. Each socket has its
I have a program in java with several Jpanels designed; with certain font and
I have a C# program that opens several Microsoft Access files, and executes functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.