visual-c++ - 在 MFC 中使用 TaskDialogIndirect 时出现异常
问题描述
我正在尝试以下代码,因为我想使用验证复选框功能:
const HICON hQuestionIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
CString strTitle = CString();
CString strMainInstruction = CString();
CString strContent = CString();
CString strAdditional = CString();
CString strFooter = CString();
CString strExpand = CString();
CString strCollapse = CString();
ENSURE(strTitle.LoadString(AFX_IDS_APP_TITLE));
ENSURE(strMainInstruction.LoadString(IDS_STR_SUBMIT_STATS_MAIN_TEXT));
ENSURE(strContent.LoadString(IDS_STR_SUBMIT_STATS_CONTENT_TEXT));
ENSURE(strAdditional.LoadString(IDS_STR_SUBMIT_STATS_ADDITIONAL_TEXT));
ENSURE(strFooter.LoadString(IDS_STR_TASK_DIALOG_FOOTER));
ENSURE(strExpand.LoadString(IDS_STR_FIND_OUT_MORE));
ENSURE(strCollapse.LoadString(IDS_STR_COLLAPSE));
TASKDIALOGCONFIG sConfig = { 0 };
sConfig.cbSize = sizeof(TASKDIALOGCONFIG);
sConfig.hInstance = AfxGetResourceHandle();
sConfig.dwCommonButtons = TDCBF_YES_BUTTON | TDCBF_NO_BUTTON;
sConfig.hMainIcon = hQuestionIcon;
sConfig.pszMainInstruction = strMainInstruction.GetString();
sConfig.pszContent = strContent.GetString();
sConfig.pszExpandedControlText = strAdditional.GetString();
sConfig.pszFooter = strFooter.GetString();
sConfig.pszCollapsedControlText = strExpand.GetString();
sConfig.pszExpandedControlText = strCollapse.GetString();
sConfig.pszFooterIcon = TD_INFORMATION_ICON;
sConfig.pszVerificationText = _T("Stop displaying this message");
sConfig.cxWidth = CMeetingScheduleAssistantApp::DetectMessageBoxWidth();
int iButtonPressed = IDNO; // Default
BOOL bStopDisplayingMessage = FALSE;
TaskDialogIndirect(&sConfig, &iButtonPressed, NULL, &bStopDisplayingMessage);
if (bStopDisplayingMessage)
{
CChristianLifeMinistryUtils::HidePromptToSubmitWorkbookDownloadStats();
}
return iButtonPressed;
但我遇到了一个例外:
我可以做一个常规的CTaskDialog
(没有复选框),这很好。
解决方案
虽然我在问题中向您展示的代码存在其他问题,但异常的解决方案是设置父级:
sConfig.hwndParent = GetSafeHwnd();
这里不讨论或提及。我通过查看源代码发现了它。TASKDIALOGCONFIG
主题说它NULL
可以。
无论哪种方式,现在都不会发生异常。
完整代码
const HICON hQuestionIcon = AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
CString strTitle;
CString strMainInstruction;
CString strContent;
CString strAdditional;
CString strFooter;
CString strExpand;
CString strCollapse;
ENSURE(strTitle.LoadString(AFX_IDS_APP_TITLE));
ENSURE(strMainInstruction.LoadString(IDS_STR_SUBMIT_STATS_MAIN_TEXT));
ENSURE(strContent.LoadString(IDS_STR_SUBMIT_STATS_CONTENT_TEXT));
ENSURE(strAdditional.LoadString(IDS_STR_SUBMIT_STATS_ADDITIONAL_TEXT));
ENSURE(strFooter.LoadString(IDS_STR_TASK_DIALOG_FOOTER));
ENSURE(strExpand.LoadString(IDS_STR_FIND_OUT_MORE));
ENSURE(strCollapse.LoadString(IDS_STR_COLLAPSE));
TASKDIALOGCONFIG sConfig = { 0 };
sConfig.hwndParent = GetSafeHwnd();
sConfig.cbSize = sizeof(TASKDIALOGCONFIG);
sConfig.dwFlags = TDF_ENABLE_HYPERLINKS | TDF_USE_HICON_MAIN;
sConfig.hInstance = AfxGetResourceHandle();
sConfig.dwCommonButtons = TDCBF_YES_BUTTON | TDCBF_NO_BUTTON;
sConfig.hMainIcon = hQuestionIcon;
sConfig.pszMainInstruction = strMainInstruction.GetString();
sConfig.pszContent = strContent.GetString();
sConfig.pszExpandedInformation = strAdditional.GetString();
sConfig.pszFooter = strFooter.GetString();
sConfig.pszCollapsedControlText = strExpand.GetString();
sConfig.pszExpandedControlText = strCollapse.GetString();
sConfig.pszFooterIcon = TD_INFORMATION_ICON;
sConfig.pszVerificationText = _T("Stop displaying this message");
sConfig.cxWidth = CMeetingScheduleAssistantApp::DetectMessageBoxWidth();
int iButtonPressed = IDNO; // Default
BOOL bStopDisplayingMessage = FALSE;
HRESULT hResult = TaskDialogIndirect(&sConfig, &iButtonPressed, NULL, &bStopDisplayingMessage);
if (hResult == S_OK)
{
if (bStopDisplayingMessage)
{
CChristianLifeMinistryUtils::HidePromptToSubmitWorkbookDownloadStats();
}
}
return iButtonPressed;
推荐阅读
- python - 如何将多个 Excel 文件发送到 Outlook 中的多个组?
- jwt - 除了 ADB2C Token 之外,单独创建另一个 JWT Token 进行授权
- node.js - npm 包没有安装 clarifai-nodejs-grpc
- reactjs - 自定义图例并不总是使用 react-chartjs-2 呈现
- ios - 迁移到 xcframwork Xcode 12.5,dyld:未加载库
- c++ - 如何使用 NODEMCU 同时使用 MFRC522 和 RDM6300
- windows-machine-learning - 访问 TensorFloat 数据
- python - 我正在尝试使用回溯解决 N 个皇后。我的解决方案是计算值,但是返回逻辑有问题
- javascript - 尝试在单击按钮时从两个不同的文本区域复制文本
- javascript - 在烧瓶中正确执行 javasript 代码