C++ 在 MFC 中调整控件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/127514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:04:18  来源:igfitidea点击:

Resizing Controls in MFC

c++mfc

提问by Konrad

I am writing a program which has two panes (via CSplitter), however I am having problems figuring out out to resize the controls in each frame. For simplicity, can someone tell me how I would do it for a basic frame with a single CEditcontrol?

我正在编写一个具有两个窗格(通过CSplitter)的程序,但是我在弄清楚如何调整每一帧中的控件大小时遇到​​了问题。为简单起见,有人可以告诉我如何使用单个CEdit控件对基本框架进行操作吗?

I'm fairly sure it is to do with the CEdit::OnSize()function... But I'm not really getting anywhere...

我相当确定这与CEdit::OnSize()功能有关......但我并没有真正到达任何地方......

Thanks! :)

谢谢!:)

采纳答案by Rob

When your frame receives an OnSize message it will give you the new width and height - you can simply call the CEdit SetWindowPos method passing it these values.

当您的框架收到 OnSize 消息时,它将为您提供新的宽度和高度 - 您可以简单地调用 CEdit SetWindowPos 方法将这些值传递给它。

Assume CMyPane is your splitter pane and it contains a CEdit you created in OnCreate called m_wndEdit:

假设 CMyPane 是您的拆分器窗格,它包含您在 OnCreate 中创建的名为 m_wndEdit 的 CEdit:

void CMyPane::OnSize(UINT nType, int cx, int cy)
{
    m_wndEdit.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}

回答by Serge

A window receives WM_SIZE message (which is processed by OnSize handler in MFC) immediately after it was resized, so CEdit::OnSize is not what you are looking for.

窗口在调整大小后立即收到 WM_SIZE 消息(由 MFC 中的 OnSize 处理程序处理),因此 CEdit::OnSize 不是您要查找的。

You should add OnSize handler in your frame class and inside this handler as Rob pointed outyou'll get width and height of the client area of your frame, then you should add the code which adjusts size and position of your control.

你应该在你的框架类和这个处理程序中添加 OnSize 处理程序,正如 Rob指出的那样,你将获得框架客户区的宽度和高度,然后你应该添加调整控件大小和位置的代码。

Something like this

像这样的东西

void MyFrame::OnSize(UINT nType, int w, int h)
{
    // w and h parameters are new width and height of your frame
    // suppose you have member variable CEdit myEdit which you need to resize/move
    myEdit.MoveWindow(w/5, h/5, w/2, h/2);
}

回答by Eddie

GetDlgItem(IDC_your_slidebar)->SetWindowPos(...) // actually you can move ,resize...etc

GetDlgItem(IDC_your_slidebar)->SetWindowPos(...) // 实际上你可以移动、调整大小...等

回答by ravenspoint

SetWindowPos is a little heavy duty for this purpose. MoveWindow has just what is needed.

SetWindowPos 为此目的有点繁重。MoveWindow 正是需要的。

回答by Brian Ensink

Others have pointed out that WM_SIZE is the message you should handle and resize the child controls at that point. WM_SIZE is sent after the resize has finished.

其他人指出 WM_SIZE 是您应该在此时处理和调整子控件大小的消息。WM_SIZE 在调整大小完成后发送。

You might also want to handle the WM_SIZING message which gets sent while the resize is in progress. This will let you actively resize the child windows while the user is still dragging the mouse. Its not strictly necessary to handle WM_SIZING but it can provide a better user experience.

您可能还想处理在调整大小进行时发送的 WM_SIZING 消息。这将让您在用户仍在拖动鼠标时主动调整子窗口的大小。处理 WM_SIZE 并不是绝对必要的,但它可以提供更好的用户体验。

回答by Sergey Kornilov

I use CResize class from CodeGuru to resize all controls automatically. You tell how you want each control to be resized and it does the job for you.

我使用 CodeGuru 的 CResize 类自动调整所有控件的大小。您告诉您希望如何调整每个控件的大小,它会为您完成工作。

The resize paradigm is to specify how much each side of a control will move when the dialog is resized.

调整大小范例是指定在调整对话框大小时控件的每一侧将移动多少。

SetResize(IDC_EDIT1, 0,   0,   0.5, 1);
SetResize(IDC_EDIT2, 0.5, 0,   1,   1);

Very handy when you have a large number of dialog controls.

当您有大量对话框控件时非常方便。

Source code

源代码

回答by nUOs

When it comes to the window size changes, there are three window messages you may be interested in: ON_WM_SIZE(), ON_WM_SIZING(), and ON_WM_GETMINMAXINFO().

当涉及到窗口大小的变化,也有可能有兴趣在三个窗口消息:ON_WM_SIZE()ON_WM_SIZING(),和ON_WM_GETMINMAXINFO()

As the official docssays:

正如官方文档所说:

  • ON_WM_SIZEwhose message handler is ::OnSize()is triggered after the size of the CWnd has changed;
  • ON_WM_SIZINGwhose message handler is ::OnSizing()is triggered when the size of the client area of the clipbord-viewer window has changed;
  • ON_WM_GETMINMAXINFOwhose message handler is ::OnGetMinMaxInfo()is triggered whenever the window needs to know the maximized position or dimensions , or the minimum or maximum tracking size.
  • ON_WM_SIZE::OnSize()在 CWnd 的大小改变后触发其消息处理程序;
  • ON_WM_SIZING::OnSizing()当剪贴板查看器窗口的客户区大小改变时触发其消息处理程序;
  • ON_WM_GETMINMAXINFO::OnGetMinMaxInfo()只要窗口需要知道最大化的位置或尺寸,或者最小或最大跟踪尺寸,就会触发其消息处理程序。

If you want to restrict the size of the cwndto some range, you may refer to message ON_WM_GETMINMAXINFO; and if you want to retrieve the size changes in real time, you may refer to the other two messages.

如果您想将 的大小限制在cwnd某个范围内,您可以参考 message ON_WM_GETMINMAXINFO; 如果您想实时检索大小变化,您可以参考其他两个消息。