`
mmdev
  • 浏览: 12919344 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

论控件在窗口的自适应

 
阅读更多

近来在做一个程序,有十个对话框,每个对话框有上百个控件。并且对话框还是有背景图的。如果在初始化时将位置定死,那么在窗口大小改变时整个界面就会变的很乱。同样,当用户改变屏幕分辨率时,也会有这个问题。

总之,这是一个控件适应窗口的问题,要求:在主窗口大小或者屏幕分辨率改变时,控件的位置和宽高都要改变。

可以推断,控件左上角位置,高度,宽度都和主窗口的宽高线性相关 比如控件width = 窗口width* 0.010

又因为控件较多,所以不能用浮点运算,全部乘以1000就可以满足精度要求。

于是有下面这个函数:

static void Move(CWnd* pWnd, int l, int t, ULONG cx, ULONG cy,
int xScale, int yScale, int width, int height)
{

ASSERT(IsWindow(pWnd->GetSafeHwnd()));
pWnd->MoveWindow(l + static_cast<int>((cx * xScale) / 1000),
t + static_cast<int>((cy * yScale) / 1000),
static_cast<int>((cx * width) / 1000),
static_cast<int>((cy * height)/ 1000));


}

cx,cy 是主窗口宽高

l, t 是开始计算点的坐标

xScale,yScale,width,height 是左上角坐标和宽高相对于主窗口的比例。

这时就可以在主窗口的OnSize()中这样写:

Move(&m_static1, IMG_LEFT, IMG_TOP, cx, cx, 17, 54, 52, 16);
Move(&m_static2, IMG_LEFT, IMG_TOP, cx, cx, 76, 54, 52, 16);
Move(&m_static5, IMG_LEFT, IMG_TOP, cx, cx, 135, 54, 52, 16);
Move(&m_static6, IMG_LEFT, IMG_TOP, cx, cx, 223, 54, 52, 16);
Move(&m_static7, IMG_LEFT, IMG_TOP, cx, cx, 282, 54, 52, 16);
Move(&m_static8, IMG_LEFT, IMG_TOP, cx, cx, 341, 54, 52, 16);
Move(&m_static9, IMG_LEFT, IMG_TOP, cx, cx, 434, 54, 52, 16);
Move(&m_static10, IMG_LEFT, IMG_TOP, cx, cx, 494, 54, 52, 16);
Move(&m_static11, IMG_LEFT, IMG_TOP, cx, cx, 572, 54, 52, 16);
Move(&m_static12, IMG_LEFT, IMG_TOP, cx, cx, 631, 54, 52, 16);
Move(&m_static13, IMG_LEFT, IMG_TOP, cx, cx, 709, 54, 52, 16);
Move(&m_static14, IMG_LEFT, IMG_TOP, cx, cx, 768, 54, 52, 16);
Move(&m_static15, IMG_LEFT, IMG_TOP, cx, cx, 846, 54, 52, 16);
Move(&m_static16, IMG_LEFT, IMG_TOP, cx, cx, 906, 54, 52, 16);

也许有人会问,这些参数如何取得,自已一点点调不会太累啊。

我是这样做的,在OnMouseMove中计算鼠标坐标,再算出当前点相对于整个窗口的比例,显示在一个EDIT中

我在各种环境下测试过,效果还不错。似乎控件变的“聪明”了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics