日期:2014-05-17  浏览次数:21228 次

C# 如何获取系统活动窗口相对屏幕的位置以及大小
本人正用 C# 开发屏幕录制程序,允许用户指定任意窗口区域为录制范围,这就必须获得当前用户指定的窗口对象及其相对屏幕的位置和大小,固有此问。

------解决方案--------------------
算了。估计你也看不明白如何实现,写个例子给你好了。

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
public static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public static extern int GetWindowRect(IntPtr hwnd, ref System.Drawing.Rectangle lpRect);
private static void TestGetForegroundWindowRectangle()
{
    IntPtr hForeground = GetForegroundWindow();
    System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
    GetWindowRect(hForeground, ref rect);
    //这里的rect就是你你要的
}