Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to open a popup window but without make it focused and active?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Oct 2, 2014 8:53 AM Last Post By: Jean-Milost Rey... Threads: [ Previous | Next ]
Jean-Milost Rey...

Posts: 23
Registered: 11/8/11
How to open a popup window but without make it focused and active?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2014 7:56 AM
Hello,

My application main form contains a tree component that should open a popup window when I right click on it, however the tree MUST keep the focus, even after my popup window is opened and visible. The behavior of this window must be similar to popup menu windows, as it should be shown but don't accept the focus, and not become active until the mouse cursor hover it. For now, I'm able to open a popup window when I right click my tree component, however the newly opened popup window become active and keep the focus immediately, at the expense of my tree component, even if I configure my popup window with the WS_EX_NOACTIVATE style. IMPORTANT NOTE: I open my window using the Show() function AND NOT the ShowModal(). The problem isn't that my main form isn't accessible, but really that my tree component looses the focus and need to be clicked to receive it again.

So I want to know how to create or configure a TForm object to not receive the focus and not become active when I open it, as occurs with a popup menu window. Is there a way to do this?

Regards
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to open a popup window but without make it focused and active?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2014 9:13 AM   in response to: Jean-Milost Rey... in response to: Jean-Milost Rey...
Jean-Milost wrote:

NOTE: I open my window using the Show() function AND NOT
the ShowModal().

That is your problem. You need to skip Show() and use the Win32 API ShowWindow()
function directly. That way, you can specify the SW_SHOWNOACTIVATE flag.

--
Remy Lebeau (TeamB)
Jean-Milost Rey...

Posts: 23
Registered: 11/8/11
Re: How to open a popup window but without make it focused and active?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 30, 2014 8:41 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,

Thanks you for your answer.

Remy Lebeau (TeamB) wrote:
You need to skip Show() and use the Win32 API ShowWindow() function directly.

I tried the following code to open my window
    ::SetWindowPos(pForm->Handle, 0, x, y, pForm->Width, pForm->Height, SWP_NOZORDER | SWP_NOACTIVATE);
    ::ShowWindow(pForm->Handle, SW_SHOWNOACTIVATE);


Also, I tried to modify the exStyle parameters of my Window, as follow
void __fastcall TMyPopupForm::CreateParams(TCreateParams& params)
{
    inherited::CreateParams(params);
    params.ExStyle = params.ExStyle | (WS_EX_TOPMOST | WS_EX_NOACTIVATE);
}


Unfortunately it did not work in my case. My window did not appear at all. An important thing is that my popup window is layered. Is that it can cause such problems?

Regards
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to open a popup window but without make it focused and active?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 30, 2014 9:21 AM   in response to: Jean-Milost Rey... in response to: Jean-Milost Rey...
Jean-Milost wrote:

An important thing is that my popup window is layered. Is that
it can cause such problems?

Do you, by chance, have its alpha set to 0?

--
Remy Lebeau (TeamB)
Jean-Milost Rey...

Posts: 23
Registered: 11/8/11
Re: How to open a popup window but without make it focused and active?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 2, 2014 8:47 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,

Thank you again for your reply.

Remy Lebeau (TeamB) wrote:
Do you, by chance, have its alpha set to 0?

I think not. In fact, I declare my form layered using SetWindowLong, as in following code:
::SetWindowLong(Handle, GWL_EXSTYLE, ::GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);


Then I draw my form using this code:
bool __fastcall MyLayeredForm::OnPaintLayeredWindow()
{
    // check if overlay is already created
    if (!m_pOverlay.get())
        return false;
 
    // update overlay width and height
    m_pOverlay->Width  = Width;
    m_pOverlay->Height = Height;
 
    // cleanup previous draw completely (IMPORTANT alpha channel must be cleaned too)
    if (!WGDIPlusTools::Clear(m_pOverlay.get()))
        return false;
 
    // is window visible?
    if (Visible)
        // draw form content in overlay
        Draw(m_pOverlay->Canvas);
 
    TPoint pos(0, 0);
    TSize  size(Width, Height);
 
    // configure alpha blender
    ::BLENDFUNCTION bf     = {0};
    bf.AlphaFormat         = AC_SRC_ALPHA;
    bf.SourceConstantAlpha = 255;
    bf.BlendFlags          = 0;
    bf.BlendOp             = AC_SRC_OVER;
 
    // update layered window with overlay content
    return ::UpdateLayeredWindow(Handle, NULL, NULL, &size, m_pOverlay->Canvas->Handle, &pos, 0,
            &bf, ULW_ALPHA);
}


In the code above, I actually clear my alpha channel, but the Draw() function called inside draws a lot of transparent and opaque shapes, so I suppose that finally my alpha channel contains something. It's right?

When I show my form using the TForm::Show() method, all is fine and I can show it without problems, my only concern is that the form thus opened takes the focus and is activated. However, when I try to use the Window API ::ShowWindow() function, my window not appear at all, by against the main window keeps the focus and is not deactivated.

Do you know why?

Regards

Edited by: Jean-Milost Reymond on Oct 2, 2014 8:48 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02