EASYSIZE
假設(shè)創(chuàng)建的對話框的類名是CMyDlg,具體過程如下:
步驟一:把easysize.h拷貝到CMyDlg項(xiàng)目文件夾中,同時(shí)在CMyDlg的.h文件和.cpp文件中加入#include "easysize.h";
步驟二:在CMyDlg類的h文件中,加入DECLARE_EASYSIZE,注意結(jié)尾處不要加“;”。
class CMyDlg : public CDialog
{
?????? DECLARE_EASYSIZE
// Construction
…
}
步驟三:在CMyDlg類的OnInitDialog()函數(shù)的結(jié)尾處加入INIT_EASYSIZE, 注意此處結(jié)尾處要加“;”。
BOOL CMyDlg::OnInitDialog()
{???? …
// TODO: Add extra initialization here
?????? INIT_EASYSIZE;
?????? return TRUE;? // return TRUE? unless you set the focus to a control
}
?
步驟四:增加WM_SIZE消息響應(yīng)函數(shù)OnSize(),在函數(shù)中加入U(xiǎn)PDATE_EASYSIZE,注意此處結(jié)尾處要加“;”。
void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
?????? CDialog::OnSize(nType, cx, cy);
?????? // TODO: Add your message handler code here
????? UPDATE_EASYSIZE;
}
步驟五:在CMyDlg的cpp文件中添加EASYSIZE 的宏映射
BEGIN_EASYSIZE_MAP(CMyDlg)
EASYSIZE(control,left,top,right,bottom,options)
END_EASYSIZE_MAP
注意:如果沒有添加EASYSIZE 的宏映射就開始編譯的話,會出現(xiàn)鏈接錯(cuò)誤!
這里需要解釋的是宏EASYSIZE()的用法,其原型如下:
EASYSIZE(control,left,top,right,bottom,options)
該宏表示對ID值為control的控件實(shí)施縮放效果,縮放后的上下左右位置由control,left,top,right,bottom來確定,大小由option確定。
其中:control為對話框中的控件ID值,left,top,right,bottom四個(gè)參數(shù)為控件位置的坐標(biāo),其值可以選擇ES_BOARD,ES_KEEPSIZE, 控件ID值三者之一。Options可以為ES_HCENTER, ES_VCENTER的結(jié)合,options可置0。
ES_BOARD表示控件與對話框邊界(以下簡稱邊界)的距離;
ES_KEEPSIZE表示控件水平/垂直方向上尺寸保持不變;
控件ID值表示當(dāng)前控件與指定控件之間的距離;
ES_HCENTER表示縮放后控件在指定位置內(nèi)水平居中;
ES_VCENTER表示縮放后控件在指定位置內(nèi)垂直居中;
?
例如:
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的控件,在對話框內(nèi)垂直居中,水平居中;
EASYSIZE(IDOK,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)
表示縮放后,值為IDOK的控件,距離邊界左、下方位置保持不變,同時(shí)保持控件尺寸;
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的右方位置距離保持不變,水平和垂直方向尺寸拉伸;(當(dāng)使用指定控件作為定位參數(shù)時(shí)候,確保指定控件的EASYSIZE在該宏前面)
----------------------------------------------------------------------------------------------------------------------------
下面是easysize.h的內(nèi)容
/*===================================================*\ | | | EASY-SIZE Macros | | | | Copyright (c) 2001 - Marc Richarme | | devix@devix.cjb.net | | 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_?
總結(jié)
- 上一篇: 让VC程序在Win7下获得管理员权限
- 下一篇: RegEnumValue枚举注册表值小记