Improved dialog base units calculations on Windows.

This commit is contained in:
Pietro Gagliardi 2014-08-12 13:10:30 -04:00
parent 9ecd124500
commit 24a42d29a5
2 changed files with 5 additions and 1 deletions

View File

@ -88,6 +88,7 @@ void calculateBaseUnits(HWND hwnd, int *baseX, int *baseY, LONG *internalLeading
HDC dc;
HFONT prevFont;
TEXTMETRICW tm;
SIZE size;
dc = GetDC(hwnd);
if (dc == NULL)
@ -97,7 +98,9 @@ void calculateBaseUnits(HWND hwnd, int *baseX, int *baseY, LONG *internalLeading
xpanic("error loading control font into device context for preferred size calculation", GetLastError());
if (GetTextMetricsW(dc, &tm) == 0)
xpanic("error getting text metrics for preferred size calculations", GetLastError());
*baseX = (int) tm.tmAveCharWidth; /* TODO not optimal; third reference below has better way */
if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
xpanic("error getting text extent point for preferred size calculations", GetLastError());
*baseX = (int) ((size.cx / 26 + 1) / 2);
*baseY = (int) tm.tmHeight;
*internalLeading = tm.tmInternalLeading;
if (SelectObject(dc, prevFont) != controlFont)

View File

@ -86,6 +86,7 @@ func containerResize(data unsafe.Pointer, r *C.RECT) {
// - http://msdn.microsoft.com/en-us/library/ms645502%28VS.85%29.aspx - the calculation needed
// - http://support.microsoft.com/kb/125681 - to get the base X and Y
// (thanks to http://stackoverflow.com/questions/58620/default-button-size)
// In my tests (see https://github.com/andlabs/windlgunits), the GetTextExtentPoint32() option for getting the base X produces much more accurate results than the tmAveCharWidth option when tested against the sample values given in http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing, but can be off by a pixel in either direction (probably due to rounding errors).
// note on MulDiv():
// div will not be 0 in the usages below