How can I bring the application window to the fore?

How to bring the application window to the fore? For example, my application needs attention.

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

This is what I got. But it does NOT work 100% times.

public void BringToFrontToEnterCaptha() { if (InvokeRequired) { Invoke(new Action(BringToFrontToEnterCaptha)); } else { this.TopMost = true; this.Focus(); this.BringToFront(); this.textBox1.Focus(); this.textBox1.Text = string.Empty; System.Media.SystemSounds.Beep.Play(); } } public void BringToBackAfterEnterCaptha() { if (InvokeRequired) { Invoke(new Action(BringToBackAfterEnterCaptha)); } else { this.TopMost = false; } } 

And I call them from the background worker.

 BringToFrontToEnterCaptha(); while (!ready) { Thread.Sleep(100); } BringToBackAfterEnterCaptha(); Thread.Sleep(300); 

And after clicking the Accept button, bool ready is true.

I work great, but not always.

+63
c # winforms
Mar 12 2018-11-12T00:
source share
8 answers

Use Control.BringToFront :

 myForm.BringToFront(); 
+25
Mar 12 '11 at 12:55
source share

Here is a snippet of code that worked for me

 this.WindowState = FormWindowState.Minimized; this.Show(); this.WindowState = FormWindowState.Normal; 

It always displays the desired window in front of everyone else.

+128
Aug 13 '12 at 20:12
source share

As long as I agree with everyone, this is not good behavior, here is the code:

 [DllImport("User32.dll")] public static extern Int32 SetForegroundWindow(int hWnd); SetForegroundWindow(Handle.ToInt32()); 

Update

David is completely right, for completeness, I include a paragraph with a +1 mark for David!

  • The process is the front process.
  • The process was started foreground.
  • The process received the last entry of the event.
  • There is no foreground process.
  • The foreground process is debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock timeout has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • There is no menu.
+50
Mar 12 2018-11-12T00:
source share

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

+46
Mar 12 '11 at 12:56
source share

it works:

 if (WindowState == FormWindowState.Minimized) WindowState = FormWindowState.Normal; else { TopMost = true; Focus(); BringToFront(); TopMost = false; } 
+18
Nov 30 '12 at 13:56
source share

Before I stumbled upon this post, I came up with this solution - switch the TopMost property:

 this.TopMost = true; this.TopMost = false; 

I have this code in my form constructor, for example:

 public MyForm() { //... // Brint-to-front hack this.TopMost = true; this.TopMost = false; //... } 
+16
Jan 05 '13 at 17:24
source share

I use SwitchToThisWindow to bring the application to the forefront, as in this example:

 static class Program { [DllImport("User32.dll", SetLastError = true)] static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { bool createdNew; int iP; Process currentProcess = Process.GetCurrentProcess(); Mutex m = new Mutex(true, "XYZ", out createdNew); if (!createdNew) { // app is already running... Process[] proc = Process.GetProcessesByName("XYZ"); // switch to other process for (iP = 0; iP < proc.Length; iP++) { if (proc[iP].Id != currentProcess.Id) SwitchToThisWindow(proc[0].MainWindowHandle, true); } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new form()); GC.KeepAlive(m); } 
+4
Feb 19 '13 at 19:21
source share

My advice: do not do this! Applications requiring attention should flicker the taskbar , and not jump before what the user was doing, and start intercepting keys and mouseclicks. This is VERY bad form.

I was going to post this as a comment, but this is too important for the problem.

-3
Mar 12 2018-11-11T00:
source share



All Articles