use*_*444 11 c# graphics winapi
我试图获取当前活动窗口的高度和宽度.
[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);
此代码不起作用,因为我需要使用RECT,我不知道如何.
MK.*_*MK. 28
这样的事情很容易被谷歌回答(C#GetWindowRect); 您还应该了解pinvoke.net - 从C#调用本机API的绝佳资源.
http://www.pinvoke.net/default.aspx/user32/getwindowrect.html
我想完整性我应该在这里复制答案:
        [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;
            myRect.Height = rct.Bottom - rct.Top;
        }