//从此类派生出的对话框可以根据对话框大小变化自动调整控件大小
//头文件
-
-
-
-
-
-
- class ClxDialog : public CDialog
- {
- public:
- ClxDialog(UINT nID, CWnd* pParent = NULL);
-
- typedef struct _dlgControlTag
- {
- int iId;
- int iFlag;
- int iPercent;
- } DLGCTLINFO, *PDLGCTLINFO;
-
- enum
- {
- MOVEX = 0,
- MOVEY,
- MOVEXY,
- ELASTICX,
- ELASTICY,
- ELASTICXY
- };
-
-
- BOOL SetControlProperty(PDLGCTLINFO lp, int nElements);
-
-
- void ShowSizeIcon(BOOL bShow = TRUE);
-
- protected:
- virtual BOOL OnInitDialog();
- afx_msg void OnSize(UINT nType, int cx, int cy);
- afx_msg void OnSizing(UINT nSide, LPRECT lpRect);
- DECLARE_MESSAGE_MAP()
-
- private:
- int m_iClientWidth;
- int m_iClientHeight;
- int m_iMinWidth;
- int m_iMinHeight;
- PDLGCTLINFO m_pControlArray;
- int m_iControlNumber;
- BOOL m_bShowSizeIcon;
- CStatic m_wndSizeIcon;
-
- CBitmap m_bmpSizeIcon;
- BITMAP m_bitmap;
- };
//实现文件
-
-
-
-
-
-
- #include "stdafx.h"
- #include "lxDialog.h"
-
-
- #ifndef OBM_SIZE
- #define OBM_SIZE 32766
- #endif
-
- ClxDialog::ClxDialog(UINT nID, CWnd* pParent )
- : CDialog(nID, pParent)
- , m_iClientWidth(0)
- , m_iClientHeight(0)
- , m_iMinWidth(0)
- , m_iMinHeight(0)
- , m_pControlArray(NULL)
- , m_iControlNumber(0)
- , m_bShowSizeIcon(TRUE)
- {}
-
- BEGIN_MESSAGE_MAP(ClxDialog, CDialog)
- ON_WM_SIZE()
- ON_WM_SIZING()
- END_MESSAGE_MAP()
-
- BOOL ClxDialog::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
-
- ModifyStyle(0, WS_SIZEBOX);
-
-
- CRect rectDlg;
- GetWindowRect(rectDlg);
- m_iMinWidth = rectDlg.Width();
- m_iMinHeight = rectDlg.Height();
-
-
- CRect rectClient;
- GetClientRect(rectClient);
- m_iClientWidth = rectClient.Width();
- m_iClientHeight = rectClient.Height();
-
-
- m_bmpSizeIcon.LoadOEMBitmap(OBM_SIZE);
- m_bmpSizeIcon.GetBitmap(&m_bitmap);
-
-
- m_wndSizeIcon.Create(NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP, CRect(0, 0, m_bitmap.bmWidth, m_bitmap.bmHeight), this, 0);
- m_wndSizeIcon.SetBitmap(m_bmpSizeIcon);
- m_wndSizeIcon.MoveWindow(m_iClientWidth - m_bitmap.bmWidth, m_iClientHeight - m_bitmap.bmHeight, m_bitmap.bmWidth, m_bitmap.bmHeight);
-
-
- m_wndSizeIcon.ShowWindow(m_bShowSizeIcon);
-
- return TRUE;
- }
-
- void ClxDialog::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);
-
-
- int iIncrementX = cx - m_iClientWidth;
- int iIncrementY = cy - m_iClientHeight;
-
-
- if (nType == SIZE_MINIMIZED)
- {
- iIncrementX = iIncrementY = 0;
- }
-
- for (int i = 0; i < m_iControlNumber; i++)
- {
- CWnd *pWndCtrl = NULL;
-
- int iId = m_pControlArray[i].iId;
- int iFlag = m_pControlArray[i].iFlag;
- int iPercent = m_pControlArray[i].iPercent;
-
-
- if ((iPercent < 0) || (iPercent > 100))
- continue;
-
-
- pWndCtrl = GetDlgItem(iId);
- if ((NULL != pWndCtrl) && IsWindow(pWndCtrl->GetSafeHwnd()))
- {
- CRect rectCtrl;
- pWndCtrl->GetWindowRect(rectCtrl);
-
- ScreenToClient(rectCtrl);
-
- int iLeft = rectCtrl.left;
- int iTop = rectCtrl.top;
- int iWidth = rectCtrl.Width();
- int iHeight = rectCtrl.Height();
-
- switch (iFlag)
- {
- case MOVEX:
- iLeft += (iIncrementX * iPercent / 100);
- break;
-
- case MOVEY:
- iTop += (iIncrementY * iPercent / 100);
- break;
-
- case MOVEXY:
- iLeft += (iIncrementX * iPercent / 100);
- iTop += (iIncrementY * iPercent / 100);
- break;
-
- case ELASTICX:
- iWidth += (iIncrementX * iPercent / 100);
- break;
-
- case ELASTICY:
- iHeight += (iIncrementY * iPercent / 100);
- break;
-
- case ELASTICXY:
- iWidth += (iIncrementX * iPercent / 100);
- iHeight += (iIncrementY * iPercent / 100);
- break;
-
- default:
- ;
- }
-
-
- pWndCtrl->MoveWindow(iLeft, iTop, iWidth, iHeight);
- }
- }
-
-
- if (IsWindow(m_wndSizeIcon.GetSafeHwnd()))
- m_wndSizeIcon.MoveWindow(cx - m_bitmap.bmWidth, cy - m_bitmap.bmHeight, m_bitmap.bmWidth, m_bitmap.bmHeight);
-
-
- if (nType != SIZE_MINIMIZED)
- {
- m_iClientWidth = cx;
- m_iClientHeight = cy;
- }
- }
-
- void ClxDialog::OnSizing(UINT nSide, LPRECT lpRect)
- {
- CDialog::OnSizing(nSide, lpRect);
-
-
-
- int iWidth = lpRect->right - lpRect->left;
- int iHeight = lpRect->bottom - lpRect->top;
-
- if (iWidth <= m_iMinWidth)
- lpRect->right = lpRect->left + m_iMinWidth;
-
- if(iHeight <= m_iMinHeight)
- lpRect->bottom = lpRect->top + m_iMinHeight;
- }
-
- BOOL ClxDialog::SetControlProperty(PDLGCTLINFO lp, int nElements)
- {
-
-
- if (NULL == lp)
- return FALSE;
-
- if (nElements <= 0)
- return FALSE;
-
- m_pControlArray = lp;
- m_iControlNumber = nElements;
-
- return TRUE;
- }
-
- void ClxDialog::ShowSizeIcon(BOOL bShow )
- {
- m_bShowSizeIcon = bShow;
- }
使用方法
//使用方法,添加到工程文件中,然后从ClxDialog派生对话框
1,在非模态对话框类的头文件(.h)顶部加上#include "lxDialog.h",然后把非模态对话框类的基类改成ClxDialog,也就是在头文件里class C****** : public CDialog 改成: class C****** : public ClxDialog
2,在非模态对话框类的.cpp函数里,把构造函数
C*****::C*****(CWnd* pParent ): CDialog(C*****::IDD, pParent) 改成:
C*****::C*****(CWnd* pParent ): ClxDialog(C*****::IDD, pParent) 2,用类向导给非模态对话框添加一个OnInitDialog()函数;也就是在双击类向导->Message Maps-> 选非模态对话框类;Messages选WM_INITDIALOG双击它,这样在你的非模态对话框的cpp文件里就生成了ON_WM_INITDIALOG和OnInitDialog()函数;
3,在这个OnInitDialog函数里添加代码: ClxDialog::OnInitDialog();
ClxDialog::ShowSizeIcon(TRUE);//显示可以调整大小的图标
static DLGCTLINFO dcMenuGroup[] = {
{IDCANCEL, MOVEX, 100},
{IDCANCEL, MOVEY, 100},
{IDC_EDIT_RECDSPDLG, ELASTICX, 100},
{IDC_EDIT_RECDSPDLG, ELASTICY, 100},
};
// 设置控件信息
SetControlProperty(dcMenuGroup, sizeof(dcMenuGroup)/sizeof(DLGCTLINFO));
return TRUE;
//IDCANCEL是关闭按钮的ID,IDOK按钮我去掉了所以没加,IDC_EDIT_RECDSPDLG是一个EDIT控件
4,再用类向导添加OnSize()函数,也就是双击ON_WM_SIZE,这样就增加了
void C******::OnSize(UINT nType, int cx, int cy) 这个函数,然后在这个函数里添加代码:ClxDialog::OnSize(nType, cx, cy);
5,别忘了在资源里右键你的非模态对话框,把属性栏Styles里Border选成Resizing。