【问题标题】:Setting the Position of an MFC Dialog设置 MFC 对话框的位置
【发布时间】: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 );

标签: c++ winapi mfc


【解决方案1】:

如果窗口是子窗口,您需要将相对坐标传递给SetWindowPos。 GetWindowRect 返回屏幕(绝对)坐标。对于您的方案,您不能将这些传递给SetWindowPos。在父级上使用GetClientRect 并将此矩形传递给SetWindowPos。

RECT rect;
CWnd::GetClientRect(&rect);   // Only line changed
searchDialog->SetWindowPos(&CWnd::wndNoTopMost,rect.left,rect.bottom,rect.left,rect.bottom,SWP_SHOWWINDOW );

【讨论】:

  • @ItbanSaeed 仅用 GetClientRect 替换 GetWindowRect。
【解决方案2】:

试试这个:

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);

这会将对话框移动到父窗口的右上角。

【讨论】:

  • 非常感谢它帮助了我 :) 但我需要将对话框移动到顶部 right 角。我应该修改什么?
  • @ItbanSaeed :我的回答实际上是错误的。我对其进行了编辑,现在它可以满足您的需求。
  • 非常感谢,它有效!我希望你能再给一个赞成票:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 2012-12-23
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多