Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to access CDialog from an MDI view [modified]

How to access CDialog from an MDI view [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houari_id
    wrote on last edited by
    #1

    Dear all.. How can I use GetDC() in a dialog from an MDI CView? I mean, I have image data in CMyMDIView, and I want to show it in a dialog (CMyDialog). I've tried this: CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); CDC *pDC = pMyDialog->m_StaticImage.GetDC(); but the second line doesn't work... I also tried to call the function in CMyDialog that calls CDC from CMyMDIView, like this: void MyMDIView::OnDraw(CDC* pDC) { CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); pMyDialog->DoLoadImage(); ... } where DoLoadImage() contains CDC *pDC = m_StaticImage.GetDC(); but it also doesn't work.. I really bad at these pointer things :(... Thank you in advance..

    -Houari

    R 1 Reply Last reply
    0
    • H houari_id

      Dear all.. How can I use GetDC() in a dialog from an MDI CView? I mean, I have image data in CMyMDIView, and I want to show it in a dialog (CMyDialog). I've tried this: CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); CDC *pDC = pMyDialog->m_StaticImage.GetDC(); but the second line doesn't work... I also tried to call the function in CMyDialog that calls CDC from CMyMDIView, like this: void MyMDIView::OnDraw(CDC* pDC) { CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); pMyDialog->DoLoadImage(); ... } where DoLoadImage() contains CDC *pDC = m_StaticImage.GetDC(); but it also doesn't work.. I really bad at these pointer things :(... Thank you in advance..

      -Houari

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      There are (at least) three things worth pointing out here.

      • CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); This won't work because (I assume) your application's window is a mainframe window, not a dialog. Forcing the cast just tells the compiler to assume your main window is indeed a CMyDialog (which is why it compiles), but any attempt to treat the cast object as such will only cause you a lot of pain (because you've made an incorrect assumption about the structure of the dereferenced object).
      • CDC *pDC = pMyDialog->m_StaticImage.GetDC(); pMyDialog is really a pointer to your application's mainframe window, so trying to reference its m_StaticImage member's DC is going to hurt. A lot.
      • You're better off defining a method in your view class that exposes pertinent data required by any class who wishes to display the image. The displaying class (i.e. an instance of CMyDialog) can be passed a pointer to the view class - or can be so bold as to cast its GetParent() to your view class (assuming the dialog is and will always be only displayed by that view class) - in order to get at the image data, which it can then display in a suitable control.

      /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      H 1 Reply Last reply
      0
      • R Ravi Bhavnani

        There are (at least) three things worth pointing out here.

        • CMyDialog *pMyDialog = (CMyDialog*) AfxGetMainWnd(); This won't work because (I assume) your application's window is a mainframe window, not a dialog. Forcing the cast just tells the compiler to assume your main window is indeed a CMyDialog (which is why it compiles), but any attempt to treat the cast object as such will only cause you a lot of pain (because you've made an incorrect assumption about the structure of the dereferenced object).
        • CDC *pDC = pMyDialog->m_StaticImage.GetDC(); pMyDialog is really a pointer to your application's mainframe window, so trying to reference its m_StaticImage member's DC is going to hurt. A lot.
        • You're better off defining a method in your view class that exposes pertinent data required by any class who wishes to display the image. The displaying class (i.e. an instance of CMyDialog) can be passed a pointer to the view class - or can be so bold as to cast its GetParent() to your view class (assuming the dialog is and will always be only displayed by that view class) - in order to get at the image data, which it can then display in a suitable control.

        /ravi

        This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        H Offline
        H Offline
        houari_id
        wrote on last edited by
        #3

        thank you mr ravi... I just get your point about referencing from view class. so instead, I just use OnPaint() function in the CMyDialog to call the function to display the image, and it works fine.. thanks a lot... :)

        -Houari

        R 1 Reply Last reply
        0
        • H houari_id

          thank you mr ravi... I just get your point about referencing from view class. so instead, I just use OnPaint() function in the CMyDialog to call the function to display the image, and it works fine.. thanks a lot... :)

          -Houari

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          Excellent - good work! /ravi

          This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups