--------------------Configuration: PenBrush - Win32 Debug--------------------
Compiling...
PenBrushView.cpp
d:\program files (x86)\microsoft visual studio\myprojects\penbrush\penbrushview.cpp(4) : fatal error C1083: Cannot open precompiled header file: ‘Debug/PenBrush.pch‘: No such file or directory
Error executing cl.exe.
PenBrushView.obj - 1 error(s), 0 warning(s)
答:当按F4,错误定位到#include "stdafx.h";解决方法“这是编译时出现的错误,可暂不管这个错误,继续链接,可发现链接无误,如下
--------------------Configuration: PenBrush - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
PenBrush.cpp
MainFrm.cpp
PenBrushDoc.cpp
PenBrushView.cpp
Generating Code...
Linking...
PenBrush.exe - 0 error(s), 0 warning(s)
这时我们再次对这个工程进行编译就会这样
--------------------Configuration: PenBrush - Win32 Debug--------------------
Compiling...
Skipping... (no relevant changes detected)
PenBrushView.cpp
PenBrushView.obj - 0 error(s), 0 warning(s)
可以看到之前报的错误消失。这时再次链接,得到
--------------------Configuration: PenBrush - Win32 Debug--------------------
Linking...
PenBrush.exe - 0 error(s), 0 warning(s)
如此可以解决问题。
代码部分
CRect rect;
GetClientRect(rect);
pDC ->SetMapMode(MM_ANISOTROPIC);
pDC ->SetWindowExt(rect.Width(), rect.Height());
pDC ->SetViewportExt(rect.Width(), -rect.Height());
pDC ->SetViewportOrg(rect.Width()/2, rect.Height()/2);
rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
rect.DeflateRect(100, 100);
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
pOldPen = pDC ->SelectObject(&NewPen);
CBrush NewBrush, *pOldBrush;
NewBrush.CreateSolidBrush(RGB(255, 0, 0));
pOldBrush = pDC ->SelectObject(&NewBrush);
pDC ->Rectangle(&rect);
pDC ->SelectObject(pOldPen);
pDC ->SelectObject(pOldBrush);
代码1
CRect rect;
GetClientRect(rect);
pDC ->SetMapMode(MM_ANISOTROPIC);
pDC ->SetWindowExt(rect.Width(), rect.Height());
pDC ->SetViewportExt(rect.Width(), -rect.Height());
pDC ->SetViewportOrg(rect.Width()/2, rect.Height()/2);
pDC ->Rectangle(rect);
这时得到的长方形显示在第一象限,而且只能显示它的一部分(左下角部分)
代码2
CRect rect;
GetClientRect(rect);
pDC ->SetMapMode(MM_ANISOTROPIC);
pDC ->SetWindowExt(rect.Width(), rect.Height());
pDC ->SetViewportExt(rect.Width(), -rect.Height());
pDC ->SetViewportOrg(rect.Width()/2, rect.Height()/2);
rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
pOldPen = pDC ->SelectObject(&NewPen);
pDC ->Rectangle(rect);
这时得到的长方形就会是视图区范围
代码3
CRect rect;
GetClientRect(rect);
pDC ->SetMapMode(MM_ANISOTROPIC);
pDC ->SetWindowExt(rect.Width(), rect.Height());
pDC ->SetViewportExt(rect.Width(), -rect.Height());
pDC ->SetViewportOrg(rect.Width()/2, rect.Height()/2);
rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
rect.DeflateRect(100, 100);
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
pOldPen = pDC ->SelectObject(&NewPen);
pDC ->Rectangle(rect);
这时长方形就会缩小居中
回到最初的代码部分
在里面有这样的一段代码
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
pOldPen = pDC ->SelectObject(&NewPen);
CBrush NewBrush, *pOldBrush;
NewBrush.CreateSolidBrush(RGB(255, 0, 0));
pOldBrush = pDC ->SelectObject(&NewBrush);
知道它是创建画笔和画刷,并将它们选入设备上下文
然后在绘制时
pDC ->Rectangle(&rect);
可以看到,并没有什么颜色参数在里面;Pen用来画线,Brush用来填充;当用户没有创建画笔和画刷就是“黑笔白刷”。
这样,图形是会被覆盖的,底层的会被后来的上层给覆盖。
下面的代码可以解释这种情形
CRect rect;
GetClientRect(rect);
pDC ->SetMapMode(MM_ANISOTROPIC);
pDC ->SetWindowExt(rect.Width(), rect.Height());
pDC ->SetViewportExt(rect.Width(), -rect.Height());
pDC ->SetViewportOrg(rect.Width()/2, rect.Height()/2);
rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
rect.DeflateRect(100, 100);
CPen NewPen, *pOldPen;
NewPen.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
pOldPen = pDC ->SelectObject(&NewPen);
pDC ->Rectangle(rect);
CBrush NewBrush, *pOldBrush;
NewBrush.CreateSolidBrush(RGB(255, 0, 0));
pOldBrush = pDC ->SelectObject(&NewBrush);
pDC ->RoundRect(rect, CPoint(50, 50));
pDC ->SelectObject(pOldPen);
pDC ->SelectObject(pOldBrush);
这里先绘的长方形是“蓝边白面”,而后绘的钝角长方形是“蓝边红面”。