How to show a form without giving it focus

B

Bob Altman

Hi all,

What is the "proper" way to display a form even if it is minimized, hidden,
or obscured by other windows, without giving the form input focus?

My (untested) guess looks kind of like this:

' Make the form visible
frm.Show()

' Restore the form if it's minimized
frm.WindowState = FormWindowState.Normal

' Temporarily set the TopMost property to bring it to the desktop Z-order
temp = frm.TopMost
frm.TopMost = True
frm.TopMost = temp
 
S

Scott M.

I would just bring it up and then give whatever is supposed to have the
focus, the focus.
 
L

Linda Liu [MSFT]

Hi Bob,
What is the "proper" way to display a form even if it is minimized,
hidden, or obscured by other windows, without giving the form input focus?

I am sorry that I couldn't understand what you mean by 'even if it is
minimized, hidde, or obscrured by other windows' in the above sentence.
Could you explain it more and tell me why you want that behavior?

When we show a form by calling the Show method, the form gets focused after
it is shown. If you'd like not to give the focus to the form, I think a
simple way is to set the focus to other window or control.

Let's say that there're two forms called Form1 and Form2. Form1 has a
button on it. When we click the button on Form1, Form2 is shown. To get the
focus from Form2, we could set the focus to the button.

The following is the sample code.

Dim frm as New Form2
frm.Show()
Button1.Focus()

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bob Altman

My application has a form that displays error messages that may generated
while the app is running. This form can be minimized (by clicking the
standard Windows minimize button at the top-right corner of the form). If
the user tries to close the form (by clicking the "x" at the top-right
corner of the form) then the form is hidden.

Now, when a critical error message is received by the form, it wants to
display itself in front of all other windows on the desktop. But, being the
polite form that it is, it doesn't want to take focus away from whatever
other window the user happens to be interacting with at the time.

The code that I posted with the original question attempted (unsuccessfully,
as it turns out) to display the form without taking focus. Here is that
code:

' Make the form visible
frm.Show()

' Restore the form if it's minimized
frm.WindowState = FormWindowState.Normal

' Temporarily set the TopMost property to bring it to the desktop Z-order
temp = frm.TopMost
frm.TopMost = True
frm.TopMost = temp

The first thing I need to do is to ensure that the form is visible via the
Show method. At this point, if the form was minimized, it's still minimized
(but now its icon is visible in the toolbar).

The next step appears to be at least part of my problem. If the form is
minimized then I need to set the form's WindowState to Normal. But doing so
(setting a minimized form's WindowState to Normal) causes the form to come
to the top of the desktop Z-order and take focus. (If the form's
WindowState is already Normal then setting the WindowState to Normal has no
effect on the form's position in the Z-order or its focus.)

Finally, if the form was Normal and visible, but obscured by other windows,
I need to bring it to the front of the Z-order. Temporarily setting the
form's TopMost property to true does this, but it seems like an ugly way to
do it.

- Bob
 
B

Bob Altman

Let me simplify the question: Suppose I have an application whose main form
is minimized to the taskbar. How can that application display its main form
in front of all other windows on the desktop without taking focus away from
whatever window the user happens to be interacting with at the time?

- Bob
 
L

Linda Liu [MSFT]

Hi Bob,

Thank you for your response and detailed explanation. I understand what you
really want now.

I performed a test based on your description and did reproduce the problem.
I think you need to make use of some Win32 APIs to get what you want.
Unfortunately, I haven't found them yet.

I will go on researching and will get the result back to you ASAP. I
appreciate your patience.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Bob,

When a form is minimized to the taskbar, we could call the Win32 API
function 'ShowWindow' passing the parameter 'SW_SHOWNOACTIVATE' to restore
the form without activating it.

Note that, the function 'ShowWindow' with the parameter 'SW_SHOWNOACTIVATE'
has no effect when the form's window state is normal.

However, the form will not be brought to front after we call the Win32 API
function 'ShowWindow'. To achieve this, we could call the Win32 API
function 'SetWindowPos' passing the parameter 'HWND_TOP_MOST', which has
the same effect of setting the form's TopMost property to true. If you
don't want the form to be always 'top most', you could call the function
'SetWindowPos' passing the parameter 'HWND_NOTOPMOST', which has the same
effect of setting the form's TopMost property to false.

The following is a sample.

using System.Runtime.InteropServices;

public partial class Form1 : Form
{
public static IntPtr HWND_TOP_MOST = (IntPtr)(-1);
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2);
public static uint SWP_NOMOVE = 0x2;
public static uint SWP_NOSIZE = 0x1;
public static int SW_SHOWNOACTIVATE = 4;

[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public void ShowWithoutActivating()
{
ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
// bring the form to the top most, alternatively, you may set the
TopMost property of the form to true
SetWindowPos(this.Handle, HWND_TOP_MOST, 0, 0, 0, 0, SWP_NOSIZE |
SWP_NOMOVE );
// not keep the form always top most, alternatively, you may set the
TopMost property of the form to false
SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE |
SWP_NOMOVE);
}
}

I have performed a test on the above code and confirmed it works.

If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top