此为我程序中的一个类,本用于WinCE,但在桌面系统上也同样适用!
使用方法(在WM_INITDIALOG或WM_CREATE消息中加入):
CWindowAnchor::BeginControlBound(hwnd)
手动调整控件位置:
CWindowAnchor::AddControl(hwnd,IDC_STATIC1,
&
WindowAnchorInfo(WAT_LEFT
|
WAT_TOP,
2
,
8
,
4
,
10
));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT, 2 , 20 , 4 , 10 ));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP, 2 , 8 , 40 , 10 ));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT, 2 , 20 , 4 , 10 ));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP, 2 , 8 , 40 , 10 ));
自动调整控件位置(跟据设计时资源文件中控件的大小及位置):
CWindowAnchor::AddControl(hwnd,IDC_STATIC1,
&
WindowAnchorInfo(WAT_LEFT
|
WAT_TOP));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT));
响应WM_SIZE消息:
case
WM_SIZE:
return HANDLE_WM_SIZE(hwndDlg,wParam,lParam,CWindowAnchor::OnSize);
return HANDLE_WM_SIZE(hwndDlg,wParam,lParam,CWindowAnchor::OnSize);
响应WM_DESTROY消息:
CWindowAnchor::EndControlBound(hwnd);
代码:
#pragma
once
#include < map >
#if defined (_MSC_VER)
#pragma warning(disable: 4786)
#endif
/* 用于WindowAnchorInfo结构的停靠类型 */
typedef enum WindowAnchorType
{
WAT_TOP = 0x0001 ,
WAT_LEFT = 0x0002 ,
WAT_RIGHT = 0x0004 ,
WAT_BOTTOM = 0x0008
};
/* 控件定位描述信息 */
typedef struct WindowAnchorInfo{
DWORD dwAnchor; // WAT_*
RECT rcOriginalRect; // 控件的原始边距,如果为空则自动获取(仅适用于WM_INIT中)
WindowAnchorInfo(DWORD pAnchor = WAT_TOP | WAT_LEFT,LONG pLeft = 0 ,LONG pTop = 0 ,LONG pRight = 0 ,LONG pBottom = 0 )
{
dwAnchor = pAnchor;
rcOriginalRect.left = pLeft;
rcOriginalRect.top = pTop;
rcOriginalRect.right = pRight;
rcOriginalRect.bottom = pBottom;
};
};
typedef std::map < HWND,WindowAnchorInfo > ControlHashtable;
typedef struct {
INT nWidth; // 对话框宽度
INT nHeight; // 对话框高度
INT nMinHeight; // 对话框最小高度
ControlHashtable mapControls; // 对话框所有子控件
}WindowAnchorDialog;
/*
* 对话框子控件定位
* 2009.03.29 By Frank
*/
static class CWindowAnchor
{
private :
static BOOL _ReSize(HWND hwndDlg, const WindowAnchorDialog * wad, HWND hwndCtrl, const WindowAnchorInfo * wai);
public :
/*
* 开始调整(此调用中会获取当前对话框的大小,如果在设计后要调整对话框大小,请先调用此方法)
* hwndDlg:对话框句柄
*/
static BOOL BeginControlBound(HWND hwndDlg);
/*
* 结束调整
* hwndDlg:对话框句柄
*/
static BOOL EndControlBound(HWND hwndDlg);
/*
* 添加一个控件到调整列表
* hWndInsertAfter:HWND_BOTTOM |HWND_NOTOPMOST | HWND_TOP | HWND_TOPMOST |-2不改变 | Is Hwnd
*/
static BOOL AddControl(HWND hwndDlg, INT nCtrlID, WindowAnchorInfo * wai, HWND hWndInsertAfter = (HWND) - 2 );
/*
* 调整一个指定控件的大小
*/
static BOOL ReSize(HWND hwndDlg, HWND hwndCtrl);
/*
* 响应WM_SIZE消息
*/
static BOOL OnSize(HWND hwndDlg, UINT state, int cx, int cy);
/* 相应WM_VSCROLL消息 */
static BOOL OnVScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos);
};
#include < map >
#if defined (_MSC_VER)
#pragma warning(disable: 4786)
#endif
/* 用于WindowAnchorInfo结构的停靠类型 */
typedef enum WindowAnchorType
{
WAT_TOP = 0x0001 ,
WAT_LEFT = 0x0002 ,
WAT_RIGHT = 0x0004 ,
WAT_BOTTOM = 0x0008
};
/* 控件定位描述信息 */
typedef struct WindowAnchorInfo{
DWORD dwAnchor; // WAT_*
RECT rcOriginalRect; // 控件的原始边距,如果为空则自动获取(仅适用于WM_INIT中)
WindowAnchorInfo(DWORD pAnchor = WAT_TOP | WAT_LEFT,LONG pLeft = 0 ,LONG pTop = 0 ,LONG pRight = 0 ,LONG pBottom = 0 )
{
dwAnchor = pAnchor;
rcOriginalRect.left = pLeft;
rcOriginalRect.top = pTop;
rcOriginalRect.right = pRight;
rcOriginalRect.bottom = pBottom;
};
};
typedef std::map < HWND,WindowAnchorInfo > ControlHashtable;
typedef struct {
INT nWidth; // 对话框宽度
INT nHeight; // 对话框高度
INT nMinHeight; // 对话框最小高度
ControlHashtable mapControls; // 对话框所有子控件
}WindowAnchorDialog;
/*
* 对话框子控件定位
* 2009.03.29 By Frank
*/
static class CWindowAnchor
{
private :
static BOOL _ReSize(HWND hwndDlg, const WindowAnchorDialog * wad, HWND hwndCtrl, const WindowAnchorInfo * wai);
public :
/*
* 开始调整(此调用中会获取当前对话框的大小,如果在设计后要调整对话框大小,请先调用此方法)
* hwndDlg:对话框句柄
*/
static BOOL BeginControlBound(HWND hwndDlg);
/*
* 结束调整
* hwndDlg:对话框句柄
*/
static BOOL EndControlBound(HWND hwndDlg);
/*
* 添加一个控件到调整列表
* hWndInsertAfter:HWND_BOTTOM |HWND_NOTOPMOST | HWND_TOP | HWND_TOPMOST |-2不改变 | Is Hwnd
*/
static BOOL AddControl(HWND hwndDlg, INT nCtrlID, WindowAnchorInfo * wai, HWND hWndInsertAfter = (HWND) - 2 );
/*
* 调整一个指定控件的大小
*/
static BOOL ReSize(HWND hwndDlg, HWND hwndCtrl);
/*
* 响应WM_SIZE消息
*/
static BOOL OnSize(HWND hwndDlg, UINT state, int cx, int cy);
/* 相应WM_VSCROLL消息 */
static BOOL OnVScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos);
};
下载地址:单击下载