如何获取客户端的窗口大小

时间:2014-06-12 02:07:08

标签: c# windows winforms

所以我是C#和Visual Basic的新手。 我想要获得客户规模。基本上我正在尝试找到一个函数,它将以单独的数组或x + y大小返回客户端大小。

我不想让我的桌面或我的应用程序大小。我想做这样的事情:

var size = WinGetSize("RandomWindow");

然后玩大小。

任何帮助?

1 个答案:

答案 0 :(得分:3)

好的,你需要win api,这是一个例子(c#的代码):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]

        [return: MarshalAs(UnmanagedType.Bool)]

        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]

        public struct RECT
        {

            public int Left; 

            public int Top; 

            public int Right; 

            public int Bottom; 

        }

        static void Main(string[] args)
        {
            string WinTitle = ""; //windows title
            IntPtr awin = FindWindow(null, WinTitle);
            if (awin != IntPtr.Zero)
            {
                RECT rc = new RECT();

                GetWindowRect(awin, ref rc);

                int width = rc.Right - rc.Left; 

                int height = rc.Bottom - rc.Top; 

                int x = rc.Left;

                int y = rc.Top;

                Console.WriteLine(string.Format("width:{0} height:{1} left:{2} top:{3}", width, height, x, y));
            }
            else
            {
                Console.WriteLine("can not find the window");
            }
        }
    }
}