2

I am having problems setting the width. This is my code:

void CWin32FileError::DisplayExceptionMessage()
{
    CString strContent = _T(""); // Default shouldn't happen

    if (!m_strErrorFile1.IsEmpty() && !m_strErrorFile2.IsEmpty())
    {
        strContent.Format(_T("Source file: %s\nTarget file: %s"),
            (LPCTSTR)m_strErrorFile1, (LPCTSTR)m_strErrorFile2);

    }
    else
    {
        strContent.Format(_T("File: %s"), (LPCTSTR)m_strErrorFile1);
    }

    CTaskDialog dlgTaskError(strContent, Description(), _T("Exception"), TDCBF_OK_BUTTON);
    dlgTaskError.SetWindowTitle(_T("Exception"));
    dlgTaskError.SetMainIcon(TD_ERROR_ICON);
    dlgTaskError.SetFooterIcon(TD_INFORMATION_ICON);
    dlgTaskError.SetFooterText(m_strActionDescription);

    //dlgTaskError.SetDialogWidth(::GetSystemMetrics(SM_CXSCREEN) / 2);
    dlgTaskError.SetDialogWidth(300);

    dlgTaskError.DoModal();
}

This is what the message box looks like:

Task Dialog

The above screen show was using the value 300. Originally i was trying to use:

dlgTaskError.SetDialogWidth(::GetSystemMetrics(SM_CXSCREEN) / 2);

It did not like it and was nearly as wide as the screen. If I used 500 then it looked like it was nearly 50% of the screen. Yet my screen is just under 2000 pixels wide.

The help documentation states the parameter is pixels so why is my code now showing the message box at 50% of the screen width (if I enable my code)?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

3

Looks like the MFC documentation is wrong. This is what the Win32 documentation says about TASKDIALOGCONFIG::cxWidth:

The width of the task dialog's client area, in dialog units. If 0, the task dialog manager will calculate the ideal width.

The Win32 documentation applies, because CTaskDialog::SetDialogWidth() effectively just sets the TASKDIALOGCONFIG::cxWidth member.

zett42
  • 25,437
  • 3
  • 35
  • 72
  • Thank you. So how do I set the width of the dialog to 50% or 75% of the screen width in dialog units? Bearing in mind that if a person is two two monitors which span from one monitor to another we just want the width of the monitor the task dialog is showing on? – Andrew Truckle Apr 21 '19 at 17:18
  • Got it: `DWORD dw = GetDialogBaseUnits(); int iWidth = MulDiv(::GetSystemMetrics(SM_CXSCREEN) / 2, 4, LOWORD(dw)); dlgTaskError.SetDialogWidth(iWidth);` – Andrew Truckle Apr 21 '19 at 17:32
  • @AndrewTruckle I'm surprised that this works, because `GetDialogBaseUnits()` is supposed to work only for the "system font", which is **not** the standard font used by dialog boxes. Propably it works by chance, if the metrics of the two fonts are similar. Make sure to test with localized Windows (e. g. by installing Chinese language pack) and different DPI factors. – zett42 Apr 21 '19 at 17:37
  • Well it does not have to be exact. I just want the dialog wider so that it looks nicer for the paths IMHO. – Andrew Truckle Apr 21 '19 at 18:02