设置基于MFC对话框的应用程序窗口的位置

设置基于MFC对话框的应用程序窗口的位置,mfc,cdialog,Mfc,Cdialog,我有一个基于CDialog的MFC应用程序。程序结束时,我保存窗口的当前位置。当程序启动时,我想恢复以前的位置。我目前正试图在OnInitDialog()中执行此操作,但是,当我从OnInitDialog()中调用SetWindowPos()时,程序会断言。我对SetWindowPos()的调用类似于: SetWindowPos(&CWnd::wndTop, 10, 10, 500, 500, SWP_NOREDRAW | SWP_NOZORDER); 该断言与nullm_hWnd句柄

我有一个基于
CDialog
的MFC应用程序。程序结束时,我保存窗口的当前位置。当程序启动时,我想恢复以前的位置。我目前正试图在
OnInitDialog()
中执行此操作,但是,当我从
OnInitDialog()中调用
SetWindowPos()
时,程序会断言。我对
SetWindowPos()
的调用类似于:

SetWindowPos(&CWnd::wndTop, 10, 10, 500, 500, SWP_NOREDRAW | SWP_NOZORDER);
该断言与null
m_hWnd
句柄相关

这是重新定位基于对话框的应用程序窗口的正确位置吗


你知道我为什么这么说吗?

在为我的初始问题提供更多信息的过程中,我发现调用
SetWindowPos()
会将消息传递给对话框中尚未创建的工具栏。我将
SetWindowPos()
移动到了
oninitialdialog()的末尾,它成功了

谢谢你的鼓励,@zett42.

我就是这样做的:

#包括“stdafx.h”
#包括“resource.h”
#包括“ResizingDialog.h”
实施动态(CResizingDialog、CDialogEx)
CResizingDialog::CResizingDialog(const CString&strWindowID,UINT nIDTemplate,CWnd*pParent/*nullptr*/,bool bOnlyStorePosition/*false*/)
:CDialogEx(nIDTemplate,pParent)
,m_strWindowID(strWindowID)
,m_bOnlyStorePosition(bOnlyStorePosition)
,m_bdonotshowlesizeicon(假)
{
m_rcInit.SetRect(0,0,0,0);
}
CResizingDialog::~CResizingDialog()
=违约;
void CResizingDialog::DoDataExchange(CDATA Exchange*pDX)
{
CDialogEx::DoDataExchange(pDX);
}
开始消息映射(CResizingDialog,CDialogEx)
关于_WM_GETMINMAXINFO()
关于_WM_DESTROY()
关于油漆
关于WM_NCHITTEST()
关于_WM_SIZE()
结束消息映射()
BOOL CResizingDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
//将初始窗口大小保存到m_rcInit
GetWindowRect(&m_rcInit);
//如果(!m_bOnlyStorePosition&!m_bDoNotShowResizeIcon)
//InitializeresizeIcon(m_bmpResize,m_lblResize,this);
恢复内部位置(m_strWindowID,this,true);
return TRUE;//除非将焦点设置为控件,否则返回TRUE
//异常:OCX属性页应返回FALSE
}
void CResizingDialog::OnGetMinMaxInfo(MINMAXINFO*lpMMI)
{
//将最小窗口大小设置为初始大小。
lpMMI->ptMinTrackSize.x=m_rcInit.Width();
lpMMI->ptMinTrackSize.y=m_rcInit.Height();
CDialogEx::OnGetMinMaxInfo(lpMMI);
}
void CResizingDialog::RestoreWindowPosition(CString strWindow、CWnd*pWindow、bool bOverrideState)
{
int max_x,max_y;
矩形窗口;
if(pWindow==nullptr)
返回;
//仅当存在以前保存的位置时恢复
如果((rtWindow.top=AfxGetApp()->GetProfileInt(strWindow,_T(“top”),-1))!=-1&&
(rtWindow.left=AfxGetApp()->GetProfileInt(strWindow,_T(“left”),-1))!=-1&&
(rtWindow.bottom=AfxGetApp()->GetProfileInt(strWindow,_T(“bottom”),-1))!=-1&&
(rtWindow.right=AfxGetApp()->GetProfileInt(strWindow,_T(“right”),-1)))
{
max_x=rtWindow.right-rtWindow.left;
max_y=rtWindow.bottom-rtWindow.top;
//控制显示器
HMONITOR HMONITOR=::MonitorFromPoint(
CPoint(rtWindow.left,rtWindow.top),监视器\u DEFAULTTONEAREST);
//获取监视器信息
MONITORINFO monInfo;
monInfo.cbSize=sizeof(MONITORINFO);
如果(::GetMonitorInfo(hMonitor,&MonitorInfo)==0)
AfxMessageBox(_T(“GetMonitorInfo失败”);
其他的
{
//调整工作区域
rtWindow.left+=monInfo.rcWork.left-monInfo.rcMonitor.left;
rtWindow.top+=monInfo.rcWork.top-monInfo.rcMonitor.top;
//确保左上角点在屏幕上
if(corct(monInfo.rcWork).PtInRect(CPoint(rtWindow.left,rtWindow.top))==FALSE)
{
rtWindow.left=monInfo.rcWork.left;
rtWindow.top=monInfo.rcWork.top;
}
rtWindow.right=rtWindow.left+max_x;
rtWindow.bottom=rtWindow.top+max_y;
//恢复窗口大小
pWindow->MoveWindow(&rtWindow,FALSE);
}
如果(bOverrideState)
{
//让我们通过恢复窗口状态来覆盖
int iState=AfxGetApp()->GetProfileInt(strWindow,_T(“ShowCmd”),SW_SHOWNORMAL);
pWindows->ShowWindow(iState);
}
}
}
void CResizingDialog::SaveWindowPosition(CString strWindow,CWnd*pWindow)
{
窗口放置可湿性粉剂;
if(pWindow==nullptr)
返回;
pWindow->GetWindowPlacement(&wp);
//提交到注册表
AfxGetApp()->writeprofile(strWindow,_T(“Top”),wp.rcNormalPosition.Top);
AfxGetApp()->writeprofile(strWindow,_T(“左”),wp.rcNormalPosition.Left);
AfxGetApp()->writeprofile(strWindow,_T(“底部”),wp.rcNormalPosition.Bottom);
AfxGetApp()->writeprofile(strWindow,_T(“右”),wp.rcNormalPosition.Right);
AfxGetApp()->writeprofile(strWindow,_T(“ShowCmd”),wp.ShowCmd);
}
void CResizingDialog::InitializerizeIcon(CBitmap和rBmpResize、CStatic和rLblResize、CWnd*pDialog)
{
正确的rcIcon,RCC客户;
if(pDialog!=nullptr)
{
rBmpResize.LoadOEMBitmap(OBM_大小);
rLblResize.Create(nullptr,WS|u CHILD | WS|u VISIBLE | SS|u位图,
正确(0,0,16,16),pDialog,IDC\u STATIC\u RESIZE);
rLblResize.SetBitmap(rBmpResize);
//app.UpdateBitmapBackground(rLblResize.GetBitmap(),true,GetSysColor(COLOR\u ACTIVECAPTION));
pDialog->GetClientRect(rcClient);
rLblResize.GetClientRect(rcIcon);
rLblResize.SetWindowPos(&CWnd::wndTop),
rcClient.right-rcIcon.Width(),
rcClient.bottom-rcIcon.Height(),0,0,SWP_NOSIZE);
CMFCDynamicLayout*pddynamiclayout=pDialog->GetDynamicLayout();
if(pdynamicallayout!=nullptr)