此為我程序中的一個類,本用于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));
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);
響應WM_DESTROY消息:
CWindowAnchor::EndControlBound(hwnd);
代碼:
#pragma once
#include <map>
#if defined (_MSC_VER)
#pragma warning(disable: 4786)
#endif
/*用于WindowAnchorInfo結構的??款愋?/span>*/
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);
};
下載地址:單擊下載