The mapping mode governs how windows translates logical coordinates into device coordinates within the current device context. Logical coordinates represent the graphics and text application values and the device coordinates are the resulting pixel positions within a window. The mapping mode also determines the orientation of the X axis and the Y axis and whether the values of x and y increase or decrease in respect to the origin. The default device context sets logical units the same as pixels with the X axis being right positive, the Y axis being positive down, and sets the coordinate origin to the upper left corner of the window. Windows defines eight mapping modes. These are listed below
Mapping Mode | Logical Unit | x-axis and y-axis |
MM_TEXT | Pixel | Positive x is to the right; positive y is down |
MM_LOMETRIC | 0.1 mm | Positive x is to the right;positive y is up. |
MM_HIMETRIC | 0.01 mm | Positive x is to the right;positive y is up. |
MM_LOENGLISH | 0.01 in | Positive x is to the right; positive y is up. |
MM_HIENGLISH | 0.001 in | Positive x is to the right; positive y is up. |
MM_TWIPS | 1/1440 in | Positive x is to the right; positive y is up. |
MM_ISOTROPIC | user specified | user specified |
MM_ANISOTROPIC | user specified | user specified |
To select a different mapping mode, use the CDC member function SetMapMode() –
virtual int SetMapMode( int nMapMode );
where
nMapMode specifies the new mapping mode. The Return Value is the previous mapping mode.
Programmable Mapping Modes
The MM_ISOTROPIC and MM_ANISOTROPIC mapping modes differ from the other mapping modes in that the unit of measures used to transform logical coordinates to device coordinates can be set by the user. The MM_ISOTROPIC and MM_ANISOTROPIC mapping modes differ from each other in that with the former, the range of the x-axis and y-axis must be the same, and with the latter, the range of the x-axis and y-axis can be different. Selecting either of these modes means the developer will need to set the dimensions of the window.
To set the logical extents of the window associated with the device context use the SetWindowExt() member function. To map the corresponding device size, known as the viewpoint onto these logical coordinates use the SetViewportExt() member function.
SetWindowExt
Sets the x- and y-extents of the window associated with the device context.
virtual CSize SetWindowExt( int cx, int cy ); virtual CSize SetWindowExt( SIZE size );
Parameters
cx – Specifies the x-extent (in logical units) of the window.
cy – Specifies the y-extent (in logical units) of the window.
size – Specifies the x- and y-extents (in logical units) of the window.
Returns the previous extents of the window as a CSize object. If an error occurs, the x- and y-coordinates of the returned CSize object are both set to 0.
SetViewportExt
Sets the x- and y-extents of the viewport of the device context.
virtual CSize SetViewportExt( int cx, int cy ); virtual CSize SetViewportExt( SIZE size );
Parameters
cx – Specifies the x-extent of the viewport (in device units).
cy – Specifies the y-extent of the viewport (in device units).
size – Specifies the x- and y-extents of the viewport (in device units).
Returns the previous extent of the viewport as a CSize object. When an error occurs, the x- and y-coordinates of the returned CSize object are both set to 0.
For example SetViewportExt called with parameters 100,50 and SetWindowExt called with parameters 100,100 will mean that each logical unit in the x direction will equate to 1 device unit while each logical unit in the y direction will equate to 1/2 a unit in the device coordinate.
Moving the Origin
By default, a device context’s origin, regardless of the mapping mode. is in the upper left corner of the display. This device context’s origin can however be changed by the CDC member functions SetWindowOrg() and SetViewportOrg() . The former moves the windows origin, and the latter, the viewport origin. The prototype for these functions are
CPoint SetWindowOrg( int x, int y ); CPoint SetWindowOrg( POINT point );
And
virtual CPoint SetViewportOrg( int x, int y ); virtual CPoint SetViewportOrg( POINT point );
where
cx – Specifies the x-extent (in logical units) of the window.
cy – Specifies the y-extent (in logical units) of the window.
size – Specifies the x- and y-extents (in logical units) of the window.
Returns the previous origin or viewport of the window as a CPoint object
The following short program draws 5 squares under different mapping to illustrate the different display characteristics of each
#include <afxwin.h> class CMFCApp1 : public CWinApp { public: BOOL InitInstance(); }; class CMainFrame : public CFrameWnd { public: CMainFrame(); afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() }; BOOL CMFCApp1::InitInstance() { m_pMainWnd = new CMainFrame(); m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; } CMainFrame::CMainFrame() { Create(NULL, "MFC Mapping mode demo"); CClientDC dc(this); } BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_PAINT() END_MESSAGE_MAP() afx_msg void CMainFrame::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect (&rect); //draw rectangle with default pen dc.Rectangle (100, 100, 300, 300); //draw rectangle with red pen using mapping mode MM_HIMETRIC CPen linecolour; linecolour.CreatePen(PS_SOLID,1,RGB(255, 0, 0)); dc.SelectObject(linecolour); dc.SetMapMode (MM_HIMETRIC); dc.Rectangle (100, -100, 300, -300); DeleteObject(linecolour); //draw rectangle with blue pen using mapping mode MM_LOMETRIC CPen linecolour1; linecolour1.CreatePen(PS_SOLID,1,RGB(0, 0, 255)); dc.SelectObject(linecolour1); dc.SetMapMode (MM_LOMETRIC); dc.Rectangle (100, -100, 300, -300); DeleteObject(linecolour1); //draw rectangle with purple pen using mapping mode MM_LOENGLISH dc.SetMapMode (MM_LOENGLISH); CPen linecolour2; linecolour2.CreatePen(PS_SOLID,1,RGB(255, 0, 255)); dc.SelectObject(linecolour2); dc.Rectangle (100, -100, 300, -300); DeleteObject(linecolour2); //draw rectangle with blue pen using and PS_DASH pen style using mapping mode MM_ANISOTROPIC dc.SetMapMode (MM_ANISOTROPIC); dc.SetViewportExt (1, 1); dc.SetWindowExt (1,10); CPen linecolour3; linecolour3.CreatePen(PS_DASH,1,RGB(0, 0, 255)); dc.SelectObject(linecolour3); dc.Rectangle (100, 300, 300, 500); DeleteObject(linecolour3); } CMFCApp1 MFCApp1;