Weirdness with SetWindowExt and SetWindowExtEx (negative height, upside down)

心已入冬 提交于 2019-12-11 08:03:05

问题


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 the SetViewportExt() call with a SetViewportExtEx() call then the metafile is correct. The difference seems to be that SetViewportExtEx() 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 calling SetViewportExtEx(), which is probably the reason for this.
  • In normal view rendering (MFC view) both SetViewportExt() and SetViewportExtEx() result in a positive viewport height.

So, does anybody have answers to these two questions?

  1. Why the heck does SetViewportExtEx() set my viewport height as negative value in metafile and print preview rendering, although I pass a positive one?
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!