在 C# 中获取活动窗口坐标和高度宽度

编程入门 行业动态 更新时间:2024-10-21 23:00:12
本文介绍了在 C# 中获取活动窗口坐标和高度宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I just check in some of the posts here, but none were helpful to me.

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

The thing I am trying to do is to run a background process of Screen Capturing. Now I want a piece of code that would give me the X, Y or any Active/Current Window opened (Say Notepad) and its Height and Width.

仅此而已.

推荐答案

[DllImport("user32.dll")]  
static extern IntPtr GetForegroundWindow();  


private IntPtr GetActiveWindow()  
{  
    IntPtr handle = IntPtr.Zero;  
    return GetForegroundWindow();  
}

然后使用 GetWindowRect 获取窗口位置.

Then get the window position with 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  
}

这篇关于在 C# 中获取活动窗口坐标和高度宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 23:31:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:坐标   宽度   高度   窗口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!