Fix file viewer layout

If scaling > 100% is enabled on the primary display, the controls in
the dialog window get bigger.  The file viewer uses hard-coded values
for the initial and minimum window size, which causes the expanded
controls to draw on top of each other.  (Issue #53.)

We now query the system for the scale factor, and adjust the window
size accordingly.
This commit is contained in:
Andy McFadden 2022-01-07 11:34:46 -08:00
parent b42cc8efe9
commit 2cb5bf0dcc
4 changed files with 42 additions and 6 deletions

View File

@ -247,16 +247,20 @@ Preferences::Preferences(void)
SetPrefString(kPrViewTextTypeFace, L"Courier New");
SetPrefLong(kPrViewTextPointSize, 10);
long width = 680 + /* exact width for 80-column text */
::GetSystemMetrics(SM_CXVSCROLL);
long height = 516; /* exact height for file viewer to show IIgs graphic */
float scaleFactor = GetDesktopScaleFactor();
/* exact width for 80-column text; note text is scaled up */
long width = (int)(680 * scaleFactor) + ::GetSystemMetrics(SM_CXVSCROLL);
/* exact height for file viewer to show IIgs graphic at 1.0x */
long height = (int)(516 * scaleFactor);
if (GetSystemMetrics(SM_CXSCREEN) < width)
width = GetSystemMetrics(SM_CXSCREEN);
if (GetSystemMetrics(SM_CYSCREEN) < height)
height = GetSystemMetrics(SM_CYSCREEN); // may overlap system bar
//width = 640; height = 480;
SetPrefLong(kPrFileViewerWidth, width);
SetPrefLong(kPrFileViewerHeight, height);
SetPrefLong(kPrDiskImageCreateFormat, -1);
}

View File

@ -154,8 +154,12 @@ void ViewFilesDialog::OnCancel(void)
void ViewFilesDialog::OnGetMinMaxInfo(MINMAXINFO* pMMI)
{
pMMI->ptMinTrackSize.x = 664;
pMMI->ptMinTrackSize.y = 200;
float scaleFactor = GetDesktopScaleFactor();
// 664x200 was determined empirically to fit all controls. The initial
// window size is set over in the Preferences constructor.
pMMI->ptMinTrackSize.x = (int)(664 * scaleFactor);
pMMI->ptMinTrackSize.y = (int)(200 * scaleFactor);
}
void ViewFilesDialog::OnSize(UINT nType, int cx, int cy)

View File

@ -417,6 +417,27 @@ void CheckedLoadString(CString* pString, UINT nID)
}
}
float GetDesktopScaleFactor()
{
// The current scale factor for controls is determined by the settings
// for the main display at the time the application was launched. If we
// don't do this, the controls will get all scrunched together if the
// scale is over 100% (issue #53). The settings API doesn't seem to
// allow scaling things down, so we're only worried about expansion here.
//
// https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings?view=windows-11#primary-display-dpi-scale-factor
// Get desktop dc
HDC desktopDc = ::GetDC(NULL);
// Get native resolution
int horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
int verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);
float scaleFactor = horizontalDPI / 96.0f;
LOGD("Native DPI: %dx%d, scaleFactor=%.3f",
horizontalDPI, verticalDPI, scaleFactor);
return scaleFactor;
}
/*
* ===========================================================================

View File

@ -199,6 +199,13 @@ bool IsWin9x(void);
*/
void CheckedLoadString(CString* pString, UINT nID);
/*
* Returns the desktop scaling factor (usually 1.0). Assumes vertical and
* horizontal scaling is identical.
*/
float GetDesktopScaleFactor();
/*
* ====================================
* Miscellaneous functions