【发布时间】:2011-05-04 05:25:14
【问题描述】:
我只是在这里查看了一些帖子,但没有一个对我有帮助。
我想做的是运行屏幕捕获的后台进程。现在我想要一段代码,它可以为我提供 X、Y 或任何打开的活动/当前窗口(比如记事本)及其高度和宽度。
仅此而已。
【问题讨论】:
-
什么平台?银光? wpf?表格? ASP.NET?安慰? (等等)
-
这是您使用的 .net 版本。不是你的平台。 :)
我只是在这里查看了一些帖子,但没有一个对我有帮助。
我想做的是运行屏幕捕获的后台进程。现在我想要一段代码,它可以为我提供 X、Y 或任何打开的活动/当前窗口(比如记事本)及其高度和宽度。
仅此而已。
【问题讨论】:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
private IntPtr GetActiveWindow()
{
IntPtr handle = IntPtr.Zero;
return GetForegroundWindow();
}
然后用GetWindowRect获取窗口位置。
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
【讨论】:
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
static void Main()
{
SetProcessDPIAware();
Rectangle t2;
GetWindowRect(GetForegroundWindow(),out t2);
}
【讨论】: