【发布时间】:2015-08-25 11:15:18
【问题描述】:
我需要将MFC Dialog 的位置设置为某个relative position(右上角)到它的父对话框。我正在使用以下代码:
MainFrame.h:
/*Creation of Dialog*/
SearchCommands* searchDialog;
MainFrame.cpp:
/*In Constructor*/
searchDialog = new SearchCommands();
/*In an Init Method*/
if (!searchDialog->Create(IDD_COMMANDS_SEARCH, this))
{
TRACE0("Failed to create Search Dialog\n");
delete searchDialog;
}
/*Button Click Event*/
void CMainFrame::OnSearchButton()
{
/*Get the ordinates of parent*/
RECT rect;
CWnd::GetWindowRect(&rect);
/*searchDialog is the pointer to a dynamically created Dialog*/
searchDialog->SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.bottom,rect.left,rect.bottom,SWP_SHOWWINDOW );
searchDialog->ShowWindow(SW_SHOWNORMAL);
}
但对话框消失了,而不是正确显示。我不明白应该如何将参数传递给CWnd::SetWindowPos 方法,所以我可能在那里做错了什么。
任何指导将不胜感激。
【问题讨论】:
-
显示更多代码。此代码在
OninitDialog中吗?另外:试试MoveWindow,它比SetWindowPos更简单。 -
我在按钮单击事件中打开对话框,所以这是父窗口中
OnSearch事件的代码。 -
请编辑您的问题并显示更多代码。您的
SetWindowPosition或MoveWindow必须在OnititDialog中。 -
@MichaelWalz 现在请看代码,这就是我想要做的
-
还有一件事是错误的:
SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.bottom,rect.left,rect.bottom,SWP_SHOWWINDOW );应该是SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW );