Re: OnCancel not being called
On Jul 12, 3:39 pm, "David Ching" <d...@remove-this.dcsoft.com> wrote:
You are right, but maybe something is wrong with your PeekMessage loop. Are
you calling CProgressWnd::PeekAndPump()?
No. Because of my understanding (which I described in the previous
posting) I decided that a PeekMessage loop was NOT needed, so I don't
call ::PeekMessage() nor CProgressWnd::PeekAndPump().
And provided I send FEWER than approx 5 progress messages per sec from
the filter graph to the handler in my main CMyDialog object then
CProgressWnd::OnCancel() gets called and it *seems* to works fine -
but it doesn't fill me with confidence!
To simplify, first start a 0.5 second timer. Then call CProgressWnd::Create
to put up the dialog modelessly. Then start your background thread that
starts the filter graph. When your primary thread receives messages from
the thread, call the CProgressWnd to update the progress. When your
OnTimer() is called every 0.5 seconds, call CProgressWnd::Cancelled() to see
if the Cancel button has been clicked.
Thanks for your suggestion. I just tried using only Create and I can
allow LOTS of progress messages per sec to be sent from filter graph
to the CMyDialog handler, and pressing the Cancel button results in
OnCancel being executed IMMEDIATELY every time!
That's great, except that I NEED the CProgressWnd to be modal - I
don't want anyone messing with the main window while the "progressed"
operation is taking place.
Any idea how I can get this to work properly as modal ?
All GoModal seems to do is call EnableWindow(FALSE) on the parent
window, then EnableWindow(TRUE) on itself. But this seems to screw up
the message handling in the way I've previously described.
There is a fragment of code in the CProgressWnd::PeekAndPump() which
says:
// Cancel button disabled if modal, so we fake it.
if (m_bModal && (msg.message == WM_LBUTTONUP))
{
CRect rect;
m_CancelButton.GetWindowRect(rect);
if (rect.PtInRect(msg.pt))
OnCancel();
}
This suggests that if you use GoModal() then the normal invocation of
the OnCancel() does not work, so he's written a workaround to see if
the left mouse button was clicked within the Cancel button's rect.
However, I HAVE been using GoModal (with NO call to PeekAndPump) and
the OnCancel handler IS being invoked when I click the Cancel button -
so the above fragment of code seems to be wrong. i.e. the cancel
button does NOT get disabled if you are using CProgressWnd::GoModal.
But it does seem to screw up the message handling.
Any idea how I can get to work properly as a MODAL window?
Here's the complete code of CProgressWnd::GoModal if that helps anyone
spot what the problem is with it:
BOOL CProgressWnd::GoModal(LPCTSTR pszTitle /*=_T("Progress")"*/, BOOL
bSmooth /*=FALSE*/)
{
CWnd *pMainWnd = AfxGetMainWnd();
if (!::IsWindow(m_hWnd) && !Create(pMainWnd, pszTitle, bSmooth))
return FALSE;
// Walk up the window chain to find the main parent wnd and
disable it.
CWnd * wnd = this;
do {
CWnd * parent = wnd->GetParent();
// if we have no parent (ie. the main window)
// or if our parent is disabled,
// then this is the window that we will want to remember for
reenabling
if (!parent || !parent->IsWindowEnabled()) {
m_wRenenableWnd = wnd;
m_wRenenableWnd->EnableWindow(false);
break;
}
wnd = parent;
} while (1);
// Re-enable this window
EnableWindow(TRUE);
m_bModal = TRUE;
return TRUE;
}