Thread: MFC: need to spawn a dialog with context sensitive attributes. how?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    72

    MFC: need to spawn a dialog with context sensitive attributes. how?

    I have a dialog based MFC app. I have a button that spawns a new dialog with an image in it. I want to have a second buttong that pops up a dialog with a different image. Both images require a bit of computing to make, but a lot of the code is the same. I'd like to be able to reuse the dialog and its class for both instances. Is this possible? In other words, can I tell the newly spawned dialog who called it?

    My thought was to create a dialog and have it flagged so it didn't do anything, then call a function that changes the flag & calls OnPaint, which would then allow either image to be built. Something like this:

    Code:
    void CFrameDlg::OnBnClickedPreview()
    {
      CPreviewDlg Dlg;
      Dlg.buildPreview();
      Dlg.DoModal();
    }
    ... then CPreviewDlg's OnPaint would look like this:
    Code:
    void CPreviewDlg::OnPaint()
    {
      if (preview)
        // do preview stuff
      if (input)
        // do input stuff
      else
        CPaintDC dc(this); 
    }
    In this example, buildPreview() sets a flag in the CPreviewDlg class so that when OnPaint is called, it will assemble the correct image, however, I can't seem to get buildPreview() to call OnPaint. I've tried RedrawWindow, UpdateWindow, and CDialog::OnPaint, but none of them work. Am I making the call wrong, or is this just wrong all together?

    FYI: my attempts at CPreview::buildPreview() have looked like this:
    Code:
    method 1:
      preview = true;
      CDialog::OnPaint();
    
    method 2:
      peview = true;
      CRect framerect;
      this->GetWindowRect(framerect);
      RedrawWindow(framerect, NULL, RDW_INTERNALPAINT);
    
    method 3:
      preview = true;
      Invalidate();
      UpdateWindow();

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Method 3 is the more typical way to cause the entire window to be redrawn.

    You're on the right track as far as making your CDialog class do something different under different circumstances.

    OnPaint() is called in response to a WM_PAINT message.
    What is "do preview stuff" and "do input stuff"?
    And what makes you think that OnPaint() isn't even being called?

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by Codeplug
    What is "do preview stuff" and "do input stuff"?
    And what makes you think that OnPaint() isn't even being called?
    as a simple test, I loaded a bmp if preview==true, otherwise I just did a CPaintDC dc(this); I never got the bmp drawn. As a matter of fact, I would get this error when I tried to click the button that drew the bmp:
    Debug Assertion Failed!

    Program: .....\Frame.exe
    File: f:\vs70builds\...\afxwin2.inl
    Line: 135
    which corresponds to this line:
    Code:
    _AFXWIN_INLINE void CWnd::Invalidate(BOOL bErase)
    	{ ASSERT(::IsWindow(m_hWnd)); ::InvalidateRect(m_hWnd, NULL, bErase); }
    (the second line)

    If I break the app there, it shows that the "UpdateWindow()" func was the call that bombed. SO I figure I'm doing something wrong.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    That's what I don't understand - since OnPaint() is called in response to a WM_PAINT message, you should always use CPaintDC (assuming you're not using RedrawWindow() with RDW_INTERNALPAINT - which you shouldn't be).

    If you don't use RedrawWindow(), an easy rule-of-thumb to remember is that OnPaint() is called when it's time to render to the screen using a CPaintDC.
    CPaintDC is simply a CDC derived class that calls BeginPaint() in the constructor and EndPaint() in the destructor.

    Do all your rendering with a CPaintDC. Render whatever you need to render for your different circumstances.

    gg

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by Codeplug
    Do all your rendering with a CPaintDC. Render whatever you need to render for your different circumstances.
    So then do I make an explicit call to CPaintDC dc(this) in my buildPreview()? If I try to do that, or Invalidate/UpdateWindow, I get the same debug assertion failed message.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    All rendering is done to a Device Context or "DC".

    If buildPreview() renders to a DC, then you should be giving it the DC to render to.

    Since the OnPaint() method should always render to a CPaintDC, you'll want to pass your CPaintDC to buildPreview() to render to.

    What does your buildPreview() currently do?

    gg

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by Codeplug
    What does your buildPreview() currently do?
    Well, that's just it, it doesn't do anything because everything I've tried doesn't work & brings up those errors I described earlier. All I really want it to do is tell the program which type of image to build and then force the window to repaint.

    Currently, I have it set a bool to true and then I want it to repaint the window. In Onpaint, I check the bool, and call the appropriate image builder. I could post code, but all it really says is "go = true;" and then several failed attempts at forcing a repaint.

    There must be something to forcing a repaint that I'm missing.

    hmmm....

    PS - sorry for replying so late. Other things came up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Document Class and Dialog Windows :: MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 12-01-2002, 12:27 AM
  2. Context Menu & Dialog Box :: MFC
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 08-11-2002, 10:01 AM
  3. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM