Bring window to front windows

Bring window to front windows

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

With a Windows Forms form i call: this.BringToFront()
Whats the equivalent in WPF?

Thank you, regards

Answers

is this what you are looking for?

All replies

is this what you are looking for?

Topmost is equivalent to «Always On Top», which is not the same is BringToFront.

Calling this.Focus() will bring the window to the top, if it’s not already in focus.

I can’t get an existing window to come to the front, using any of the suggestions (3.5sp1)

When the modeless window is initially created, I set Topmost to true, and then set it back to false after the window is rendered. That seems to work as far as getting the window to initially show over its owner, and to not retain the Topmost behavior.

Another variable is setting the Owner property on the modeless window when it is created. (This is required if you want CenterOwner to work for a startup location). I find that when the window has an Onwer set, it gives something very much like Topmost behavior — the modeless window is always on top of its owner. Not what I want.

How can I bring my application window to the front? [duplicate]

How to bring my application window to front? For example whan my app needs attention.

This is for my personal program. I need that functionality.

This is what I got. But it’s NOT working 100% times.

And I call them from background worker.

And after pressing «Accept» button bool ready is set to true.

I works great but not always.

7 Answers 7

Here is a piece of code that worked for me

It always brings the desired window to the front of all the others.

Use Form.Activate() or Form.Focus() methods.

While I agree with everyone, this is no-nice behavior, here is code:

Update

David is completely right, for completeness I include the list of conditions that must apply for this to work (+1 for David!):

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

Javascript Bring window to front if already open in window.open?

If you open a window like:

If winName is already open it just changes the URL in the window. This is ok but if that window is behind the current window most users won’t realize this and think that it is just not opening.

Is there any way that if the window is already open, it brings it to the front?

8 Answers 8

Update: This hasn’t worked since Chrome (21+). The workaround is to close/reopen.

Читайте также:  Total commander последняя версия для windows 10

The window.open() method returns an object that represents the new window. You just need to window.focus() it:

The various answers suggesting using any form of .focus() are likely not going to work in all browsers.

This used to work back in the day but not any more, mainly due to browsers working to actively stop shady ad networks from pushing their popup ads to the foreground.

In Mozilla Firefox in particular (depending on your version) there is a configuration setting that is turned on by default that stops other windows (e.g. popups) from focusing themselves.

You can find this setting in the about:config page (tread carefully!)

If I recall correctly this setting used to be called something like

Other browsers may implement something similar, but quite simply if 1 of the major browsers blocks the use of .focus() by default then there’s not much use in attempting to call it.

As a result, the only solution I’ve seen that works is to see if the window exists, and is not already closed. and if so close it, then load the window you want.

when opening your popup if there’s a chance it is already open (and burried behind other windows) then you can call this function before you attempt to open your popup.

How to bring a window to the front?

We have a Java application that needs to be brought to the foreground when a telecontrol mechanism activates something in the application.

In order to get this, we have realized in the called method of the class which represents the frame of our application (extension of a JFrame ) following implementation:

Under Windows XP, this works the first time it is called, on the second time only the tab in the taskbar flashes, the frame doesn’t come to the front anymore. Same goes for Win2k. On Vista it seems to work fine.

Do you have any ideas?

11 Answers 11

A possible solution is:

I had the same problem with bringing a JFrame to the front under Ubuntu (Java 1.6.0_10). And the only way I could resolve it is by providing a WindowListener . Specifically, I had to set my JFrame to always stay on top whenever toFront() is invoked, and provide windowDeactivated event handler to setAlwaysOnTop(false) .

So, here is the code that could be placed into a base JFrame , which is used to derive all application frames.

Whenever your frame should be displayed or brought to front call frame.setVisible(true) .

Since I moved to Ubuntu 9.04 there seems to be no need in having a WindowListener for invoking super.setAlwaysOnTop(false) — as can be observed; this code was moved to the methods toFront() and setVisible() .

Please note that method setVisible() should always be invoked on EDT.

Windows has the facility to prevent windows from stealing focus; instead it flashes the taskbar icon. In XP it’s on by default (the only place I’ve seen to change it is using TweakUI, but there is a registry setting somewhere). In Vista they may have changed the default and/or exposed it as a user accessible setting with the out-of-the-box UI.

Preventing windows from forcing themselves to the front and taking focus is a feature since Windows 2K (and I, for one, am thankful for it).

That said, I have a little Java app I use to remind me to record my activities while working, and it makes itself the active window every 30 minutes (configurable, of course). It always works consistently under Windows XP and never flashes the title bar window. It uses the following code, called in the UI thread as a result of a timer event firing:

Читайте также:  Ноутбук не форматирует флешку что делать windows 10

(the first line restores if minimized. actually it would restore it if maximized too, but I never have it so).

While I usually have this app minimized, quite often it’s simply behind my text editor. And, like I said, it always works.

I do have an idea on what your problem could be — perhaps you have a race condition with the setVisible() call. toFront() may not be valid unless the window is actually displayed when it is called; I have had this problem with requestFocus() before. You may need to put the toFront() call in a UI listener on a window activated event.

2014-09-07: At some point in time the above code stopped working, perhaps at Java 6 or 7. After some investigation and experimentation I had to update the code to override the window’s toFront method do this (in conjunction with modified code from what is above):

As of Java 8_20, this code seems to be working fine.

Bring a window to the front in WPF

How can I bring my WPF application to the front of the desktop? So far I’ve tried:

None of which are doing the job ( Marshal.GetLastWin32Error() is saying these operations completed successfully, and the P/Invoke attributes for each definition do have SetLastError=true ).

If I create a new blank WPF application, and call SwitchToThisWindow with a timer, it works exactly as expected, so I’m not sure why it’s not working in my original case.

Edit: I’m doing this in conjunction with a global hotkey.

18 Answers 18

Attempts to bring the window to the foreground and activates it.

That should do the trick, unless I misunderstood and you want Always on Top behavior. In that case you want:

I have found a solution that brings the window to the top, but it behaves as a normal window:

In case you need the window to be in front the first time it loads then you should use the following:

I know this question is rather old, but I’ve just come across this precise scenario and wanted to share the solution I’ve implemented.

As mentioned in comments on this page, several of the solutions proposed do not work on XP, which I need to support in my scenario. While I agree with the sentiment by @Matthew Xavier that generally this is a bad UX practice, there are times where it’s entirely a plausable UX.

The solution to bringing a WPF window to the top was actually provided to me by the same code I’m using to provide the global hotkey. A blog article by Joseph Cooney contains a link to his code samples that contains the original code.

I’ve cleaned up and modified the code a little, and implemented it as an extension method to System.Windows.Window. I’ve tested this on XP 32 bit and Win7 64 bit, both of which work correctly.

I hope this code helps others who encounter this problem.

To make this a quick copy-paste one —
Use this class’ DoOnProcess method to move process’ main window to foreground (but not to steal focus from other windows)

If the user is interacting with another application, it may not be possible to bring yours to the front. As a general rule, a process can only expect to set the foreground window if that process is already the foreground process. (Microsoft documents the restrictions in the SetForegroundWindow() MSDN entry.) This is because:

  1. The user «owns» the foreground. For example, it would be extremely annoying if another program stole the foreground while the user is typing, at the very least interrupting her workflow, and possibly causing unintended consequences as her keystrokes meant for one application are misinterpreted by the offender until she notices the change.
  2. Imagine that each of two programs checks to see if its window is the foreground and attempts to set it to the foreground if it is not. As soon as the second program is running, the computer is rendered useless as the foreground bounces between the two at every task switch.
Читайте также:  Что дает windows 10 home

Why some of the answers on this page are wrong!

Any answer that uses window.Focus() is wrong.

  • Why? If a notification message pops up, window.Focus() will grab the focus away from whatever the user is typing at the time. This is insanely frustrating for end users, especially if the popups occur quite frequently.

Any answer that uses window.Activate() is wrong.

  • Why? It will make any parent windows visible as well.
  • Any answer that omits window.ShowActivated = false is wrong.
    • Why? It will grab the focus away from another window when the message pops up which is very annoying!
  • Any answer that does not use Visibility.Visible to hide/show the window is wrong.
    • Why? If we are using Citrix, if the window is not collapsed when it is closed, it will leave a weird black rectangular hold on the screen. Thus, we cannot use window.Show() and window.Hide() .
    • The window should not grab the focus away from any other window when it activates;
    • The window should not activate its parent when it is shown;
    • The window should be compatible with Citrix.

    MVVM Solution

    This code is 100% compatible with Citrix (no blank areas of the screen). It is tested with both normal WPF and DevExpress.

    This answer is intended for any use case where we want a small notification window that is always in front of other windows (if the user selects this in the preferences).

    If this answer seems more complex than the others, it’s because it is robust, enterprise level code. Some of the other answers on this page are simple, but do not actually work.

    XAML — Attached Property

    Add this attached property to any UserControl within the window. The attached property will:

    • Wait until the Loaded event is fired (otherwise it cannot look up the visual tree to find the parent window).
    • Add an event handler that ensures that the window is visible or not.

    At any point, you can set the window to be in front or not, by flipping the value of the attached property.

    C# — Helper Method

    Usage

    In order to use this, you need to create the window in your ViewModel:

    For tips on how ensure that a notification window always shifts back onto the visible screen, see my answer: In WPF, how to shift a window onto the screen if it is off the screen?.

    Оцените статью