代码之家  ›  专栏  ›  技术社区  ›  G-Man

获取特定窗口的边界

  •  1
  • G-Man  · 技术社区  · 15 年前

    我有以下代码:

    system.drawing.rectangle desktop_rectangle=system.windows.forms.screen.primaryscreen.bounds

    这给了我桌面的界限。

    我现在正在使用窗口的标题获取特定窗口的边界。

    为了实现这一点,我必须使用interop吗?

    任何示例代码都将非常感谢。

    谢谢你

    2 回复  |  直到 15 年前
        1
  •  4
  •   Mark H    15 年前
    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();
            }
        }
    }
    

    用途:

    var handle = User32.FindWindowEx(IntPtr.Zero, IntPtr.Zero, String.Empty, "My Caption");
    var rect = User32.GetClientRect(handle);
    
        2
  •  0
  •   dkackman Srinivas Kokkula    15 年前