Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mfc 如何显示从调用弹出对话框到用户可见对话框的等待光标?_Mfc_Modal Dialog - Fatal编程技术网

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
类中添加as
CWaitCursor*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
This
dlgSettings->m_pWaitCursor=new CWaitCursor()的方式
最好在重写的
COutlookCalendarSettingsDlg::DoModal()
中完成,这样
COutlookCalendarSettingsDlg
的用户就不需要关心这个细节了。@zett42刚刚发现了这个改进!好主意。执行。谢谢