我刚刚查看了这里的一些帖子,但没有一个对我有用。
我想要做的事情是运行一个屏幕捕捉的后台进程。现在我需要一段代码,可以给我当前打开的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);
}