简体   繁体   中英

win32 controls: different coordinates in resource.rc and CreateWindow?

I'm creating a small win32 application. Currently I have 6 text labels coded in the resource file, like this:

IDD_MAIN DIALOGEX 0, 0, 465, 279
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "SpiderPigOverseer"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    CONTROL         "sample text",IDC_STATIC1,"Static",SS_SIMPLE | WS_GROUP,344,70,33,8
    [...]
END

But I want to access these in a for loop so I thought I would create them dynamically(?) with this code:

IRValues[i] = CreateWindow("static", "sample text", SS_SIMPLE | WS_VISIBLE | WS_CHILD, 344, 70+10*i, 33, 8, hDlg, NULL, NULL, NULL);

But, this makes the text bigger. The x and y-position are much less then the ones created with resource file and the width and height also seems to differ. Why is this?

EDIT: I tried using the following code to convert, but couldn't get it to match exactly.

HDC hdc = GetDC(hWnd);
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
cxAveChar = tm.tmAveCharWidth;
cyAveChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC( hWnd, hdc );

Coordinates in .rc files represented as Dialog units which are not equal to pixels. It is made for automatically adjust controls size depending on system font size. You can convert dialog units to pixels as follows:

pixelX = MulDiv(templateunitX, LOWORD(GetDialogBaseUnits()), 4);
pixelY = MulDiv(templateunitY, HIWORD(GetDialogBaseUnits()), 8);

Or simply use MapDialogRect()

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