问题
I am running into a weird behavior regarding SetViewportExt() and SetViewportExtEx().
My MFC application uses MM_ISOTROPIC mapping for setting up the view device context and configures the device context as follows:
m_dc.SetMapMode( MM_ISOTROPIC );
// Set the window extent (document space)
CSize docSizeLP = GetDocumentSizeLP();
m_dc.SetWindowExt(docSizeLP.cx, docSizeLP.cy);
// Next set the viewport extent
CSize docSizeDP = GetDocumentSizeDP();
m_dc.SetViewportExt((int) (docSizeDP.cx * fZoom), (int) (docSizeDP.cy * fZoom));
Now I am encountering three weird things:
- When rendering my view to a meta file (
CMetaFileDC) then my view content is upside down in the metafile. However, if I replace theSetViewportExt()call with aSetViewportExtEx()call then the metafile is correct. The difference seems to be thatSetViewportExtEx()sets a negative viewport height, although my passed value is definitely positive - and that I need the negative viewport height to get the metafile correct. - On the other hand, using the
SetViewportExtEx()as default results in the print preview to not show anything. Again the viewport height turns negative when callingSetViewportExtEx(), which is probably the reason for this. - In normal view rendering (MFC view) both
SetViewportExt()andSetViewportExtEx()result in a positive viewport height.
So, does anybody have answers to these two questions?
- Why the heck does
SetViewportExtEx()set my viewport height as negative value in metafile and print preview rendering, although I pass a positive one? - Why does my metafile rendering require a negative viewport height in order to be not upside down in the end?
I am curious whether anybody has answers to this, since my wisdom ended here. :-)
回答1:
Your question gave me a hint to solving a problem I had with enhanced metafile. My application was also outputting using MM_ISOTROPIC mode and with the logical (0,0) in the center of the view. The output image was offset and scaled incorrectly.
After spending quite a bit of time with it, I finally realized that the problem might be with the 2 versions of device contexts that MFC's CDC has. First DC m_hDC is for the actual output and the second one m_hAttribDC is for querying device metrics like DPI.
What I finally ended up doing was to prepare device context in the following way:
if (pDC->IsPrinting()){
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetViewportOrg(x0, y0);
pDC->SetWindowExt(wind_extent, wind_extent);
pDC->SetViewportExt(viewport_extent, -viewport_extent);
}
else{
::SetMapMode(pDC->m_hDC, MM_ISOTROPIC);
::SetViewportOrgEx(pDC->m_hDC, x0, y0, NULL);
::SetWindowExtEx(pDC->m_hDC, wind_extent, wind_extent, NULL);
::SetViewportExtEx(pDC->m_hDC, viewport_extent, -viewport_extent, NULL);
}
The print preview and metafile output worked fine afterwards.
Hope this helps.
来源:https://stackoverflow.com/questions/9739124/weirdness-with-setwindowext-and-setwindowextex-negative-height-upside-down