How to access CDialog from an MDI view [modified]
-
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() containsCDC *pDC = m_StaticImage.GetDC();
but it also doesn't work.. I really bad at these pointer things :(... Thank you in advance..-Houari
-
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() containsCDC *pDC = m_StaticImage.GetDC();
but it also doesn't work.. I really bad at these pointer things :(... Thank you in advance..-Houari
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 aCMyDialog
(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 itsm_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 itsGetParent()
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
-
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 aCMyDialog
(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 itsm_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 itsGetParent()
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
-
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