简体   繁体   English

如何在MFC中设置对话框的默认大小(以像素为单位)

[英]How to set the default size in pixels of dialog in MFC

I want to set the default size in pixels of dialog, say it is 640 pixel width and 384 pixel height. 我想设置对话框的默认大小(以像素为单位),比如640像素宽和384像素高。 what I mean by the default is that when the first time the CXXXDlg::OnSize(UINT nType, int cx, int cy) is called, the value of cx is 640 and the value of cy is 384. scene the default size of the dialog is in dialog units, and I can use the MapDialogRect() to convert the dialog units to the pixels, How can I do the reverse? default我的意思是,当第一次CXXXDlg::OnSize(UINT nType, int cx, int cy)cx值为640, cy值为384. scene的默认大小为对话框是对话框单元,我可以使用MapDialogRect()将对话框单位转换为像素,我该怎么做呢? the MoveWindow() and the SetWindowPos() can set eh size of the dialog but not the default size. MoveWindow()SetWindowPos()可以设置对话框的大小,但不能设置默认大小。 I also have tried the GetDialogBaseUnits() like this: 我也试过像这样的GetDialogBaseUnits()

DWORD dw = GetDialogBaseUnits();
WORD m_duXx4 = LOWORD(dw);
WORD m_duYx8 = HIWORD(dw);
int dialogUnitX = MulDiv(640, 4, m_duXx4);
int dialogUnitY = MulDiv(384, 8, m_duYx8);

it turned out that the dialogUnitX is 320 and the dialogUnitY is 192 , but when I set the dialog unit to 320 * 192 , what I got in CXXXDlg::OnSize(UINT nType, int cx, int cy) is not 640 * 384 but 560 * 336 . 事实证明, dialogUnitX320 ,而dialogUnitY192 ,但当我将对话框单元设置为320 * 192 ,我在CXXXDlg::OnSize(UINT nType, int cx, int cy)得到的不是640 * 384但是560 * 336 Any ideas? 有任何想法吗?

A window consists of a Client Area and a Nonclient Area . 窗口由客户区非客户区组成

The client area is the part of a window where the application displays output, such as text or graphics. 客户端区域是应用程序显示输出的窗口的一部分,例如文本或图形。

The title bar, menu bar, window menu, minimize and maximize buttons, sizing border, and scroll bars are referred to collectively as the window's nonclient area . 标题栏,菜单栏,窗口菜单,最小化和最大化按钮,大小调整边框和滚动条统称为窗口的非客户区域

The Window Rect designates the area that encompasses the entire window. Window Rect指定包含整个窗口的区域。 It includes the client area as well as the nonclient area. 它包括客户区域和非客户区域。 It can be retrieved by calling GetWindowRect (or its MFC-equivalent). 可以通过调用GetWindowRect (或其MFC等效项)来检索它。 It is also used as the input to functions like MoveWindow or SetWindowPos . 它还用作MoveWindowSetWindowPos等函数的输入。

The Client Rect is the area of a window that is not occupied by the nonclient area. Client Rect是非客户区域未占用的窗口区域。 It can be queried by calling GetClientRect . 可以通过调用GetClientRect来查询它。 The client rect dimensions are passed to the WM_SIZE message handler. 客户端rect维度传递给WM_SIZE消息处理程序。

If an application requires a specific size for its client area it can calculate the respective window rect by calling AdjustWindowRect or AdjustWindowRectEx . 如果应用程序需要特定大小的客户区,则可以通过调用AdjustWindowRectAdjustWindowRectEx来计算相应的窗口rect。

The window rect is usually expressed in Screen Coordinates while the client rect uses Client Coordinates . 窗口rect通常以屏幕坐标表示,而客户端rect使用客户端坐标 Both coordinate systems represent device pixels. 两个坐标系都代表设备像素。 The origin is in the top left corner of the primary display for screen coordinates and the top left corner of the client area for client coordinates. 原点位于屏幕坐标主显示屏的左上角,客户端坐标的客户区左上角。 To translate between the coordinate systems an application uses ClientToScreen or ScreenToClient . 要在坐标系之间进行转换,应用程序使用ClientToScreenScreenToClient

Dialog templates specify dimensions and positions in Dialog Template Units . 对话框模板在对话框模板单元中指定尺寸和位置。 Dialog template units are directly related to a dialog's font. 对话框模板单元与对话框的字体直接相关。 To convert between dialog template units and device pixels an application calls MapDialogRect . 要在对话框模板单元和设备像素之间进行转换,应用程序会调用MapDialogRect There is no API call to calculate the reverse. 没有API调用来计算反向。 An application has to perform the calculations manually: 应用程序必须手动执行计算:

width  = MulDiv(width,  4, baseunitX);
height = MulDiv(height, 8, baseunitY);

If an application wants to confine the window size dynamically it can handle the WM_GETMINMAXINFO message and populate a MINMAXINFO structure with the desired dimensions. 如果应用程序想要动态限制窗口大小,它可以处理WM_GETMINMAXINFO消息并使用所需的维度填充MINMAXINFO结构。 This message is sent to a window when the size or position of the window is about to change. 当窗口的大小或位置即将改变时,该消息被发送到窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM