對SetViewportOrg和SetWindowOrg的理解:
void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
{
CRect rect;
GetClientRect(&rect);
CClientDC dc(this);
dc.SetViewportOrg(0,rect.Height());//把原點(diǎn)移至視口的左下角
dc.Rectangle(0,0,200,-200);
dc.SetViewportOrg(rect.Width(),rect.Height());//把原點(diǎn)移至視口的右下角
dc.Rectangle(0,0,-200,-200);
dc.SetViewportOrg(rect.Width(),0);//把原點(diǎn)移至視口的右上角
dc.Rectangle(0,0,-200,200);
dc.SetViewportOrg(0,0);//移回原來的位置
dc.Rectangle(0,0,200,200);
}
由于SetViewportOrg的參數(shù)是設(shè)備坐標(biāo),與邏輯坐標(biāo)無關(guān),所以當(dāng)它移動坐標(biāo)軸時與上一次的坐標(biāo)軸的位置無
關(guān)的,并且GetClientDC獲得的是設(shè)備坐標(biāo),更加可以相信它每一次設(shè)置的坐標(biāo)的正確性,它的主要作用是:將原點(diǎn)
(左上角)移至參數(shù)指定的點(diǎn).
至于SetWindowOrg是采用邏輯坐標(biāo)進(jìn)行設(shè)置坐標(biāo)原點(diǎn)的,它可以在對應(yīng)的邏輯映射模式下進(jìn)行設(shè)置原點(diǎn),除了
默認(rèn)的坐標(biāo)系是原點(diǎn)在左上角,向為正,向右為正外,其它非自定義模式都是原點(diǎn)在左上角,向下為負(fù),向右為正的,
和我們數(shù)學(xué)的二維坐標(biāo)系一樣,為了更好的體現(xiàn)出SetWindowOrg我將窗口分別變成我們數(shù)學(xué)上四個像限!它的
作用是這樣的:有一點(diǎn)point1,先將它從設(shè)備坐標(biāo)變成該映射模式下的邏輯坐標(biāo)(注意:一般設(shè)備坐標(biāo)點(diǎn)是+,+ 邏輯
坐標(biāo)轉(zhuǎn)換后是+,-的,由映射模式?jīng)Q定),然后它移動它的奇對稱點(diǎn)使得坐標(biāo)系跟著移動,當(dāng)它的奇對稱點(diǎn)到達(dá)原點(diǎn)
時,原來的原點(diǎn)就到達(dá)point1的位置!(SetWindowOrg的參數(shù)就是point1的奇對稱點(diǎn))
CRect rect;
GetClientRect(&rect);
沒設(shè)置前已經(jīng)是第四象限了!
第一象限:
CClientDC dc(this);
dc.SetMapMode(MM_LOENGLISH);
CPoint point1(0,rect.Height());//把原點(diǎn)移到左下角
dc.DPtoLP(&point1);//先將設(shè)備坐標(biāo)變成邏輯坐標(biāo)!SetWindowOrg要求的!
dc.SetWindowOrg(-point1.x,-point1.y);//兩個負(fù)號取奇對稱點(diǎn)!移對稱點(diǎn)使得整個坐標(biāo)跟著移使對稱點(diǎn)移到原點(diǎn)
CRect rect1(0,0,200,200);
dc.Rectangle(&rect1);
第二象限:
CClientDC dc(this);
dc.SetMapMode(MM_LOENGLISH);
CPoint point1(rect.Width(),rect.Height());//把原點(diǎn)移到右下角
dc.DPtoLP(&point1);//邏輯坐標(biāo)是相對于MM_LOENGLISH進(jìn)行轉(zhuǎn)換的,得到的坐標(biāo)肯定是(+,-)
dc.SetWindowOrg(-point1.x,-point1.y);
dc.Rectangle(0,0,-200,200);
第三象限:
CClientDC dc(this);
dc.SetMapMode(MM_LOENGLISH);
CPoint point1(rect.Width(),0);//把原點(diǎn)移到右上角
dc.DPtoLP(&point1);
dc.SetWindowOrg(-point1.x,-point1.y);
dc.Rectangle(0,0,-200,-200);
上面的SetWindowOrg分開畫的,我要的效果是要像上面的SetViewportOrg這樣連著畫:
其實在移完一個原點(diǎn)后,把原點(diǎn)移回(0,0)再移過就可以做到了:(第四象限忽略)
CRect rect;
GetClientRect(&rect);
int width=rect.Width();
int height=rect.Height();
CClientDC dc(this);
dc.SetMapMode(MM_LOENGLISH);
CPoint point1(0,height);
dc.DPtoLP(&point1);
dc.SetWindowOrg(-point1.x,-point1.y);
CRect rect1(0,0,200,200);
dc.Rectangle(&rect1);
dc.SetWindowOrg(0,0);
point1=CPoint(width,height);
dc.DPtoLP(&point1);
dc.SetWindowOrg(-point1.x,-point1.y);
dc.Rectangle(0,0,-200,200);
dc.SetWindowOrg(0,0);
point1=CPoint(width,0);
dc.DPtoLP(&point1);
dc.SetWindowOrg(-point1.x,-point1.y);
dc.Rectangle(0,0,-200,-200);
如果我中間沒有用dc.SetWindowOrg(0,0);就要思考了!
CClientDC dc(this);
dc.SetMapMode(MM_LOENGLISH);
CPoint point1(0,height);
dc.DPtoLP(&point1);
dc.SetWindowOrg(-point1.x,-point1.y);
CRect rect1(0,0,200,200);
dc.Rectangle(&rect1);
//先明確上一步我們的坐標(biāo)原點(diǎn)在左下角,要把原點(diǎn)移到右下角,此時右下角相對當(dāng)時的坐標(biāo)系是(width,0)
point1=CPoint(width,0);//移至(width,0)
dc.DPtoLP(&point1);//變換成為邏輯坐標(biāo),但符號變?yōu)?+,-),因為在MM_LOENGLISH映射模式下
dc.SetWindowOrg(-point1.x,point1.y);//(width,0)為(+,0)則它的對稱點(diǎn)應(yīng)為(-,0)才對,
//只要將point1前一個變號即可,第二個為0不用變!
dc.Rectangle(0,0,-200,200);

//先明確上一步我們的坐標(biāo)原點(diǎn)在右下角,要把原點(diǎn)移到右上角,此時右上角相對當(dāng)時的坐標(biāo)系是(0,height)
point1=CPoint(0,height);//移至(0,height)
dc.DPtoLP(&point1);//變換成為邏輯坐標(biāo),但符號變?yōu)?+,-),因為在MM_LOENGLISH映射模式下
dc.SetWindowOrg(point1.x,point1.y);//(0,height)為(0,+)則它的對稱點(diǎn)應(yīng)為(0,-)才對,和邏輯坐標(biāo)同號,不用變!
dc.Rectangle(0,0,-200,-200);

可以看出SetWindowOrg每一次執(zhí)行都改變一次坐標(biāo)系的位置!!