2012-01-12 86 views
0

使用C#,我想帶一個特定的窗口到屏幕的頂部,然後我會運行一個宏。如何獲得應用程序窗口頂部

試過,

[DllImportAttribute("User32.dll")] 
private static extern int FindWindow(String ClassName, String WindowName); 
[DllImportAttribute("User32.dll")] 
private static extern int SetForegroundWindow(int hWnd); 

後,

 string main_title; 
     Process[] processlist = Process.GetProcesses(); 
     foreach (Process theprocess in processlist) 
     { 
      if (theprocess.ProcessName.StartsWith(name)) 
      { 
       main_title = theprocess.MainWindowTitle; 
       hWnd = theprocess.Handle.ToInt32(); 
       break; 
      } 
      hWnd = FindWindow(null, main_title); 
      if (hWnd > 0) 
      { 
       SetForegroundWindow(hWnd); 
      } 

,並得到了錯誤,

'DllImportAttribute' 找不到

使用的類型或命名空間名稱,

using System.Runtime; 

,現在即時通訊無法得到的HWND在我的代碼的認可,儘管他們都在同一個命名空間和部分類。

後試驗,

IntPtr hWnd; 
    Process[] processlist = Process.GetProcesses(); 
    foreach (Process theprocess in processlist) 
    { 
     if (theprocess.ProcessName.StartsWith("msnmsgr")) 
     { 
      main_title = theprocess.MainWindowTitle; 
      hWnd = theprocess.MainWindowHandle; 
      SetForegroundWindow(hWnd); 

     } 
    } 

這看起來像重點講的窗口,至少按ALT-Tab鍵順序和應用程序看起來像工具欄中選擇,但它不會成爲可見。

+0

看來,你沒有在使用前定義的hWnd變量。只需在代碼的開頭添加「int hWnd;」。 – 2012-01-12 18:23:48

回答

1

嘗試使用的DllImport代替,就像這樣:

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

[DllImport("user32.DLL")] 
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow); 

定義的hWnd喜歡:

IntPtr hWnd; 

在你的作業做:

hWnd = theProcess.MainWindowHandle; // or use your theProcess.Handle 

而且,在你的聲明,如果突破如果發現它,不會將窗口設置爲前景。你需要重新工作的代碼段是這樣的:

if (theprocess.ProcessName.StartsWith(name)) 
{ 
    main_title = theprocess.MainWindowTitle; 
    hWnd = theProcess.MainWindowHandle; 
} 
else 
    hWnd = FindWindow(null, main_title); 

if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) 
{ 
    SetForegroundWindow(hWnd); 
    // may want to put your break here 
} 

窗口類

的Win32僅僅是一個包含類的所有的DllImport的上述,我在多個Win32的子類使用它們。

public class Window : Win32 
{ 
    public IntPtr Handle; 

    public Window(IntPtr handle) 
    { 
     Handle = handle; 
    } 

    public bool Visible 
    { 
     get { return IsWindowVisible(Handle); } 
     set 
     { 
      ShowWindow(Handle, value ? ShowWindowConsts.SW_SHOW : 
       ShowWindowConsts.SW_HIDE); 
     } 
    } 

    public void Show() 
    { 
     Visible = true; 
     SetForegroundWindow(Handle); 
     /* 
     try { SwitchToThisWindow(Handle, false); } // this is deprecated - may throw on new window platform someday 
     catch { SetForegroundWindow(Handle); } // 
     */ 
    } 

    public void Hide() { Visible = false; } 
} 

ShowWindowConsts類

namespace Win32 
{ 
    public class ShowWindowConsts 
    { 
     // Reference: http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx 

     /// <summary> 
     /// Minimizes a window, even if the thread that owns the window is not responding. 
     /// This flag should only be used when minimizing windows from a different thread. 
     /// </summary> 
     public const int SW_FORCEMINIMIZE = 11; 

     /// <summary> 
     /// Hides the window and activates another window. 
     /// </summary> 
     public const int SW_HIDE = 0; 

     /// <summary> 
     /// Maximizes the specified window. 
     /// </summary> 
     public const int SW_MAXIMIZE = 3; 

     /// <summary> 
     /// Minimizes the specified window and activates the next top-level window in the Z order. 
     /// </summary> 
     public const int SW_MINIMIZE = 6; 

     /// <summary> 
     /// Activates and displays the window. 
     /// If the window is minimized or maximized, the system restores it to 
     /// its original size and position. 
     /// An application should specify this flag when restoring a minimized window. 
     /// </summary> 
     public const int SW_RESTORE = 9; 

     /// <summary> 
     /// Activates the window and displays it in its current size and position. 
     /// </summary> 
     public const int SW_SHOW = 5; 

     /// <summary> 
     /// Sets the show state based on the public const long SW_ value specified in 
     /// the STARTUPINFO structure passed to the CreateProcess function by 
     /// the program that started the application. 
     /// </summary> 
     public const int SW_SHOWDEFAULT = 10; 

     /// <summary> 
     /// Activates the window and displays it as a maximized window. 
     /// </summary> 
     public const int SW_SHOWMAXIMIZED = 3; 

     /// <summary> 
     /// Activates the window and displays it as a minimized window. 
     /// </summary> 
     public const int SW_SHOWMINIMIZED = 2; 

     /// <summary> 
     /// Displays the window as a minimized window. 
     /// This value is similar to public const long SW_SHOWMINIMIZED, 
     /// except the window is not activated. 
     /// </summary> 
     public const int SW_SHOWMINNOACTIVE = 7; 

     /// <summary> 
     /// Displays the window in its current size and position. 
     /// This value is similar to public const long SW_SHOW, except that the window is not activated. 
     /// </summary> 
     public const int SW_SHOWNA = 8; 

     /// <summary> 
     /// Displays a window in its most recent size and position. 
     /// This value is similar to public const long SW_SHOWNORMAL, 
     /// except that the window is not activated. 
     /// </summary> 
     public const int SW_SHOWNOACTIVATE = 4; 

     public const int SW_SHOWNORMAL = 1; 
    } 
} 

所以,你的代碼將變成:

if (theprocess.ProcessName.StartsWith(name)) 
{ 
    main_title = theprocess.MainWindowTitle; 
    hWnd = theProcess.MainWindowHandle; 
} 
else 
    hWnd = FindWindow(null, main_title); 

if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) 
{ 
    new Window(hWnd).Show(); 
} 
+0

編輯了代碼,看起來像我們去某個地方,但我不能使窗口可見 – Bartu 2012-01-12 20:04:20

+0

使可見不同於向前。我將編輯代碼以顯示該部分。 – 2012-01-12 20:16:44

+0

等待它,謝謝,與這些相關的任何堅實的C#教程? – Bartu 2012-01-12 20:31:17

相關問題