VC 对话框程序在窗体改变大小,或则调整分辨率的时候,控件的布局就会乱套
以下是我模仿MFC消息映射宏写的一个 窗口调整布局的宏:
/**/
/********************************************************************
* Copyright 2007-2010 Digital Technology Co., Ltd.
* Filename: HIKafxWin.h
* Date: 2008/02/23
* Author:
* Description:模仿Microsoft afxwin.h头文件定义的一个HIK头文件
* Modification History:[<version > <time> <author> <desc>]
*********************************************************************/
#ifndef __HIKAFXWIN_H__
#define __HIKAFXWIN_H__
typedef enum
... {
HIK_MOVEX = 0,
HIK_MOVEY,
HIK_MOVEXY,
HIK_ELASTICX,
HIK_ELASTICY,
HIK_ELASTICXY
} HIK_MOVE_FLAG;
struct HIK_AFX_CTL_LOCAL
... {
//int iContainId;//容器的id
int iId; //控件id
int iFlag; //标志
int iPercent; //百分比
} ;
#define HIK_CONTRAL_MAP(id,flag,percent) {id,flag,percent},
#define HIK_DECLARE_CONTRAL_LOCAL_MAP()
public :
static const HIK_AFX_CTL_LOCAL * HIK_SetWndPosition(CWnd * pParentWnd , int iIncrementX , int iIncrementY) ;
#define HIK_BEGIN_CONTRAL_LOCAL_MAP()
static const HIK_AFX_CTL_LOCAL * HIK_SetWndPosition(CWnd * pParentWnd , int iIncrementX , int iIncrementY)
... {
static const HIK_AFX_CTL_LOCAL _HIK_Entries[]=
...{
#define HIK_END_CONTRAL_LOCAL_MAP()
...{-1,-1,-1}
};
int i = 0;
int iContainId = -1;
int iId = -1;
int iFlag = -1;
int iPercent = -1;
CWnd *pWndCtrl;
CRect rect;
int iLeft = -1;
int iTop = -1;
int iWidth =-1;
int iHeight = -1;
while (_HIK_Entries[i].iFlag != -1&&_HIK_Entries[i].iId != -1)
...{
pWndCtrl = NULL;
iId = _HIK_Entries[i].iId;
iFlag = _HIK_Entries[i].iFlag;
iPercent = _HIK_Entries[i].iPercent;
i++;
pWndCtrl= pParentWnd->GetDlgItem(iId);
if (!((NULL != pWndCtrl) && IsWindow(pWndCtrl->GetSafeHwnd())))continue;
pWndCtrl->GetWindowRect (rect);
pParentWnd->ScreenToClient (rect);
iLeft = rect.left;
iTop = rect.top;
iWidth = rect.Width();
iHeight = rect.Height();
switch(iFlag)
...{
case HIK_MOVEX:
iLeft += (iIncrementX * iPercent / 100);
break;
case HIK_MOVEY:
iTop += (iIncrementY * iPercent / 100);
break;
case HIK_MOVEXY:
iLeft += (iIncrementX * iPercent / 100);
iTop += (iIncrementY * iPercent / 100);
break;
case HIK_ELASTICX:
iWidth += (iIncrementX * iPercent / 100);
break;
case HIK_ELASTICY:
iHeight += (iIncrementY * iPercent / 100);
break;
case HIK_ELASTICXY:
iWidth += (iIncrementX * iPercent / 100);
iHeight += (iIncrementY * iPercent / 100);
break;
default:
break;
}
pWndCtrl->MoveWindow(iLeft, iTop, iWidth, iHeight);
}
return NULL;
}
#endif // __HIKAFXWIN_H__
* Copyright 2007-2010 Digital Technology Co., Ltd.
* Filename: HIKafxWin.h
* Date: 2008/02/23
* Author:
* Description:模仿Microsoft afxwin.h头文件定义的一个HIK头文件
* Modification History:[<version > <time> <author> <desc>]
*********************************************************************/
#ifndef __HIKAFXWIN_H__
#define __HIKAFXWIN_H__
typedef enum
... {
HIK_MOVEX = 0,
HIK_MOVEY,
HIK_MOVEXY,
HIK_ELASTICX,
HIK_ELASTICY,
HIK_ELASTICXY
} HIK_MOVE_FLAG;
struct HIK_AFX_CTL_LOCAL
... {
//int iContainId;//容器的id
int iId; //控件id
int iFlag; //标志
int iPercent; //百分比
} ;
#define HIK_CONTRAL_MAP(id,flag,percent) {id,flag,percent},
#define HIK_DECLARE_CONTRAL_LOCAL_MAP()
public :
static const HIK_AFX_CTL_LOCAL * HIK_SetWndPosition(CWnd * pParentWnd , int iIncrementX , int iIncrementY) ;
#define HIK_BEGIN_CONTRAL_LOCAL_MAP()
static const HIK_AFX_CTL_LOCAL * HIK_SetWndPosition(CWnd * pParentWnd , int iIncrementX , int iIncrementY)
... {
static const HIK_AFX_CTL_LOCAL _HIK_Entries[]=
...{
#define HIK_END_CONTRAL_LOCAL_MAP()
...{-1,-1,-1}
};
int i = 0;
int iContainId = -1;
int iId = -1;
int iFlag = -1;
int iPercent = -1;
CWnd *pWndCtrl;
CRect rect;
int iLeft = -1;
int iTop = -1;
int iWidth =-1;
int iHeight = -1;
while (_HIK_Entries[i].iFlag != -1&&_HIK_Entries[i].iId != -1)
...{
pWndCtrl = NULL;
iId = _HIK_Entries[i].iId;
iFlag = _HIK_Entries[i].iFlag;
iPercent = _HIK_Entries[i].iPercent;
i++;
pWndCtrl= pParentWnd->GetDlgItem(iId);
if (!((NULL != pWndCtrl) && IsWindow(pWndCtrl->GetSafeHwnd())))continue;
pWndCtrl->GetWindowRect (rect);
pParentWnd->ScreenToClient (rect);
iLeft = rect.left;
iTop = rect.top;
iWidth = rect.Width();
iHeight = rect.Height();
switch(iFlag)
...{
case HIK_MOVEX:
iLeft += (iIncrementX * iPercent / 100);
break;
case HIK_MOVEY:
iTop += (iIncrementY * iPercent / 100);
break;
case HIK_MOVEXY:
iLeft += (iIncrementX * iPercent / 100);
iTop += (iIncrementY * iPercent / 100);
break;
case HIK_ELASTICX:
iWidth += (iIncrementX * iPercent / 100);
break;
case HIK_ELASTICY:
iHeight += (iIncrementY * iPercent / 100);
break;
case HIK_ELASTICXY:
iWidth += (iIncrementX * iPercent / 100);
iHeight += (iIncrementY * iPercent / 100);
break;
default:
break;
}
pWndCtrl->MoveWindow(iLeft, iTop, iWidth, iHeight);
}
return NULL;
}
#endif // __HIKAFXWIN_H__
#pragma
once
#include " HIKafxWin.h "
class CHareWareTestDlg : public CDialog
... {
public:
CHareWareTestDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum ...{ IDD = IDD_HAREWARETEST_DIALOG };
DECLARE_MESSAGE_MAP()
HIK_DECLARE_CONTRAL_LOCAL_MAP() //hikafxwin.h定义的宏,在这里声明
............
}
#include " HIKafxWin.h "
class CHareWareTestDlg : public CDialog
... {
public:
CHareWareTestDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum ...{ IDD = IDD_HAREWARETEST_DIALOG };
DECLARE_MESSAGE_MAP()
HIK_DECLARE_CONTRAL_LOCAL_MAP() //hikafxwin.h定义的宏,在这里声明
............
}
以下是在cpp文件中怎么实现:
#include
"
stdafx.h
"
#include " HareWareTestDlg.h "
// 控件布局宏,见HIKafxWin.h文件
HIK_BEGIN_CONTRAL_LOCAL_MAP()
HIK_CONTRAL_MAP(IDC_STATIC_PREGROUP,HIK_ELASTICX, 100 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_ELASTICX, 100 )
HIK_CONTRAL_MAP(IDC_STATIC_PREGROUP,HIK_ELASTICY, 50 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_ELASTICY, 44 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_MOVEY, 48 )
HIK_CONTRAL_MAP(IDC_BUTTON_RESETFPGA,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_RESETAD,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_ONE,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_FOUR,HIK_MOVEX, 100 )
HIK_END_CONTRAL_LOCAL_MAP()
BOOL CHareWareTestDlg::OnInitDialog()
... {
CDialog::OnInitDialog();
UINT totalWndCount;
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
CRect rectClient;
GetClientRect(rectClient);
m_cx = rectClient.Width();//窗口正常打开时的宽度
m_cy = rectClient.Height();//高度
ShowWindow(SW_MAXIMIZE);
int _cx = GetSystemMetrics(SM_CXSCREEN);//屏幕宽
int _cy = GetSystemMetrics(SM_CYSCREEN);//高
if (_cx != 1024|| _cy !=768)
...{
::HIK_SetWndPosition(this,_cx-m_cx,_cy-m_cy); // 我在宏里定义的函数,_cx-m_cx,表示x方向上改变的距离,同理_cy-m_cy在y方向上改变的距离,当然该函数可以在onsize事件中处理,我在OnInitDialog()处理是因为分辨率改变时需要处理的。
}
}
#include " HareWareTestDlg.h "
// 控件布局宏,见HIKafxWin.h文件
HIK_BEGIN_CONTRAL_LOCAL_MAP()
HIK_CONTRAL_MAP(IDC_STATIC_PREGROUP,HIK_ELASTICX, 100 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_ELASTICX, 100 )
HIK_CONTRAL_MAP(IDC_STATIC_PREGROUP,HIK_ELASTICY, 50 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_ELASTICY, 44 )
HIK_CONTRAL_MAP(IDC_STATIC_DISPALYGROUP,HIK_MOVEY, 48 )
HIK_CONTRAL_MAP(IDC_BUTTON_RESETFPGA,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_RESETAD,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_ONE,HIK_MOVEX, 100 )
HIK_CONTRAL_MAP(IDC_BUTTON_FOUR,HIK_MOVEX, 100 )
HIK_END_CONTRAL_LOCAL_MAP()
BOOL CHareWareTestDlg::OnInitDialog()
... {
CDialog::OnInitDialog();
UINT totalWndCount;
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
CRect rectClient;
GetClientRect(rectClient);
m_cx = rectClient.Width();//窗口正常打开时的宽度
m_cy = rectClient.Height();//高度
ShowWindow(SW_MAXIMIZE);
int _cx = GetSystemMetrics(SM_CXSCREEN);//屏幕宽
int _cy = GetSystemMetrics(SM_CYSCREEN);//高
if (_cx != 1024|| _cy !=768)
...{
::HIK_SetWndPosition(this,_cx-m_cx,_cy-m_cy); // 我在宏里定义的函数,_cx-m_cx,表示x方向上改变的距离,同理_cy-m_cy在y方向上改变的距离,当然该函数可以在onsize事件中处理,我在OnInitDialog()处理是因为分辨率改变时需要处理的。
}
}