设置 MFC 对话框的位置
Setting the Position of an MFC Dialog
我需要将 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
方法 所以可能是我在那里做错了什么。
任何指导将不胜感激。
如果 window 是 child window,您需要将相对坐标传递给 SetWindowPos
。 GetWindowRect
returns 你的屏幕(绝对)坐标。对于您的方案,您不能将这些传递给 SetWindowPos
。在 parent 上使用 GetClientRect
并将此 rect 传递给 SetWindowPos
.
RECT rect;
CWnd::GetClientRect(&rect); // Only line changed
searchDialog->SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.bottom,rect.left,rect.bottom,SWP_SHOWWINDOW );
试试这个:
CRect rect;
searchDialog->GetWindowRect(rect);
int dx = rect.Width();
int dy = rect.Height();
GetWindowRect(rect);
rect.left = rect.right - dx;
rect.bottom = rect.top + dy;
searchDialog->MoveWindow(rect);
searchDialog->ShowWindow(SW_SHOW);
这会将您的对话框移动到您父项的右上角位置 window。
我需要将 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
方法 所以可能是我在那里做错了什么。
任何指导将不胜感激。
如果 window 是 child window,您需要将相对坐标传递给 SetWindowPos
。 GetWindowRect
returns 你的屏幕(绝对)坐标。对于您的方案,您不能将这些传递给 SetWindowPos
。在 parent 上使用 GetClientRect
并将此 rect 传递给 SetWindowPos
.
RECT rect;
CWnd::GetClientRect(&rect); // Only line changed
searchDialog->SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.bottom,rect.left,rect.bottom,SWP_SHOWWINDOW );
试试这个:
CRect rect;
searchDialog->GetWindowRect(rect);
int dx = rect.Width();
int dy = rect.Height();
GetWindowRect(rect);
rect.left = rect.right - dx;
rect.bottom = rect.top + dy;
searchDialog->MoveWindow(rect);
searchDialog->ShowWindow(SW_SHOW);
这会将您的对话框移动到您父项的右上角位置 window。