Get A Window's Bounds By Its Handle

后端 未结 2 1474
名媛妹妹
名媛妹妹 2020-12-06 05:38

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

[DllImport(\"user32.dll\", CharSet = CharSet.Auto, ExactSpelling = true)]
public st         


        
2条回答
  •  时光取名叫无心
    2020-12-06 05:56

    Of course that code will not work. It has to be this way: GetWindowRect(handle, ref rect);. So, edit your GetWindowRect declaration. And Rectangle is just a wrapper of the native RECT. Rectangle and RECT has left, top, right and bottom fields that the Rectangle class changed to read-properties (Left, Top, Right, Bottom). Width is not equivalent to right and Height is not equivalent to bottom. Width is right-left and Height is bottom-top. Of course, RECT don't have these sort of properties. It's just a bare struct.

    Creating RECT is an overkill. Rectangle is enough in .NET for native/unmanaged API that need it. You just have to pass it in the appropriate way.

提交回复
热议问题