[英]Controls not rendering in modeless dialog box MFC
更新 :可能的解決方案。 在標題中聲明CWait對話框似乎可以解決此問題。
UPDATE2:消息泵可能是罪魁禍首。 明確的“抽水”似乎可以解決問題。
我正在嘗試在應用程序中執行某些功能時顯示模式“請稍候”對話框。 我要顯示的對話框是這樣的:
我正在使用此代碼來調用對話框。
CWait dialog;
dialog.Create(IDD_WAIT);
dialog.SetWindowTextW(L"Geocoding");
dialog.ShowWindow(SW_SHOW);
mastImageGeocode(); //Working here
slvImageGeocode();
interfImageGeocode();
cohImageGeocode();
dialog.CloseWindow();
最終顯示的內容是:
我似乎無法弄清楚控件為什么不呈現。
我嘗試使用以下方法初始化對話框后手動處理消息循環:
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)刷新它們,並確保它們有足夠的時間這樣做。 通常,當我認為我需要一個無模式的對話框時,實際上可以通過將任務拆分為多個單獨的線程來更好地服務,即將處理部分放置在其自己的工作線程中。 因人而異。
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;
}
您不僅需要在對話框的開頭,而且還需要在更新之后手動泵送消息。 像這樣:
void CProgressDialog::SetProgress(int Progress)
{
m_progressBar.SetPos(Progress);
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
采用:
m_progressBar.SetPos(進步+ 1);
m_progressBar.SetPos(進步);
它會顯示出來。 不要問我為什么... PS:到達最后一個Progressstep時要注意!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.