正文

不容小视的坐标问题  差点翻船!2006-12-03 00:17:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yuqiexing/21208.html

分享到:

没事的时候做了小程序,结果出现了大问题。

情况是这样的:

是一个基于对话框的程序。首先

BOOL  Cmydlg::OnInitDialog()

{

......

CRect rect1;

GetWindowRect(rect1);   

}

同时我也添加了一个按钮控件,处理单击事件:

void Cmydlg::OnButtonClk()

{

CRect rect2;

GetWindowRect(rect2); 

}

以前我的理解是GetWindowRect获得的都是相对于屏幕坐标系的坐标,所以我猜想rect1此时不会左上角是0,0,,并且rect1==rect2,哪知道一跟踪,差点晕过去,左上角虽然不是0,但是也接近于0了,rect1也根本不等于rect2,这是怎么回事?

不明白。问了高手,才知道,原来在初始化(OnInitDialog)的时候,窗口还没有完全显示,此时用函数进行测试结果肯定不正确。后来测试的rect2才是真的屏幕坐标系上的坐标。原来是这样!好险!

但是我还有个问题没能解决。那就是rect1到底测的是什么呢?不知道,知道的告诉一下啊

下面再来谈谈MoveWindow

For a top-level CWnd object, the x and y parameters are relative to the upper-left corner of the screen. For a child CWnd object, they are relative to the upper-left corner of the parent window’s client area.

The MoveWindow function sends the WM_GETMINMAXINFO message. Handling this message gives CWnd the opportunity to modify the default values for the largest and smallest possible windows. If the parameters to the MoveWindow member function exceed these values, the values can be replaced by the minimum or maximum values in the WM_GETMINMAXINFO handler.

以上是MSDN上的话,大意是说如果是顶层窗口,那么movewindow中的参数坐标是屏幕坐标系的,否则就是父窗口的客户区坐标。听起来很简单,但到了实际应用中,很容易忽略。把顶层窗口还有子窗口的概念早就抛之脑后。请大家记住:

顶层窗口是指父窗口是屏幕窗口的窗口。movewindow中的参数实际上就是父窗口(或者客户区)坐标系中的坐标。

阅读(3777) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册