The code is present in OnDraw() function.
Printable View
The code is present in OnDraw() function.
Quote:
Original code,
CPreviewView* pPreviewView = DYNAMIC_DOWNCAST(CPreviewView, pMainFrame->GetDlgItem(AFX_IDW_PANE_FIRST));
CWnd* pToolBar = pMainFrame->GetDlgItem(AFX_IDW_PREVIEW_BAR);
Here during debugging,found that,
pPreviewView=0x00000000
pToolBar=0x00000000
I found something,
The code,
CPreviewView* pPreviewView = DYNAMIC_DOWNCAST(CPreviewView, pMainFrame->GetDlgItem(AFX_IDW_PANE_FIRST));
when corrected to,
CPreviewView* pPreviewView= (CPreviewView *)(pMainFrame->GetDlgItem(AFX_IDW_PANE_FIRST));
gives results
Well, the reason pPreviewView is NULL is you are trying to cast some CWnd* pointer to the CPreviewView* type while the object of this CWnd* is NOT a kind of CPreviewView class! Just read about DYNAMIC_DOWNCAST in MSDN:Now, please explainQuote:
The DYNAMIC_DOWNCAST macro provides a handy way to cast a pointer to a pointer to a class object while checking to see if the cast is legal. The macro will cast the pointer parameter to a pointer to an object of the class parameter's type.
If the object referenced by the pointer is a "kind of" the identified class, the macro returns the appropriate pointer. If it isn't a legal cast the macro returns NULL.
- Why do you call
What are your expectations of the value retuned by this expression?Code:pMainFrame->GetDlgItem(AFX_IDW_PANE_FIRST)
- Why do you cast this CWnd* type to the CPreviewView*? Note that there is no any class in your project derived from CPreviewView!
1.The Project what I am working is a menu based display software,wherein each menu or submenu or each tab is considered as a pane will be displayed as a page in the print preview.The return value of
pMainFrame->GetDlgItem(AFX_IDW_PANE_FIRST)
is a handle to the pane(It's a CWnd* object)
2.The above thing explains
Yes, but only because you have faked the compiler!
It is just the same as in this example:and what you get is:Code:CWnd wnd;
CPreviewView* pPreviewView = (CPreviewView *)&wnd;
pPreviewView->SetPrintView(this);
So if you want to cast in a c-style then you have to check usingQuote:
Unhandled exception at 0x78ad6805 (mfc90ud.dll) in test.exe
Code:ASSERT_KINDOF(CPreviewView, pPreviewView);
I have got a mail,I found in the google,
Does this mean,I have to implement the Print Preview part from the scratch in Visual Studio 2008.HTML Code:Hi Greg,
The print preview window in VC6 is not a child window of the MDI mainframe*
window, when being called it will be displayed to replace the mainframe*
window, and the mainframe window would be hidden. But in MFC7, the print*
preview window is a child window of the MDI mainframe window, so it will be*
contained mainframe window and seems to be somehow "broken".
However, I don't think it is a bug to MFC7, it just a different*
implementation from the MFC6's implementation, and under MFC7's scenario I*
suggest you can maximize the print preview window while it is been called...
Thanks for your understanding!
Best regards,
Gary Chang
Microsoft Online Partner Support
Do you understand what a C-style cast does? It does not convert from one type to another.
I just turned an Elephant into a Car by casting the pointer, right? Wrong.Code:class Car
{
public:
void StartEngine() {}
};
class Elephant
{
};
int main()
{
Elephant e;
Car* pCar = (Car*)&e;
pCar->StartEngine();
}
That is exactly what you are doing when you blindly cast pointers and assume they magically turn the object into whatever you want it to be. If the object T is not a U, casting a pointer to T to a pointer to U doesn't make a T into a U.
Regards,
Paul McKenzie