Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

winapi

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 );
like image 694
Moberg Avatar asked Dec 22 '22 14:12

Moberg


1 Answers

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()

like image 129
pure cuteness Avatar answered Dec 29 '22 01:12

pure cuteness