mfc基于对话框的应用程序,如何设置初始对话框大小,移动控件位置

 1 void MmPLEntPropertyDlg::SetInitDialogSize()
 2 {
 3     CRect rectDlg;
 4     GetWindowRect(rectDlg);//x,y为对话框左上角的坐标 w,h为对话框的宽高
 5     ::SetWindowPos(this->m_hWnd, HWND_BOTTOM, 100, 200, rectDlg.Width(), rectDlg.Height() + 30, SWP_NOZORDER);
 6     CenterWindow();//居中显示
 7 }
 8 
 9 void MmPLEntPropertyDlg::MoveCtrlPosition(bool bModifySize /* = false */)
10 {
11     if(bModifySize)
12     {//改变控件大小
13         for(int i = 0; i < 3; i++)
14         {
15             CWnd *pWndCtrl = NULL;
16             if(0 == i) pWndCtrl = GetDlgItem(IDOK);
17             if(1 == i) pWndCtrl = GetDlgItem(IDCANCEL);
18             if(2 == i) pWndCtrl = GetDlgItem(IDC_LIST_PLENTPROPERTY);
19 
20             if ((NULL != pWndCtrl) && IsWindow(pWndCtrl->GetSafeHwnd()))
21             {
22                 CRect rectCtrl;
23                 pWndCtrl->GetWindowRect(rectCtrl);
24                 ScreenToClient(rectCtrl);
25                 int iLeft = rectCtrl.left;
26                 int iTop = rectCtrl.top;
27                 int iWidth = rectCtrl.Width();
28                 int iHeight = rectCtrl.Height();
29                 
30                 if(0 == i || 1 == i)
31                 {
32                     //把button控件移动到新位置
33                     pWndCtrl->MoveWindow(iLeft, iTop, iWidth, iHeight);
34                 }
35                 if(2 == i)
36                 {//listCtrl高度
37                     pWndCtrl->MoveWindow(iLeft, iTop + MoveSize, iWidth, iHeight - ListCtrl_ModifyHeight);
38                 }
39             }
40         }
41     }
42     else//不改变控件大小,只改变位置
43     {
44         for(int i = 0; i < 3; i++)
45         {
46             CWnd *pWndCtrl = NULL;
47             if(0 == i) pWndCtrl = GetDlgItem(IDOK);
48             if(1 == i) pWndCtrl = GetDlgItem(IDCANCEL);
49             if(2 == i) pWndCtrl = GetDlgItem(IDC_LIST_PLENTPROPERTY);
50 
51             if ((NULL != pWndCtrl) && IsWindow(pWndCtrl->GetSafeHwnd()))
52             {
53                 CRect rectCtrl;
54                 pWndCtrl->GetWindowRect(rectCtrl);
55                 ScreenToClient(rectCtrl);
56                 int iLeft = rectCtrl.left;
57                 int iTop = rectCtrl.top;
58                 int iWidth = rectCtrl.Width();
59                 int iHeight = rectCtrl.Height();
60                 iTop += MoveSize;
61                 // 把控件移动到新位置
62                 pWndCtrl->MoveWindow(iLeft, iTop, iWidth, iHeight);
63             }
64         }
65     }
66 }

参考:http://zhidao.baidu.com/link?url=PdhhOAWqm93bvjSQexwf8WlT5By1RWQBISO5hmMqZ_VbyVhY6NivX5mh3twkFxD9JODhWgA0XPVrlPl_USTzOa

http://blog.csdn.net/lyshiba/article/details/7489510

原文地址:https://www.cnblogs.com/lpxblog/p/5195205.html