2012-11-10 112 views
3

我有一個只在後臺運行的小程序(沒有任何窗口)。它監視按鍵,當滿足某些要求時,它會打開特定的程序。問題是程序的窗口沒有在前臺打開 - 它在當前活動的窗口後面打開。我如何強迫它在前臺打開?從後臺程序在前臺打開程序

我使用Visual Basic .NET(.NET框架4.5),這是我當前的代碼:

Dim temp As New Process 
temp = Process.Start("C:\cygwin\bin\mintty.exe", "-") 
temp.WaitForInputIdle(10000) 
+0

您需要調用SetForegroundWindow()。獲得你需要的窗口處理應該是相當具有挑戰性的。當Windows拒絕請求時它也不起作用,因爲它檢測到用戶正在使用另一個窗口。你不能把窗戶推到用戶的臉上。 –

回答

5

試試這個(在C#它):從this site服用。

internal class Program 
{ 
    [DllImport("user32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool SetWindowPos(IntPtr hWnd, 
     int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags); 

    private const int HWND_TOPMOST = -1; 
    private const int SWP_NOMOVE = 0x0002; 
    private const int SWP_NOSIZE = 0x0001; 

    public static void Main() 
    { 
     Process process = Process.Start(@"notepad.exe", ""); 

     if (null != process) 
     { 
      SetWindowPos(process.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); 
     } 
    } 
} 
+0

此代碼不會**在前臺放置一個窗口。它僅僅與用戶正在使用的窗口重疊,所以她不能再看到她正在輸入的內容。 –

+0

@HansPassant我沒有得到你。我知道這個計劃將會在所有其他被大肆化的計劃中看到,這會導致焦點從目前的計劃中失去焦點。 –

+0

無論windodw狀態如何,都不會丟失焦點,但此代碼不會改變焦點。你爲什麼不試試呢? –