gpt4 book ai didi

c++ - 控件不在无模式对话框 MFC 中呈现

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:30:54 25 4
gpt4 key购买 nike

更新:可能的解决方案。在 header 中声明 CWait 对话框似乎可以解决此问题。

更新 2: 消息泵可能是罪魁祸首。明确的“泵送”似乎可以解决问题。

当某些功能在应用程序中执行时,我试图显示模态“请稍候”对话框。我要显示的对话框是这样的: enter image description here

我使用这段代码来调用对话框。

CWait dialog;
dialog.Create(IDD_WAIT);
dialog.SetWindowTextW(L"Geocoding");
dialog.ShowWindow(SW_SHOW);

mastImageGeocode(); //Working here
slvImageGeocode();
interfImageGeocode();
cohImageGeocode();

dialog.CloseWindow();

最终显示的是这样的: enter image description here

我似乎无法弄清楚为什么控件不呈现。

我尝试使用这种方法在对话框初始化后手动处理消息循环:

MSG stMsg;

while (::PeekMessage (&stMsg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage (&stMsg);
::DispatchMessage (&stMsg);
}

没有真正起作用。

我也试过指针方式

Cwait * dialog; //THis is in header
dialog = new CWait(this);
dialog->Create(IDD_WAIT);
dialog->SetWindowTextW(L"Geocoding");
dialog->ShowWindow(SW_SHOW);

mastImageGeocode(); //Some work here
slvImageGeocode();
interfImageGeocode();
cohImageGeocode();

dialog->CloseWindow();
delete dialog;

我是不是做错了什么。

感谢您的帮助。

更新:如果我在各个函数中调用它,它工作正常!

最佳答案

听起来您没有从主处理循环中更新对话框。我在下面放置了我的 MFC 进度对话框的简化版本。请注意,需要定期调用 SetProgress 来更新屏幕。作为更一般的一点,如果您在 MFC 中使用无模式对话框,则需要调用 OnUpdate(FALSE) 来刷新它们,并确保它们有足够的时间进行刷新。很多时候,当我认为我需要一个无模式对话框时,实际上通过将任务分成单独的线程来更好地服务我,即将处理部分放在它自己的工作线程中。 YMMV.

class CProgressDialog : public CDialog
{
public:
CProgressDialog(LPCTSTR Name,int Max,CWnd* pParent = NULL);
CProgressDialog(UINT NameID,int Max,CWnd* pParent = NULL);
virtual ~CProgressDialog();

int m_Max;
void SetProgress(int Progress);
void SetRange(int Range);
enum { IDD = IDD_PROGRESS_DIALOG };
CProgressCtrl m_Progress;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};


CProgressDialog::CProgressDialog(LPCTSTR Name,int Max,CWnd* pParent /*=NULL*/)
: CDialog(CProgressDialog::IDD, pParent)
{
Create(CProgressDialog::IDD, pParent);
SetWindowPos(&wndTop,1,1,0,0,SWP_NOSIZE | SWP_SHOWWINDOW);
SetWindowText(Name);
m_Max = Max;
}


CProgressDialog::~CProgressDialog()
{
DestroyWindow();
}

void CProgressDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
}


BEGIN_MESSAGE_MAP(CProgressDialog, CDialog)
END_MESSAGE_MAP()

BOOL CProgressDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_Progress.SetRange32(0,m_Max);
return TRUE;
}

void CProgressDialog::SetProgress(int Progress)
{
m_Progress.SetRange32(0,m_Max);
m_Progress.SetPos(Progress);
UpdateData(FALSE);
}

void CProgressDialog::SetRange(int Range)
{
m_Max = Range;
}

关于c++ - 控件不在无模式对话框 MFC 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12322018/

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