Chapter 5: Windows Message Mapping
CRect, CPoint, and CSize Arithmetic
The CRect, CPoint, and CSize classes are derived from the Windows RECT, POINT, and SIZE structures, and thus they inherit public integer data members, as follows:
CRect
|
left, top, right, bottom
|
CPoint
|
x, y
|
CSize
|
cx, cy
|
If you look in the MFC Library Reference, you'll see that these three classes have a number of overloaded operators. You can, among other things, do the following:
- Add a CSize object to a CPoint object
- Subtract a CSize object from a CPoint object
- Subtract one CPoint object from another, yielding a CSize object
- Add a CPoint or CSize object to a CRect object
- Subtract a CPoint or CSize object from a CRect object
The CRect class has member functions that relate to the CSize and CPoint classes. For example, the TopLeft member function returns a CPoint object, and the Size member function returns a CSize object. From this, you can begin to see that a CSize object is the "difference between two CPoint objects" and that you can "bias" a CRect object by a CPoint object.
GetClientRect
The following view member function code retrieves the client rectangle coordinates and stores them in rectClient:
CRect rectClient;
GetClientRect(rectClient);
Determining Whether a Point Is Inside an Ellipse
CRgn rgn;
rgn.CreateEllipticRgnIndirect(&m_rectEllipse);
if(rgn.PtInRegion(point))
{
m_nColor = GRAY_BRUSH;
}
else
{
m_nColor = BLACK_BRUSH;
}
Using Windows Mapping Modes
setWindowOrg 改变坐标原点的位置,例如可以setWindowOrg(100,100) 那么窗口左上点就是100,100
按习惯,(0,0)就原点,原点就是(0,0),但是如果用此来理解windows的map mode,就会走弯路。其实,稍微改变一下观念,windows的map mode就比较好理解了。举例说明:
page space---->device space
pDC->SetMapMode(MM_LOMETRIC);
pDC->SetWindowOrg(40,0); //这句“设定”page space的原点为(40,0),注意,
//这时(40,0)就是原点,原点就是(40,0)这个点,其实,(0,0)与原点没有必然联系。这
//一句对下面的画图函数在page space中所作的图不会有任何影响。一句话:SetWindowOrg
//就是指定一下,page space中哪个点为原点。
pDC->Rectangle(0,0,100,-100);
pDC->Rectangle(0,-100,50,-200);
同理,SetViewportOrg也是指定一下,device space中哪个点为原点,两个坐标系映射时,两个原点重合。
SetWindowExt设定page space的大小,SetViewportOrg设定device space的大小,其实,真正有意义的只是两者的比例关系,例如,在一个1024*768的显示屏上:
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(10240,7680);
pDC->SetViewportExt(1024,768);
pDC->Rectangle(0,0,100,100);
就会画一个10 pixels*10 pixels的矩形。其本质就是,X方向,每个逻辑单位有1024/10240个象素,Y方向每个逻辑单位有768/7680个象素。因此,下面的代码有相同的作用:
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(102400,76800);
pDC->SetViewportExt(10240,7680);
pDC->Rectangle(0,0,100,100);