Skip to content

Instantly share code, notes, and snippets.

@Tocchann
Created September 12, 2023 16:48
Show Gist options
  • Save Tocchann/a9277edc9d0e0f6fa59099b2afcd13ec to your computer and use it in GitHub Desktop.
Save Tocchann/a9277edc9d0e0f6fa59099b2afcd13ec to your computer and use it in GitHub Desktop.
AfxMessageBox をこっそり TaskDialog にして、HighDPI 対応するおまじない実装
int CMyApp::DoMessageBox( LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt )
{
// アイコンを整理しておく
if( (nType & MB_ICONMASK) == 0 )
{
switch( nType & MB_TYPEMASK )
{
case MB_OK:
case MB_OKCANCEL:
nType |= MB_ICONEXCLAMATION;
break;
case MB_YESNO:
case MB_YESNOCANCEL:
nType |= MB_ICONQUESTION;
break;
case MB_ABORTRETRYIGNORE:
case MB_RETRYCANCEL:
// No default icon for these types, since they are rarely used.
// The caller should specify the icon.
break;
}
}
#ifdef _DEBUG
if( (nType & MB_ICONMASK) == 0 )
TRACE( traceAppMsg, 0, "Warning: no icon specified for message box.\n" );
#endif
// ボタンを確定する
bool useTaskDialog = true;
int commonButton = 0;
switch( nType & MB_TYPEMASK )
{
case MB_OK:
commonButton = TDCBF_OK_BUTTON;
break;
case MB_OKCANCEL:
commonButton = TDCBF_OK_BUTTON|TDCBF_CANCEL_BUTTON;
break;
case MB_YESNO:
commonButton = TDCBF_YES_BUTTON|TDCBF_NO_BUTTON;
break;
case MB_YESNOCANCEL:
commonButton = TDCBF_YES_BUTTON|TDCBF_NO_BUTTON|TDCBF_CANCEL_BUTTON;
break;
// WPF は Abort 系は対応していないので TaskDialog 版も対応しない
case MB_ABORTRETRYIGNORE:
case MB_RETRYCANCEL:
case MB_CANCELTRYCONTINUE:
useTaskDialog = false;
break;
}
LPCTSTR mainIcon = nullptr;
// アイコンを確定する
switch( nType & MB_ICONMASK )
{
case MB_ICONHAND:
mainIcon = TD_ERROR_ICON;
break;
case MB_ICONQUESTION: // LoadIcon できれば表示できることが判明したのでそのまま使う
mainIcon = IDI_QUESTION;
break;
case MB_ICONEXCLAMATION:
mainIcon = TD_WARNING_ICON;
break;
case MB_ICONASTERISK:
mainIcon = TD_INFORMATION_ICON;
break;
default:
useTaskDialog = false;
break;
}
if( useTaskDialog )
{
CTaskDialog dlg( lpszPrompt, _T( "" ), m_pszAppName, commonButton, TDF_USE_HICON_MAIN );
dlg.SetMainIcon( mainIcon );
auto result = dlg.DoModal();
return static_cast<int>(result);
}
else
{
return DoMessageBox( lpszPrompt, nType, nIDPrompt );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment