RECT结构与Rectangle结构? [英] RECT struct vs Rectangle struct?

查看:81
本文介绍了RECT结构与Rectangle结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Windows API函数GetWindowRect,传入的参数之一是RECT类型,定义为:

 结构 RECT
{
    int 左,上,右,下;
} 



如果使用该类型的变量来传递函数GetWindowRect,则可以精确返回窗口位置(左,上,右,下)的值,以获取宽度,我从右减去左就可以了.达到高度.
但是,如果使用Rectangle变量来传递,它仍然可以正常工作,但是返回的值是不可用的,我的意思是它们是我不知道的某种东西,例如,返回的Width或Height不是窗口的实际宽度或高度(我想获取其RECT).我认为这可能是因为像素,缇,点等测量单位的差异...
您能帮我解决这里的问题吗?我可以使用Rectangle结构代替上面的RECT结构吗?我不想声明更多这样的结构,最好使用可用的元素!

解决方案

对其进行封装:编写一个接受/返回Rectangle的方法,该方法在内部创建RECT并将其交给GetWindowRect函数. /blockquote>

我问了一个问题:我可以使用矩形结构代替RECT结构吗?"

现在我可以自己回答:是"!

我发现在GetWindowRect中传递的第二个参数应该是具有4个int元素的任何结构的类型(名称实际上一点都不重要),第一个将存储窗口的左侧,第二个将存储顶部,第三个将存储"Right",第四个将存储"Bottom".这就是GetWindowRect可以返回到第二个参数的所有信息.

元素在结构中的顺序非常重要.例如,如果我将RECT声明为:

 结构 RECT
{
   public   int 顶部,左侧,右侧,底部;
} 

并称为GetWindowRect(Handle.ToInt32(), out r);
然后,窗口的实际左侧为r.top(不是r.left),窗口的实际顶部为r.left.

这使我想到了GetWindowRect如何将值返回到第二个参数.就像将所有数据(左,上,右,下)填充到按照该顺序传入的结构中一样,根本不需要关心结构及其元素的名称!
如果只想接收窗口的左侧,则可以将GetWindowRect声明为:

 [DllImport(" )]]
私有 外部 静态  int  hwnd, int  left); 

然后调用此GetWindowRect(Handle.ToInt32(), out left);,其中left是int的变量,它正好存储窗口的Left.如果要获得Left和Top,则可以声明仅包含两个元素(left和top)的结构,并适当地声明GetWindowRect,甚至可以使用System.Drawing.Size结构传递一个通知:Size.Width将存储左侧,而Size.Height将存储顶部.

哇!矩形呢?
我已经测试并得出以下结论:
Rectangle.Left将存储Left
Rectangle.Top将存储顶部
Rectangle.Width将存储Right
Rectangle.Height将存储底部

这就是使用Rectangle而不是我在问题中提到的RECT所需要知道的全部内容.

要获得宽度,我们从Rectangle.Width中减去Rectangle.Left.

Rectangle.RightRectangle.Bottom呢?
嗯,我真的不知道它们存储了什么!

谢谢OriginalGriff,您的评论正确!


I have used the windows API function GetWindowRect one of parameters is passed in is type of RECT with the definition:

struct RECT
{
   int left, top, right, bottom;
}



If using a variable of that type to pass in the function GetWindowRect, it works fine with returning exactly the values of window position (left, top, right, bottom), to get the width, I subtract left from right, the same to height.
But if using a variable of Rectangle instead to pass in, it still works without any exception but the values returned are unusable, I mean they are some kind of what I don''t know, for example, the Width or Height returned is not the actual the width or height of the window (that I want to get its RECT). I think that may be perhaps for the difference in measurement units such as pixel, twip, point ...
Could you please help me figure out the problem here? Could I use Rectangle structure instead of RECT structure above? I don''t want to declare more such a structure, using available elements is always the best!

Thank you so much!

解决方案

Encapsulate it: write a method that accepts / returns a Rectangle, but which creates the RECT internally and hands that to the GetWindowRect function.


I asked the question "Could I use Rectangle structure instead of RECT structure?"

And now I can answer it myself: "YES"!

I have discovered that the second parameter passed in the GetWindowRect should be type of any structure (the name is really not important at all) with 4 int elements, the first will store the Left of the window, the second will store the Top, the third will store the Right, the fourth will store the Bottom. That''s all the information the GetWindowRect can return into the second parameter.

The order of elements in the structure is very important. For example, if I declare RECT as:

struct RECT
{
  public int top,left,right, bottom;
}

and call this GetWindowRect(Handle.ToInt32(), out r);
Then the actual left of the window is r.top (not r.left) and the actual top of the window is r.left.

That leaded me to the how GetWindowRect returns values into the second parameter. It works as if it fills all the data (left, top, right, bottom) into the structure passed in according to that order and doesn''t care the names of the structure and its elements at all!
If you only want to receive the Left of a window, you can declare GetWindowRect as:

[DllImport("user32")]
private extern static int GetWindowRect(int hwnd, out int left);

Then call this GetWindowRect(Handle.ToInt32(), out left); in which, left is a variable of int and it stores exactly the Left of the window. If you want to get the Left and the Top, you can declare a structure containing two elements only (left and top) and declare the GetWindowRect appropriately, or even you can use System.Drawing.Size structure to pass in with a notice: Size.Width will store the Left and Size.Height will store the Top.

WOW! What about the Rectangle?
I have tested and conclude that:
Rectangle.Left will store the Left
Rectangle.Top will store the Top
Rectangle.Width will store the Right
Rectangle.Height will store the Bottom

That''s all you need to know to use Rectangle instead of the RECT I mentioned in the question.

To get the Width we subtract Rectangle.Left from Rectangle.Width.

What about Rectangle.Right and Rectangle.Bottom?
Hmm, I really don''t know what they store!

Thank you OriginalGriff, you are right in the your comment!


这篇关于RECT结构与Rectangle结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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