简体   繁体   中英

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. 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? the MoveWindow() and the SetWindowPos() can set eh size of the dialog but not the default size. I also have tried the GetDialogBaseUnits() like this:

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 . 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. It includes the client area as well as the nonclient area. It can be retrieved by calling GetWindowRect (or its MFC-equivalent). It is also used as the input to functions like MoveWindow or SetWindowPos .

The Client Rect is the area of a window that is not occupied by the nonclient area. It can be queried by calling GetClientRect . The client rect dimensions are passed to the WM_SIZE message handler.

If an application requires a specific size for its client area it can calculate the respective window rect by calling AdjustWindowRect or AdjustWindowRectEx .

The window rect is usually expressed in Screen Coordinates while the client rect uses Client Coordinates . 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 .

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 . There is no API call to calculate the reverse. 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. This message is sent to a window when the size or position of the window is about to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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