Re: OnCancel not being called

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Jul 12, 3:39 pm, "David Ching" <d...@xxxxxxxxxxxxxxxxxxxxxx> 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;
}

.



Relevant Pages

  • Re: Model Dialog box causes another application to get focus
    ... when you cancel one of the in our application it will activate and ... bring the focus to another open window anyone ... However, sometimes it's as simple as just making sure that the parent of the dialog box is set correctly, by passing the reference to the form that _should_ be the parent of the dialog box in the constructor when creating the dialog box. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Model Dialog box causes another application to get focus
    ... when you cancel one of the in our application it will activate and ... bring the focus to another open window anyone ... _should_ be the parent of the dialog box in the constructor when creating ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need help solving a threading issue
    ... (represented by a SendMessage to the window) ... create HWND's on a secondary thread. ... thread, but the issues really *are* different if you create child windows, dialogs, etc. ... I agree a child HWND whose parent was created in another thread is an issue, ...
    (microsoft.public.vc.mfc)
  • Re: Position a modeless dialog over a modal dialog?
    ... When F is instantiated by its parent dialog using DoModal, ... Because A is a modeless dialog, I would add handlers to override the default OK and Cancel ... Why would you write a GetDlgItem call for any control? ... window, you must give its coordinates in parent client coordinates. ...
    (microsoft.public.vc.mfc)
  • Re: MFC and c++ problems
    ... the interface is defined by the control. ... Note that the messages of CButton are defined by the CButton ... I would think the parent would define which messages the child could send ... window didn't define the message itself, and you didn't design the classes ...
    (microsoft.public.vc.mfc)