获取窗口的边界握住手柄 [英] Get A Window's Bounds By Its Handle

查看:171
本文介绍了获取窗口的边界握住手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的高度和当前活动窗口的宽度。

I am trying to get the height and the width of the current Active window.

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, Rectangle rect);

Rectangle bonds = new Rectangle();
GetWindowRect(handle, bonds);
Bitmap bmp = new Bitmap(bonds.Width, bonds.Height);

这code不工作,因为我需要使用RECT,我不知道怎么办。

This code doesn't work because I need to use RECT and I don't know how.

推荐答案

像这样的事情很容易被谷歌(C#GetWindowRect)回答;你也应该知道pinvoke.net - 很好的资源,从C#调用原生API

Things like this are easily answered by google (C# GetWindowRect); you should also know about pinvoke.net -- great resource for calling native APIs from C#.

<一个href=\"http://www.pinvoke.net/default.aspx/user32/getwindowrect.html\">http://www.pinvoke.net/default.aspx/user32/getwindowrect.html

我猜我完整性这里应该复制了答案:

I guess for completeness I should copy the answer here:

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(HandleRef 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
        }

        Rectangle myRect = new Rectangle();

        private void button1_Click(object sender, System.EventArgs e)
        {
            RECT rct;

            if(!GetWindowRect(new HandleRef(this, this.Handle), out rct ))
            {
                MessageBox.Show("ERROR");
                return;
            }
            MessageBox.Show( rct.ToString() );

            myRect.X = rct.Left;
            myRect.Y = rct.Top;
            myRect.Width = rct.Right - rct.Left + 1;
            myRect.Height = rct.Bottom - rct.Top + 1;
        }

这篇关于获取窗口的边界握住手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆