找到窗口高度和宽度

RV.*_*RV. 4 c#

如何找到聚焦窗口高度和宽度..

它可能是任何Windows窗口,如记事本,mspaint等...我可以借助此代码获得焦点窗口

[DllImport("user32")] 
public static extern IntPtr GetForegroundWindow();
Run Code Online (Sandbox Code Playgroud)

嗨f3lix它正在工作,但它的返回值只取决于位置..如果我改变位置它返回一些其他值

Kunal它的返回错误信息....就像没有设置对象参考

f3l*_*lix 9

我认为你必须通过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);
Run Code Online (Sandbox Code Playgroud)

注意:我没有尝试此代码,因为我目前不在Windows上工作...

编辑: Rory向我指出(见注释)我们不能在这里使用标准的Rectangle,我们需要定义自己的RECT.

[StructLayout(LayoutKind.Sequential)]
public struct RECT {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在第一段代码中用RECT替换Rectangle.


dom*_*mer 0

如果您正在构建 MDI 应用程序,您可以使用:

parentForm.ActiveMDIChild.Size
Run Code Online (Sandbox Code Playgroud)