VS2010環境下EasySize.h使用心得
轉載:https://blog.csdn.net/u011762993/article/details/40898871
一、使用方法
假設建立的對話方塊的類名是CMyDlg,具體過程如下:
步驟一:在CMyDlg對話方塊介面單擊右鍵進入對話方塊的屬性設定介面,將Border設定為Resizing;將Maximize box和Minimize box均設定為True;
步驟二:把easysize.h拷貝到CMyDlg專案資料夾中,同時在CMyDlg的.h檔案和.cpp檔案中加入#include “easysize.h”;
步驟三:在CMyDlg類的h檔案中,加入DECLARE_EASYSIZE,注意結尾處不要加“;
// CMyDlg 對話方塊
class CMyDlg : public CDialog
{
DECLARE_EASYSIZE
// 構造
public:
CMyDlg (CWnd* pParent = NULL);// 標準建構函式
……
}
步驟四:在CMyDlg類的OnInitDialog()函式的結尾處加入INIT_EASYSIZE, 注意此處結尾處要加“;
// CMyDlg 訊息處理程式
BOOL CMyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此新增額外的初始化程式碼
INIT_EASYSIZE;
return TRUE; // 除非將焦點設定到控制元件,否則返回 TRUE
}
步驟五:選中對話方塊編輯介面,右鍵進入類嚮導,選中WM_SIZE訊息,點選右邊“新增處理程式”,增加WM_SIZE訊息響應函式OnSize(),在函式中加入UPDATE_EASYSIZE,注意此處結尾處要加“,WM_SIZING處理程式同此方法。
//最大化
void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// TODO: 在此處新增訊息處理程式程式碼
UPDATE_EASYSIZE;
}
//如果需要限制對話方塊有一個最小尺寸,在OnSizing訊息的響應函式新增EASYSIZE_MINSIZE(600, 400, fwSide, pRect),其中600、400為最小的長高尺寸
void CMyDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
CDialogEx::OnSizing(fwSide, pRect);
// TODO: 在此處新增訊息處理程式程式碼
EASYSIZE_MINSIZE(600, 400, fwSide, pRect);
}
步驟六:在CMyDlg的cpp檔案中新增EASYSIZE 的巨集對映
BEGIN_EASYSIZE_MAP(CMyDlg)
//EASYSIZE(Control_ID,left,top,right,bottom,options)
EASYSIZE(IDC_COMBO_COMMON,ES_BORDER,ES_BORDER,ES_BORDER,ES_KEEPSIZE,0)
EASYSIZE(IDC_BUTTON_OPEN,IDC_COMBO_COMMON,ES_BORDER,ES_BORDER,ES_KEEPSIZE,0)
EASYSIZE(IDC_BUTTON_CLOSE,IDC_BUTTON_OPEN,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,0)
EASYSIZE(IDC_EDIT_RECEVE,ES_BORDER,ES_KEEPSIZE,ES_BORDER,IDC_BUTTON_CLR_RXBUF,ES_HCENTER)
EASYSIZE(IDC_EDIT_SEND,IDC_BUTTON_CLOSE,ES_BORDER,ES_BORDER,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_SETMAXSEND,ES_BORDER,ES_KEEPSIZE,ES_BORDER,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_SETMAXRECV,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_ALLSEND,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_ALLRECEVE,ES_BORDER,ES_BORDER,ES_BORDER,ES_KEEPSIZE,0)
EASYSIZE(IDC_BUTTON_SEND,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_AUTO_PICK,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
EASYSIZE(IDC_BUTTON_END_PICK,IDC_BUTTON_AUTO_PICK,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
EASYSIZE(IDC_STATIC_SEND1,IDC_BUTTON_END_PICK,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
EASYSIZE(IDC_STATIC_SEND_COUNT,IDC_STATIC_SEND1,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
EASYSIZE(IDC_STATIC_COUNT,IDC_STATIC_SEND_COUNT,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
EASYSIZE(IDC_STATIC_NUM,IDC_STATIC_COUNT,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
END_EASYSIZE_MAP
注意:如果沒有新增EASYSIZE 的巨集對映就開始編譯的話,會出現連結錯誤!
二、EASYSIZE使用方法
這裡需要解釋的是巨集EASYSIZE()的用法,其原型如下:
EASYSIZE(control,left,top,right,bottom,options)
該巨集表示對ID值為control的控制元件實施縮放效果,縮放後的上下左右位置由control,left,top,right,bottom來確定,大小由option確定。
其中:control為對話方塊中的控制元件ID值,left,top,right,bottom四個引數為控制元件位置的座標,其值可以選擇ES_BOARD,ES_KEEPSIZE, 控制元件ID值三者之一。Options可以為ES_HCENTER, ES_VCENTER的結合,options可置0。
ES_BOARD表示控制元件與對話方塊邊界(以下簡稱邊界)的距離;
ES_KEEPSIZE表示控制元件水平/垂直方向上尺寸保持不變;
控制元件ID值表示當前控制元件與指定控制元件之間的距離;
ES_HCENTER表示縮放後控制元件在指定位置內水平居中;
ES_VCENTER表示縮放後控制元件在指定位置內垂直居中;
例如:
EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0)
表示縮放後,值為IDOK的控制元件,距離邊界上下左右位置保持不變,水平和垂直方向尺寸拉伸;
EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,ES_HCENTER)
表示縮放後,值為IDOK的控制元件,距離邊界上下位置保持不變,垂直方向尺寸拉伸,水平居中;
EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,ES_HCENTER| ES_HCENTER)
表示縮放後,值為IDOK的控制元件,在對話方塊內垂直居中,水平居中;
EASYSIZE(IDOK,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
表示縮放後,值為IDOK的控制元件,距離邊界左、下方位置保持不變,同時保持控制元件尺寸;
EASYSIZE(IDOK,ES_BORDER,ES_KEEPSIZE, ES_BORDER,ES_BORDER,0)
表示縮放後,值為IDOK的控制元件,距離邊界左、右、下方位置保持不變,水平方向尺寸拉伸,垂直方向尺寸不變;
EASYSIZE(IDOK,ES_BORDER,ES_BORDER,IDCANCEL,ES_BORDER,0)
表示縮放後,值為IDOK的控制元件,距離邊界上下左位置保持不變,距離ID值為IDCANCEL的右方位置距離保持不變,水平和垂直方向尺寸拉伸;(當使用指定控制元件作為定位引數時候,確保指定控制元件的EASYSIZE在該巨集前面)
三、經驗證過的EasySize.h原始檔
/***************以下是EasySize.h源文***************/
/*====================================================
EasySize.h |
| |
| EASY-SIZE Macros |
| |
| Copyright (c) 2001 - Marc Richarme |
| [email protected] |
| http://devix.cjb.net |
| |
| License: |
| |
| You may use this code in any commersial or non-
|
| commersial application, and you may redistribute |
| this file (and even modify it if you wish) as |
| long as you keep this notice untouched in any
|
| version you redistribute. |
| |
| Usage: |
| |
| - Insert ‘DECLARE_EASYSIZE’ somewhere in your |
| class declaration |
| - Insert an easysize map in the beginning of your |
| class implementation (see documentation) and
|
| outside of any function. |
| - Insert ‘INIT_EASYSIZE;’ in your |
| OnInitDialog handler. |
| - Insert ‘UPDATE_EASYSIZE’ in your OnSize handler |
| - Optional: Insert ‘EASYSIZE_MINSIZE(mx,my);’ in |
| your OnSizing handler if you want to specify |
| a minimum size for your dialog |
| |
| Check http://devix.cjb.net for the
|
| docs and new versions
|
| |
=====================================================*/
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源文*****************************/