2010-06-17 5 views
1

Je le code suivant:Obtenir Bounds d'une fenêtre spécifique

System.Drawing.Rectangle desktop_Rectangle = System.Windows.Forms.Screen.PrimaryScreen.Bounds

qui me donne les limites du bureau.

Je cherche maintenant à obtenir les limites d'une fenêtre spécifique en utilisant la légende de la fenêtre.

Dois-je utiliser Interop pour accomplir cela?

tout exemple de code serait grandement apprécié.

Merci

Répondre

4
namespace NativeInterop { 
    using System.Runtime.InteropServices; 

    public static partial class User32 { 
     private const string DLL_NAME = "user32.dll"; 

     [StructLayout(LayoutKind.Sequential)] 
     private struct RECT { 
      int left, top, right, bottom; 

      public Rectangle ToRectangle() { 
       return new Rectangle(left, top, right - left, bottom - top); 
      } 
     } 

     [DllImport(DLL_NAME, SetLastError = true)] 
     public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, String className, String windowTitle); 

     [DllImport(DLL_NAME)] 
     private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); 

     public static Rectangle GetClientRect(IntPtr hWnd) { 
      var nativeRect = new RECT(); 
      GetClientRect(hWnd, out nativeRect); 
      return nativeRect.ToRectangle(); 
     } 
    } 
} 

utilisation:

var handle = User32.FindWindowEx(IntPtr.Zero, IntPtr.Zero, String.Empty, "My Caption"); 
var rect = User32.GetClientRect(handle); 
+0

je devais appeler FindWindowEx avec null au lieu de String.Empty. Et quand j'appelle GetClientRect() les valeurs de gauche et de haut (x et y) sont toujours 0. Toutes les idées ce qui cause ceci? – Lars

Questions connexes