Mfc 如何显示从调用弹出对话框到用户可见对话框的等待光标?
因此,我想向用户显示一个Mfc 如何显示从调用弹出对话框到用户可见对话框的等待光标?,mfc,modal-dialog,Mfc,Modal Dialog,因此,我想向用户显示一个CDialog: void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions() { COutlookCalendarSettingsDlg dlgSettings(this); dlgSettings.DoModal(); } 现在,弹出对话框(在OnInitDialog中)在后台运行控制台应用程序。此控制台应用程序正在与Microsoft Graph进行通信 因此,显示对话框
CDialog
:
void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions()
{
COutlookCalendarSettingsDlg dlgSettings(this);
dlgSettings.DoModal();
}
现在,弹出对话框(在OnInitDialog
中)在后台运行控制台应用程序。此控制台应用程序正在与Microsoft Graph进行通信
因此,显示对话框可能需要几秒钟的时间
我使用以下方法执行控制台应用程序:
bool CMeetingScheduleAssistantApp::ExecuteProgram(CString strCommand, DWORD& rExitCode)
{
PROCESS_INFORMATION processInformation = { nullptr };
STARTUPINFO startupInfo = { 0 };
int nStrBuffer;
BOOL bProcessResult, bExitCodeProcess;
bool bOK = false;
CWaitCursor wait;
SetProgramExecuting(true);
rExitCode = -1;
startupInfo.cb = sizeof(startupInfo);
nStrBuffer = strCommand.GetLength() + 50;
bProcessResult = CreateProcess(nullptr, strCommand.GetBuffer(nStrBuffer),
nullptr, nullptr, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
nullptr, nullptr, &startupInfo, &processInformation);
strCommand.ReleaseBuffer();
if (!bProcessResult)
{
// CreateProcess() failed
// Get the error from the system
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, nullptr);
// Display the error
CString strError = (LPTSTR)lpMsgBuf;
TRACE(_T("Authenticate failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), strCommand, strError);
// Free resources created by the system
LocalFree(lpMsgBuf);
SetProgramExecuting(false);
// We failed.
return false;
}
else
{
// Successfully created the process. Wait for it to finish.
DWORD WaitResult;
do
{
WaitResult = MsgWaitForMultipleObjects(1,
// only 1 wait object
&processInformation.hProcess, // worker thread
FALSE, // stop if any
INFINITE, // no timeout
QS_ALLINPUT);
if (WaitResult == WAIT_OBJECT_0 + 1)
{
// Handle windows message
MSG Msg;
while (PeekMessage(&Msg, nullptr, 0, (UINT)-1, PM_REMOVE))
{
TRACE3("%d %d %d\n", Msg.message, Msg.wParam, Msg.lParam);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
} while (WaitResult != WAIT_OBJECT_0);
ASSERT(WaitResult == WAIT_OBJECT_0);
// Get the exit code.
bExitCodeProcess = GetExitCodeProcess(processInformation.hProcess, &rExitCode);
// Close the handles.
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
if (!bExitCodeProcess)
{
// Could not get exit code.
TRACE(_T("Executed command but couldn't get exit code.\nCommand=%s\n"), strCommand);
SetProgramExecuting(false);
return false;
}
SetProgramExecuting(false);
return true;
}
}
在OnInitDialog
内部,就在调用ExecuteProgram
之前,我尝试使用:
CWaitCursor wait;
但这没什么区别。那么,如何显示从调用弹出对话框到用户可见对话框的等待光标呢?一种解决方案是使用
无模式对话框。您可以创建一个类似于wait cursor
dialog的对话框
在dlgSettings.DoModal()之前显示Modeless对话框
代码中的代码>语句。请在显示无模式对话框时使用TOP\u MOST
最后,处理结束后,从OnInitDialog()
隐藏/关闭无模式对话框
另一种办法可以是:
在COutlookCalendarSettingsDlg
类中添加asCWaitCursor*m_pWaitCursor
的public
成员。现在将代码修改为
void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions()
{
COutlookCalendarSettingsDlg dlgSettings(this);
dlgSettings->m_pWaitCursor = new CWaitCursor();
dlgSettings.DoModal();
}
然后将COutlookCalendarSettingsDlg的OnInitDialog
修改为delete
实例CWaitCursor
,然后再从中返回
delete m_pWaitCursor;
更新
我想我会为这个答案添加一个适用于其他情况的更新。你要做的是用a代替。本文提供了一个小例子:
#include "PersistentWaitCursor.h"
void CMyWnd::DoSomeLengthyOperation()
{
// Create and show the wait cursor
CPersistentWaitCursor waitCursor;
// Do some lengthy operation
...
// waitCursor goes out of scope and cursor is restored
}
BOOL CMyWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (CPersistentWaitCursor::WaitCursorShown())
{
// We are showing the wait cursor
RestoreWaitCursor();
return TRUE;
}
// Let the base class deal with this one
return CWnd::OnSetCursor(pWnd, nHitTest, message);
}
请看这篇文章,了解它的工作原理。但我可以证实,对于我其他一些冗长的操作,这个增强的CPersistantWaitCursor
起到了作用。一个有趣的方法。但是,“一个看起来类似于等待光标对话框的对话框”的机制如何呢?@andrewruckle我编辑了答案,加入了另一种显示WaitCursor
ThisdlgSettings->m_pWaitCursor=new CWaitCursor()的方式
最好在重写的COutlookCalendarSettingsDlg::DoModal()
中完成,这样COutlookCalendarSettingsDlg
的用户就不需要关心这个细节了。@zett42刚刚发现了这个改进!好主意。执行。谢谢