Prevent the window from stealing focus.

Вадим Подгорный 41 Reputation points
2022-06-27T19:57:23.437+00:00

Hello
Maybe someone has solved a similar problem already.
I create a dialog box without a frame, assign the WS_EX_LAYERED style,
and move it over the main window within certain boundaries, and then destroy it.
The problem is that this transparent window steals focus from my main window and it looks bad.
I've used all the ways I know to lock the focus, but it only partially works.
Here is the sample code.

BOOL DialogTransparent::OnInitDialog()  
{  
    CDialog::OnInitDialog();  
    CDialog::ModifyStyleEx(0, WS_EX_LAYERED | WS_EX_TRANSPARENT, SWP_FRAMECHANGED);  
    CDialog::SetLayeredWindowAttributes(0, 120, LWA_ALPHA);  
    return FALSE;// Return FALSE in the transparent window class when creating  
}  
   
//In the main window //   
DialogTransparent m_transparent;  
   
m_transparent.CreateIndirect(pBuffer);  
m_transparent.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER);  
m_transparent.SendMessageW(WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);//there's a loss of focus  

So I managed not to take focus away from the main window when creating a transparent window.
SetWindowPos also does not steal focus.
But after dragging the main window blinks.
How to prevent this.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,856 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 86,986 Reputation points
    2022-06-28T04:07:24+00:00

    Have you tried to add the style WS_EX_NOACTIVATE on creation ?


  2. Вадим Подгорный 41 Reputation points
    2022-06-28T13:36:15.587+00:00

    Castorix31 Can I see your window move code?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.