標籤:屬性 bsp alt rect 計算 img viewport 編程 amp
對Windows編程新手來說,GDI編程中最困難的部分就是映射模式(Mapping Mode)。
什麼是映射模式?
我們知道,GDI畫圖是在裝置描述表這個邏輯意義上的顯示平面上進行,其使用的是邏輯座標,簡單地說,映射模式就是裝置描述表的屬性,用於確定從邏輯座標到裝置座標值的轉換方式。
Windows支援8中映射模式:MM_TEXT為預設。
/* Mapping Modes
#define MM_TEXT 1
#define MM_LOMETRIC 2
#define MM_HIMETRIC 3
#define MM_LOENGLISH 4
#define MM_HIENGLISH 5
#define MM_TWIPS 6
#define MM_ISOTROPIC 7
#define MM_ANISOTROPIC 8
*/
CDC* pDC = GetDC();
// 預設映射模式 MM_TEXT
pDC->Ellipse(0, 0, 100, 100);
// 設定映射模式 MM_LOMETRIC y向下為負 0.1mm
pDC->SetMapMode(MM_LOMETRIC);
pDC->Ellipse(0, 0, 100, -100);
// 設定映射模式 MM_HIMETRIC y向下為負 0.01mm
pDC->SetMapMode(MM_HIMETRIC);
pDC->Ellipse(0, 0, 100, -100);
// 設定映射模式 MM_LOENGLISH y向下為負 0.01in 0.254mm 1英寸(in)=25.4毫米(mm)
pDC->SetMapMode(MM_LOENGLISH);
pDC->Ellipse(0, 0, 100, -100);
// 設定映射模式 MM_HIENGLISH y向下為負 0.001in 0.0254mm 1英寸(in)=25.4毫米(mm)
pDC->SetMapMode(MM_HIENGLISH);
pDC->Ellipse(0, 0, 100, -100);
// 設定映射模式 MM_TWIPS y向下為負 0.0007in 1英寸(in)=25.4毫米(mm)
pDC->SetMapMode(MM_TWIPS);
pDC->Ellipse(0, 0, 100, -100);
/* 可程式化映射模式 */
// 根據視窗尺寸按比例自動調節畫圖的輸出大小
CRect rect;
GetClientRect(&rect);
// 各向異性 MM_ANISOTROPIC
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(100, 100);
pDC->SetViewportExt(rect.Width(), rect.Height());
pDC->Ellipse(0, 0, 100, 100);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(100, -100);
pDC->SetViewportExt(rect.Width(), rect.Height());
pDC->Ellipse(0, 0, 100, -100);
//各向同性 MM_ISOTROPIC
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(100, -100);
pDC->SetViewportExt(rect.Width(), rect.Height());
pDC->Ellipse(0, 0, 100, -100);
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(100, -100);
pDC->SetViewportExt(rect.Width(), rect.Height());
pDC->Ellipse(0, 0, 100, -100);
CDC::SetWindowExt 設定“視窗範圍”
CDC::SetViewportExt 設定“視口範圍”
可以這樣認為,視窗的尺寸以邏輯單位計算,視口的尺寸以裝置尺寸或像素點計算。
需要注意的是,在MM_ISOTROPIC模式下,應該首先調用SetWindowExt,否則部分視窗客戶區可能會因落在視窗的邏輯範圍之外而無法使用。
Windows繪圖中的GDI映射模式