I'd like to center a dialog box in my Win32 application, but am having trouble passing constants to my resource.rc file. I make the following declaration in resource.h :
const int SCREENX = GetSystemMetrics(SM_CXSCREEN);
However, when I replace 100 with SCREENX in my .rc file (below) and build, I get: error RC2108: expected numerical dialog constant .
#include <windows.h>
#include "resource.h"
#include "afxres.h"
// I'd like to replace "100" with "GetSystemMetrics(SM_CXSCREEN)"
ID_ABOUT DIALOG DISCARDABLE 100, 0, 237, 87
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 10, "MS Sans Serif"
BEGIN
GROUPBOX "Contact", IDC_CONTACT, 7, 43, 98, 39, WS_CHILD | WS_VISIBLE
LTEXT "My info", IDC_CONTACT, 16, 53, 85, 25, WS_CHILD | WS_VISIBLE
END
Neither can I use GetSystemMetrics(SM_CXSCREEN) directly in the desired location (same error).
I'd think it would be routine to pass data types to .rc , so I must be missing something basic here. (Resource files are giving me no end of headaches.) Thank you!
A resource file is compiled into a bunch of static data that is stored in your executable. For example, a dialog resource might be compiled into a DLGTEMPLATE
structure.
In other words, everything in a compiled resource is a constant.
GetSystemMetrics(SM_CXSCREEN)
is not a constant. It evaluates to the current screen width in pixels at run-time.
As the other answer says, to center your dialog use the DS_CENTER
style or handle WM_INITDIALOG
.
To center a Dialog on the screen, just OR the DS_CENTER style to the other window style for the dialog.
If you want to use GetSystemMetrics, do this in WM_INITDIALOG and position the dialog there.
A resource file is just a script.
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.