gpt4 book ai didi

mfc - 从调用弹出对话框到用户可以看到对话框,如何显示等待光标?

转载 作者:行者123 更新时间:2023-12-04 21:05:18 27 4
gpt4 key购买 nike

所以,我想显示一个 CDialog给用户:

void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions()
{
COutlookCalendarSettingsDlg dlgSettings(this);

dlgSettings.DoModal();
}

现在,弹出对话框(在 OnInitDialog 中)在幕后运行一个控制台应用程序。此控制台应用程序正在与 通信微软图形 .

因此,对话框可能需要几秒钟才能显示。

我用这个方法执行控制台应用程序:
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;

但这没什么区别。那么如何从我调用弹出对话框的那一刻起显示等待光标,直到用户可以看到该对话框?

最佳答案

一种解决方案是使用 Modeless Dialog .您可以创建一个类似于 wait cursor 的对话框。对话。

你表明Modeless Dialog就在之前 dlgSettings.DoModal();代码中的语句。请使用 TOP_MOST同时显示 Modeless Dialog .

最后,隐藏/关闭 Modeless Dialog来自 OnInitDialog()一旦处理结束。

另一种方法可能是:

添加 public as 成员 CWaitCursor* m_pWaitCursorCOutlookCalendarSettingsDlg类(class)。现在修改代码为

void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions()
{
COutlookCalendarSettingsDlg dlgSettings(this);
dlgSettings->m_pWaitCursor = new CWaitCursor();
dlgSettings.DoModal();
}

然后修改 OnInitDialogCOutlookCalendarSettingsDlgdelete CWaitCursor 的实例在从它回来之前。
delete m_pWaitCursor;

更新

我想我会对这个答案添加一个适用于其他情况的更新。你要做的是使用 CPersistantWaitCursor反而。文章提供了一个小例子:
#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成功了。

关于mfc - 从调用弹出对话框到用户可以看到对话框,如何显示等待光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com