MFC控件随界面自动拉伸(EasySize.h)

EasySize.h

点击查看代码
#ifndef __EASYSIZE_H_
#define __EASYSIZE_H_
#define ES_BORDER 0xffffffff
#define ES_KEEPSIZE 0xfffffffe
#define ES_HCENTER 0x00000001
#define ES_VCENTER 0x00000002
#define DECLARE_EASYSIZE \
void __ES__RepositionControls(BOOL bInit);\
void __ES__CalcBottomRight(CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft, UINT id, UINT br, int es_br, CRect &rect, int clientbottomright);
#define INIT_EASYSIZE __ES__RepositionControls(TRUE); __ES__RepositionControls(FALSE)
#define UPDATE_EASYSIZE if(GetWindow(GW_CHILD)!=NULL) __ES__RepositionControls(FALSE)
#define EASYSIZE_MINSIZE(mx,my,s,r) if(r->right-r->left < mx) { if((s == WMSZ_BOTTOMLEFT)||(s == WMSZ_LEFT)||(s == WMSZ_TOPLEFT)) r->left = r->right-mx; else r->right = r->left+mx; } if(r->bottom-r->top < my) { if((s == WMSZ_TOP)||(s == WMSZ_TOPLEFT)||(s == WMSZ_TOPRIGHT)) r->top = r->bottom-my; else r->bottom = r->top+my; }
#define BEGIN_EASYSIZE_MAP(class) \
void class::__ES__CalcBottomRight(CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft, UINT id, UINT br, int es_br, CRect &rect, int clientbottomright) {\
if(br==ES_BORDER) bottomright = clientbottomright-es_br;\
else if(br==ES_KEEPSIZE) bottomright = topleft+es_br;\
else { CRect rect2;\
pThis->GetDlgItem(br)->GetWindowRect(rect2); pThis->ScreenToClient(rect2);\
bottomright = (bBottom?rect2.top:rect2.left) - es_br;}}\
void class::__ES__RepositionControls(BOOL bInit) { CRect rect,rect2,client; GetClientRect(client);
#define END_EASYSIZE_MAP Invalidate(); UpdateWindow(); }
#define EASYSIZE(id,l,t,r,b,o) \
static int id##_es_l, id##_es_t, id##_es_r, id##_es_b;\
if(bInit) {\
GetDlgItem(id)->GetWindowRect(rect); ScreenToClient(rect);\
if(o & ES_HCENTER) id##_es_l = rect.Width()/2; else {\
if(l==ES_BORDER) id##_es_l = rect.left; else if(l==ES_KEEPSIZE) id##_es_l = rect.Width(); else {\
GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2);\
id##_es_l = rect.left-rect2.right;}}\
if(o & ES_VCENTER) id##_es_t = rect.Height()/2; else {\
if(t==ES_BORDER) id##_es_t = rect.top; else if(t==ES_KEEPSIZE) id##_es_t = rect.Height(); else {\
GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2);\
id##_es_t = rect.top-rect2.bottom;}}\
if(o & ES_HCENTER) id##_es_r = rect.Width(); else { if(r==ES_BORDER) id##_es_r = client.right-rect.right; else if(r==ES_KEEPSIZE) id##_es_r = rect.Width(); else {\
GetDlgItem(r)->GetWindowRect(rect2); ScreenToClient(rect2);\
id##_es_r = rect2.left-rect.right;}}\
if(o & ES_VCENTER) id##_es_b = rect.Height(); else { if(b==ES_BORDER) id##_es_b = client.bottom-rect.bottom; else if(b==ES_KEEPSIZE) id##_es_b = rect.Height(); else {\
GetDlgItem(b)->GetWindowRect(rect2); ScreenToClient(rect2);\
id##_es_b = rect2.top-rect.bottom;}}\
} else {\
int left,top,right,bottom; BOOL bR = FALSE,bB = FALSE;\
if(o & ES_HCENTER) { int _a,_b;\
if(l==ES_BORDER) _a = client.left; else { GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2); _a = rect2.right; }\
if(r==ES_BORDER) _b = client.right; else { GetDlgItem(r)->GetWindowRect(rect2); ScreenToClient(rect2); _b = rect2.left; }\
left = _a+((_b-_a)/2-id##_es_l); right = left + id##_es_r;} else {\
if(l==ES_BORDER) left = id##_es_l;\
else if(l==ES_KEEPSIZE) { __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right); left = right-id##_es_l;\
} else { GetDlgItem(l)->GetWindowRect(rect2); ScreenToClient(rect2); left = rect2.right + id##_es_l; }\
if(l != ES_KEEPSIZE) __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right);}\
if(o & ES_VCENTER) { int _a,_b;\
if(t==ES_BORDER) _a = client.top; else { GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2); _a = rect2.bottom; }\
if(b==ES_BORDER) _b = client.bottom; else { GetDlgItem(b)->GetWindowRect(rect2); ScreenToClient(rect2); _b = rect2.top; }\
top = _a+((_b-_a)/2-id##_es_t); bottom = top + id##_es_b;} else {\
if(t==ES_BORDER) top = id##_es_t;\
else if(t==ES_KEEPSIZE) { __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom); top = bottom-id##_es_t;\
} else { GetDlgItem(t)->GetWindowRect(rect2); ScreenToClient(rect2); top = rect2.bottom + id##_es_t; }\
if(t != ES_KEEPSIZE) __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom);}\
GetDlgItem(id)->MoveWindow(left,top,right-left,bottom-top,FALSE);\
}
#endif //__EASYSIZE_H

  • 自己创建一个 EasySize.h 文件并复制代码保存,并加载到需要的项目中
  • stdafx.h 包含 EasySize.h 文件
  • 窗体类文件定义的最前面中加入DECLARE_EASYSIZE
Class C[窗体类]Dlg::public CDialogEx

{

DECLARE_EASYSIZE

...

}
  • 在对话框的OnInitDialog消息函数中添加INIT_EASYSIZE,放到函数尾处。
OnInitDialog()

{

    CDialog::OnInitDialog();

    ...

    INIT_EASYSIZE;

    return TRUE;

}
  • 添加响应WM_SIZE消息,在生成的响应函数中添加UPDATA_EASYSIZE宏(注意有分号):
OnSize(UINT nType, int cx, int cy)

{

CDialog::OnSize(nType, cx, cy);

    UPDATE_EASYSIZE;

}
  • 设置对话框最小尺寸(防止窗体过小导致控件被掩盖)

添加响应WM_SIZING消息,在生成的的响应函数中设置

OnSizing(UINT fwSide, LPRECT pRect)

{

CDialog::OnSizing(fwSide, pRect);

EASYSIZE_MINSIZE(640, 480, fwSide, pRect);
//将最小尺寸设置为640 x 480
}
  • 添加 EASYSIZE 的宏映射(在窗体类对应的源文件中[.cpp])
BEGIN_MESSAGE_MAP(Cauto_stretchDlg, CDialogEx)
...
END_MESSAGE_MAP()
// 以上是原有代码
BEGIN_EASYSIZE_MAP(Cauto_stretchDlg)
    EASYSIZE(IDOK, ES_KEEPSIZE, ES_KEEPSIZE, ES_BORDER, ES_BORDER, 0)
    EASYSIZE(IDCANCEL, ES_KEEPSIZE, ES_KEEPSIZE, ES_BORDER, ES_BORDER, 0)
    EASYSIZE(IDC_STATIC_LEFT, ES_BORDER, ES_BORDER, ES_KEEPSIZE, ES_BORDER, 0)
    EASYSIZE(IDC_STATIC_CENTOR, ES_BORDER, ES_BORDER, ES_BORDER, ES_BORDER, 0)
    EASYSIZE(IDC_STATIC_RIGHT, ES_KEEPSIZE, ES_BORDER, ES_BORDER, ES_BORDER, 0)
END_EASYSIZE_MAP

NOTE:宏映射的设置说明

/*
control:
    需要自动拉伸的控件ID
left, top, right, bottom有两种取值:
    ES_BORDER: 控件与对话框边界的距离;
    ES_KEEPSIZE: 控件水平/垂直方向上尺寸保持不变;
options(对齐方式):
    ES_HCENTER: 表示缩放后控件在指定位置内水平居中;
    ES_VCENTER: 表示缩放后控件在指定位置内垂直居中;
  无需对齐可置0,也可以两种方式同时对齐(ES_HCENTER|ES_VCENTER)
*/
EASYSIZE(control, left, top, right, bottom, options)
posted @ 2023-02-02 16:18  _神奇海螺  阅读(133)  评论(0编辑  收藏  举报