C# 查找窗口高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/637819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find window Height & Width
提问by RV.
how to find focused window Height & Width ..
如何找到聚焦窗口的高度和宽度..
it might be any windows window like notepad,mspaint etc... i can get the focused window by help of this code
它可能是任何 Windows 窗口,如记事本、mspaint 等...我可以通过此代码获得焦点窗口
[DllImport("user32")]
public static extern IntPtr GetForegroundWindow();
hi f3lix it's working but it's return the value depends on the location only.. if i change the location it's return some other values
嗨 f3lix 它正在工作,但它返回的值仅取决于位置..如果我更改位置,它会返回一些其他值
Kunal it's return error msg....like object refrence not set
Kunal 它的返回错误 msg....like object refrence not set
回答by dommer
If you're building an MDI app, you could use:
如果您正在构建 MDI 应用程序,您可以使用:
parentForm.ActiveMDIChild.Size
回答by dommer
What exactly is your question?
你的问题究竟是什么?
- How to get the focused window?
- How to get the width and height of a Window?
- 如何获得焦点窗口?
- 如何获取窗口的宽度和高度?
If question 2, use Window.ActualWidth
and Window.ActualHeight
如果问题 2,使用Window.ActualWidth
和Window.ActualHeight
回答by dommer
This article will help you find the active application window:
本文将帮助您找到活动的应用程序窗口:
回答by f3lix
I think you have to use user32.dll functions via PInvoke. I'm not sure, but I would do it somewhat like this:
我认为您必须通过 PInvoke 使用 user32.dll 函数。我不确定,但我会这样做:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
Rectangle rect = new Rectangle();
GetWindowRect(GetForegroundWindow(), out rect);
Note:I did not try this code, because I am currently not working on Windows...
注意:我没有尝试这个代码,因为我目前不在 Windows 上工作......
EDIT: Rorypointed out to me (see comments) that we can't use the standard Rectangle here and we need to define our own RECT.
编辑: Rory向我指出(见评论)我们不能在这里使用标准矩形,我们需要定义我们自己的 RECT。
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
Don't forget to replace Rectanglewith RECTin the first piece of code.
不要忘记在第一段代码中用RECT替换Rectangle。
回答by Kunal S
If your window is inside your aplication, having an MDI app, you might use this, having
如果你的窗口在你的应用程序中,有一个 MDI 应用程序,你可以使用它,有
public static extern IntPtr GetForegroundWindow();
with you, try this
和你一起试试这个
int wHeight = Control.FromHandle(GetForegroundWindow()).Height;
int wWidth = Control.FromHandle(GetForegroundWindow()).Width;
回答by Balaji Annamalai
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
public static Size GetControlSize(IntPtr hWnd)
{
RECT pRect;
Size cSize = new Size();
// get coordinates relative to window
GetWindowRect(hWnd, out pRect);
cSize.Width = pRect.Right - pRect.Left;
cSize.Height = pRect.Bottom - pRect.Top;
return cSize;
}