2011-03-12 131 views
56

如何將我的應用程序窗口放在前面?例如我的應用程序需要注意。如何將我的應用程序窗口置於前面?

這是我的個人程序。我需要這個功能。

這就是我得到的。但是這是不是工作100%次。

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; 
    } 
} 

我從後臺工作者打電話給他們。

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

並按下「接受」按鈕後,布爾就緒設置爲true。

我工作很好,但並不總是如此。

+0

當您的應用程序確定需要關注時,是否有輸入焦點? – 2011-03-12 13:47:45

+0

輸入焦點怎麼樣?沒有它,你不能輕鬆地將你的應用程序放在前面。 – 2011-03-12 14:45:25

+0

我按照建議添加了這個評論:我害怕你的「個人」程序逃避世界並迫使自己進入一些不知情的用戶。如果你需要這個,也許你的設計需要審查... – 2014-01-02 18:21:26

回答

21

使用Control.BringToFront

myForm.BringToFront(); 
+5

如果應用程序沒有輸入焦點,這是否工作? – 2011-03-12 13:50:50

+7

這不適合我。 – 2014-09-12 09:50:05

+1

@JonCage嘗試'Application.OpenForms [「yourForm」]。BringToFront();' – 2015-06-12 09:18:46

42

使用Form.Activate()Form.Focus()方法。

+0

我在我的Form1_Load函數中使用了Focus(),但它不起作用(使用Windows 8.1) – Simon 2014-04-25 07:22:41

+12

Form.Activate()在它自己的工作 - 謝謝! – 2014-09-12 09:51:48

+1

Activate是唯一一個爲我工作的人,謝謝! – 2014-10-02 11:06:17

-3

我的建議是:不要這樣做!需要注意的應用程序應爲flicker the taskbar,不要跳到用戶所做的任何操作前,並開始攔截鍵和鼠標點擊。這是非常糟糕的形式。

我打算把它作爲評論發佈,但這對於一個問題來說太重要了。

+14

這是我的個人項目。我需要這個功能。 – Hooch 2011-03-12 14:15:54

+9

這是爲我個人私人使用。只要幫助我。 – Hooch 2011-03-12 23:16:29

+3

@Vincent:需要注意的警告應該作爲評論發佈,而不是跳到解決方案前面,無論用戶在尋找什麼,並攔截使得他們滑出焦點的惡意提議......; P這是不好的Q&A使用。 (ps我沒有投票) – Superole 2014-01-02 14:15:46

45

雖然我與大家同意,這是沒有很好的行爲,這裏是代碼:

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


SetForegroundWindow(Handle.ToInt32()); 

更新

大衛是完全正確的,對於完整性我在這裏有子彈點+1爲大衛!

  • 該過程是前臺 過程。
  • 該過程由前臺進程 開始。
  • 該進程收到最後一次輸入 事件。
  • 沒有前臺進程。
  • 前臺進程正在調試 。
  • 前景未鎖定(請參閱 LockSetForegroundWindow)。
  • 前臺鎖定超時有 已過期(請參閱 SPI_GETFOREGROUNDLOCKTIMEOUT SystemParametersInfo)。
  • 沒有菜單處於活動狀態。
+4

閱讀必須適用於此功能的[條件](http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx)的項目符號列表。 – 2011-03-12 15:10:21

+8

Form.Activate()調用SetForegroundWindow(),避免使用PInvoke。 – 2012-06-12 14:58:13

+0

它像一個魅力與我的啓動畫面,thx :) – 2015-02-12 11:20:56

112

下面是一段代碼,爲我工作

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

它總是帶給所有其他的前所需的窗口。

+6

我使用Google發現的所有方法,以及每個人不斷堅持激活和關注的關鍵所在。這是爲我工作的唯一方法。謝謝,因爲我永遠不會想到這個伎倆。 – cgTag 2012-09-20 18:48:24

+0

謝謝。在迄今爲止我找到的所有解決方案中,這是唯一一個似乎在所有時間都能正常工作的解決方案。 +1 – Alex 2013-08-12 09:54:59

+0

適用於WPF應用程序(與Activate()方法相反) – Illidan 2013-09-02 12:09:38

18

這個工程:

if (WindowState == FormWindowState.Minimized) 
    WindowState = FormWindowState.Normal; 
else 
{ 
    TopMost = true; 
    Focus(); 
    BringToFront(); 
    TopMost = false; 
} 
+0

在這裏的所有建議中,這是唯一爲我工作的人。 :) – MojoDK 2013-08-30 08:56:47

+0

沒有爲我工作。 – 2014-09-12 09:50:24

+0

你可以寄給我你的代碼嗎? – lupok 2014-09-12 12:52:56

17

前絆倒在這篇文章中,我想出了這個解決方案 - 可切換TopMost屬性:

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

我在窗體的構造函數,例如這個代碼:

public MyForm() 
{ 
    //... 

    // Brint-to-front hack 
    this.TopMost = true; 
    this.TopMost = false; 

    //... 
} 
+1

在極少數情況下,這可能會導致藍屏死機,即其他窗口最高級別的程序(如虛擬/多個桌面應用程序)。 – 2016-01-07 17:16:59

+0

Nop。 http://stackoverflow.com/questions/17964598/can-a-user-mode-fault-cause-blue-screen-of-dead – lerthe61 2016-12-14 14:44:45

+0

這應該是答案。 – 2017-11-01 16:12:42

4

我使用SwitchToThisWindow將應用程序帶到最前面,如下例所示:

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); 

    } 
相關問題