CDC 有画图函数,例如直线,弧线,bitmap, 涂色...
void CXxxView::OnDraw(CDC* pDC)
{
CPoint p0,p[10]
CPen aPen[8]
CPen* poldPen
//定义各式各样线型和颜色,不定义则为 黑细实线。
aPen[0].CreatePen(PS_SOLID ,1,COLORREF(0x0000ff))aPen[1].CreatePen(PS_SOLID ,1,COLORREF(0x0090ff))
aPen[2].CreatePen(PS_SOLID ,1,COLORREF(0x40a0a0))
.....
aPen[7].CreatePen(PS_SOLID ,1,COLORREF(0x000000))
poldPen = pDC->SelectObject(&aPen[2])// 选线型和颜色aPen[2]
// CPoint p0,p[10]点的坐标值可以从 CDoc 传入,或这里给 p[i].y=cos(i*0.5),p[i].y=i*0.5
pDC->MoveTo(p0) // “笔”移到开始点p0,落笔
for (i=0i<10i++) pDC->LineTo(p[i])// “笔”画到p[0],p[1],..p[9]点
}
同样的,我们可以编写其他菜单项的处理函数代码,其代码如下://PS_DASH菜单项处理函数
void CDrawTestView::OnPenDash()
{
// TODO: Add your command handler code here
m_PenStyle = PS_DASH
this->SetMenuItemCheck(0,0,7,1)
}
//PS_DOT菜单项处理函数
void CDrawTestView::OnPenDot()
{
// TODO: Add your command handler code here
m_PenStyle = PS_DOT
this->SetMenuItemCheck(0,0,7,2)
}
//PS_DASHDOT菜单项处理函数
void CDrawTestView::OnPenDashdot()
{
// TODO: Add your command handler code here
m_PenStyle = PS_DASHDOT
this->SetMenuItemCheck(0,0,7,3)
}
//PS_DASHDOTDOT菜单项处理函数
void CDrawTestView::OnPenDashdotdot()
{
// TODO: Add your command handler code here
m_PenStyle = PS_DASHDOTDOT
this->SetMenuItemCheck(0,0,7,4)
}
//PS_NULL菜单项处理函数
void CDrawTestView::OnPenNull()
{
// TODO: Add your command handler code here
m_PenStyle = PS_NULL
this->SetMenuItemCheck(0,0,7,5)
}
//PS_INSIDEFRAME菜单项处理函数
void CDrawTestView::OnPenInsideframe()
{
// TODO: Add your command handler code here
m_PenStyle = PS_INSIDEFRAME
this->SetMenuItemCheck(0,0,7,6)
}
//宽度1菜单项处理函数
void CDrawTestView::OnPen1()
{
// TODO: Add your command handler code here
m_PenWidth = 1
this->SetMenuItemCheck(0,1,4,0)
}
//宽度3菜单项处理函数
void CDrawTestView::OnPen3()
{
// TODO: Add your command handler code here
m_PenWidth = 3
this->SetMenuItemCheck(0,1,4,1)
}
//宽度5菜单项处理函数
void CDrawTestView::OnPen5()
{
// TODO: Add your command handler code here
m_PenWidth = 5
this->SetMenuItemCheck(0,1,4,2)
}
//宽度7菜单项处理函数
void CDrawTestView::OnPen7()
{
// TODO: Add your command handler code here
m_PenWidth = 7
this->SetMenuItemCheck(0,1,4,3)
}
//画笔颜色下黑色菜单项处理函数
void CDrawTestView::OnPenBlack()
{
// TODO: Add your command handler code here
m_PenColor = RGB(0,0,0)
this->SetMenuItemCheck(0,2,4,0)
}
//画笔颜色下红色菜单项处理函数
void CDrawTestView::OnPenRed()
{
// TODO: Add your command handler code here
m_PenColor = RGB(255,0,0)
this->SetMenuItemCheck(0,2,4,1)
}
//画笔颜色下绿色菜单项处理函数
void CDrawTestView::OnPenGreen()
{
// TODO: Add your command handler code here
m_PenColor = RGB(0,255,0)
this->SetMenuItemCheck(0,2,4,2)
}
//画笔颜色下蓝色菜单项处理函数
void CDrawTestView::OnPenBlue()
{
// TODO: Add your command handler code here
m_PenColor = RGB(0,0,255)
this->SetMenuItemCheck(0,2,4,3)
}
//None菜单项处理函数
void CDrawTestView::OnBrushNone()
{
// TODO: Add your command handler code here
m_BrushStyle = -1
this->SetMenuItemCheck(1,0,7,0)
}
//HS_BDIAGONAL菜单项处理函数
void CDrawTestView::OnBrushBdiagonal()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_BDIAGONAL
this->SetMenuItemCheck(1,0,7,1)
}
//HS_CROSS菜单项处理函数
void CDrawTestView::OnBrushCross()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_CROSS
this->SetMenuItemCheck(1,0,7,2)
}
//HS_DIAGCROSS菜单项处理函数
void CDrawTestView::OnBrushDiagcross()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_DIAGCROSS
this->SetMenuItemCheck(1,0,7,3)
}
//HS_FDIAGONAL菜单项处理函数
void CDrawTestView::OnBrushFdiagonal()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_FDIAGONAL
this->SetMenuItemCheck(1,0,7,4)
}
//HS_HORIZONTAL菜单项处理函数
void CDrawTestView::OnBrushHorizontal()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_HORIZONTAL
this->SetMenuItemCheck(1,0,7,5)
}
//HS_VERITICAL菜单项处理函数
void CDrawTestView::OnBrushVertical()
{
// TODO: Add your command handler code here
m_BrushStyle = HS_VERTICAL
this->SetMenuItemCheck(1,0,7,6)
}
//画刷颜色下白色菜单项处理函数
void CDrawTestView::OnBrushWhite()
{
// TODO: Add your command handler code here
m_BrushColor = RGB(255,255,255)
this->SetMenuItemCheck(1,1,4,0)
}
//画刷颜色下红色菜单项处理函数
void CDrawTestView::OnBrushRed()
{
// TODO: Add your command handler code here
m_BrushColor = RGB(255,0,0)
this->SetMenuItemCheck(1,1,4,1)
}
//画刷颜色下绿色菜单项处理函数
void CDrawTestView::OnBrushGreen()
{
// TODO: Add your command handler code here
m_BrushColor = RGB(0,255,0)
this->SetMenuItemCheck(1,1,4,2)
}
//画刷颜色下蓝色菜单项处理函数
void CDrawTestView::OnBrushBlue()
{
// TODO: Add your command handler code here
m_BrushColor = RGB(0,0,255)
this->SetMenuItemCheck(1,1,4,3)
}
我们在CDrawTestView类中再添加两个函数GetPen和GetBrush来获得自定义画笔和画刷指针,函数代码如下:
//获得自定义画笔指针
CPen* CDrawTestView::GetPen()
{
return new CPen(m_PenStyle,m_PenWidth,m_PenColor)
}
//获得自定义画刷指针
CBrush* CDrawTestView::GetBrush()
{
//判断是否是阴影线画刷
if (m_BrushStyle == -1)
//不是阴影线画刷
return new CBrush(m_BrushColor)
else
//是阴影线画刷
return new CBrush(m_BrushStyle,m_BrushColor)
}
编写这两个函数的好处在于:如果以后需要更改获得画笔或者画刷的方式(比如画笔改用第三个构造函数来构造),只需要修改这两个函数即可。如果在每个绘图函数菜单项的处理函数中写构造画笔和画刷的代码,一旦画笔和画刷的构造方式要发生改变,就必须逐个修改每个绘图函数菜单项的处理函数。
现在修改绘图函数菜单项的处理函数,调用GetPen和GetBrush函数获得画笔和画刷,然后选用画笔和画刷,并在函数的最后删除画笔和画刷。这是因为获得画笔和画刷的函数每次都是构造新的画笔和画刷,而它们将占用系统资源,所以在使用完毕后要进行删除。而LineTo等绘制线形图形的绘图函数不受画刷影响,所以在这些绘图函数的处理函数中不需要选用画刷。因为所有线形图形绘图函数菜单项的处理函数要添加的代码是相同的,同样所有区域图形绘图函数菜单项的处理函数要添加的代码也是相同的,所以这里只列出“LineTo”(绘制线形图形)菜单项和“Rectangle”(绘制区域图形)菜单项修改后的处理函数代码,读者只需按照相同方法修改其它处理函数即可。修改后的代码如下:
//LineTo菜单项处理函数
void CDrawTestView::OnDrawLineto()
{
// TODO: Add your command handler code here
CClientDC dc(this)
CPen* pen = this->GetPen()
dc.SelectObject(pen)
dc.MoveTo(300,300)
dc.LineTo(400,400)
pen->DeleteObject()
}
//Rectangle菜单项处理函数
void CDrawTestView::OnDrawRectangle()
{
// TODO: Add your command handler code here
CClientDC dc(this)
CPen* pen = this->GetPen()
CBrush* brush = this->GetBrush()
dc.SelectObject(pen)
dc.SelectObject(brush)
dc.Rectangle(450,100,650,250)
pen->DeleteObject()
brush->DeleteObject()
}
代码中粗体字部分是后添加的代码,修改原则是在获得设备环境对象之后,调用绘图函数之前,先获得画笔或画笔和画刷,然后让设备环境对象选用它们。最后在所有绘图函数执行完后,删除前面获得的画笔或画笔和画刷。
下面我们看一个例子,修改OnDraw函数,输入如下代码:
void CDrawTestView::OnDraw(CDC* pDC)
{
CDrawTestDoc* pDoc = GetDocument()
ASSERT_VALID(pDoc)
// TODO: add draw code for native data here
//构造要输出的文本字符串
CString s
s = "DrawTest Function"
//构造文本要输出的矩形区域
CRect r
r.left = 50r.right =150
r.top =10r.bottom = 40
//绘制矩形,以便确定输出的文本在矩形区域中的位置
pDC->Rectangle(r)
//水平,垂直方向都居中,单行显示,超出区域范围不剪切
pDC->DrawText(s,r,DT_VCENTER|DT_CENTER|DT_SINGLELINE|DT_NOCLIP)
//向下平移矩形区域
r.top = r.top + 50r.bottom = r.bottom +50
pDC->Rectangle(r)
//垂直方向居底,水平方向靠右,单行显示
pDC->DrawText(s,r,DT_BOTTOM|DT_RIGHT|DT_SINGLELINE)
//向下平移矩形区域
r.top = r.top + 50r.bottom = r.bottom +50
pDC->Rectangle(r)
//垂直方向居顶,水平方向靠左,超出矩形区域时,在单词之间换行
pDC->DrawText(s,r,DT_TOP|DT_LEFT|DT_WORDBREAK)
//向下平移矩形区域
r.top = r.top + 50r.bottom = r.bottom +50
pDC->Rectangle(r)
//垂直方向居中,水平方向靠左,单行显示,超出范围时用省略号取代字符串尾部字符
pDC->DrawText(s,r,
DT_VCENTER|DT_LEFT|DT_SINGLELINE|DT_END_ELLIPSIS)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)