【问题标题】:Getting Active Window Coordinates and Height Width in C#在 C# 中获取活动窗口坐标和高度宽度
【发布时间】:2011-05-04 05:25:14
【问题描述】:

我只是在这里查看了一些帖子,但没有一个对我有帮助。

我想做的是运行屏幕捕获的后台进程。现在我想要一段代码,它可以为我提供 X、Y 或任何打开的活动/当前窗口(比如记事本)及其高度和宽度。

仅此而已。

【问题讨论】:

  • 什么平台?银光? wpf?表格? ASP.NET?安慰? (等等)
  • 这是您使用的 .net 版本。不是你的平台。 :)

标签: c# window


【解决方案1】:
[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  
}

【讨论】:

  • 非常感谢。我现在只是要测试它:)
  • 'private Handle GetActiveWindow() '这给了 Handle 错误:/
【解决方案2】:
    [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);
    }

【讨论】:

  • 嗨,欢迎来到 stackoverflow。请详细描述答案。明确的答案将帮助人们理解您的意思,并增加选择作为答案的机会。
猜你喜欢
  • 2010-10-11
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多