2016-08-16 185 views
-1

我正在開發一個應用程序,它在前臺有一個Windows資源管理器窗口時觸發。我的應用程序觸發一個窗口(窗體),它將被放置在靠近打開的Windows資源管理器的屏幕上(計劃將其保留在搜索選項的下方)。如何使用.net獲取Windows資源管理器的位置?

但我沒有得到任何東西來獲得前臺「窗口資源管理器」窗口的窗口位置。

是否有任何方式使用.net讀取當前前臺「Windows資源管理器」窗口的位置?

+0

有本地的Win32 API爲您查詢正在運行的進程和他們的窗戶,所以你應該尋找類似的帖子,然後PInvoke的。 –

+0

@LexLi我已經搜索,但我沒有得到任何方法來檢索Windows資源管理器的窗口位置。 –

回答

2

您可以通過使用非託管代碼來完成此操作。

創建一個類:

class RectMethods 
    { 
     // http://msdn.microsoft.com/en-us/library/ms633519(VS.85).aspx 
     [DllImport("user32.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); 

     // http://msdn.microsoft.com/en-us/library/a5ch4fda(VS.80).aspx 
     [StructLayout(LayoutKind.Sequential)] 
     public struct RECT 
     { 
      public int Left; 
      public int Top; 
      public int Right; 
      public int Bottom; 
     } 
    } 

然後,確定explorer進程拿起你的手柄,並採取座標和窗口的大小,並從那裏你可以做你想做什麼:

  var processes = System.Diagnostics.Process.GetProcesses(); 
      foreach (var process in processes) 
      { 
       if (process.ProcessName == "explorer") 
       { 
        var hWnd = process.Handle; 
        RectMethods.RECT rect = new RectMethods.RECT(); 
        if (RectMethods.GetWindowRect(hWnd, ref rect)) 
        { 
         Size size = new Size(rect.Right - rect.Left, 
           rect.Bottom - rect.Top); 
        } 
       } 
      } 

設置「允許不安全的代碼」的屬性/生成...

相關問題