Take this bit of a method:
int CMeetingScheduleAssistantApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
CString strContent = CString(lpszPrompt);
CString strTitle = CString();
if (!CTaskDialog::IsSupported())
return CWinAppEx::DoMessageBox(lpszPrompt, nType, nIDPrompt);
ENSURE(strTitle.LoadString(AFX_IDS_APP_TITLE));
CTaskDialog dlgTaskMessageBox(strContent, _T(""), strTitle);
// =================================
// Can this be calculated just once?
HDC screen = GetDC(nullptr);
auto hSize = static_cast<double>(GetDeviceCaps(screen, HORZSIZE));
auto hRes = static_cast<double>(GetDeviceCaps(screen, HORZRES));
auto PixelsPerMM = hRes / hSize; // pixels per millimeter
auto MaxPixelWidth = PixelsPerMM * 150.0;
auto PixelWidth = (hRes / 100.0) * 30.0;
// =================================
int iDialogUnitsWidth = MulDiv(
min(static_cast<int>(PixelWidth), static_cast<int>(MaxPixelWidth)), 4, LOWORD(GetDialogBaseUnits()));
dlgTaskMessageBox.SetDialogWidth(iDialogUnitsWidth);
// Code snipped
}
Is it possible to adjust this function so that it calculates the MaxPixelWidth
value only once? Without the need to add other variables to my class?
My objective is to allow multiple calls to DoMessageBox
and and only calculate the max width just the once.