m_FontColor=RGB(0,0,0); //设置默认的颜色 }
步骤6:编写CMymenuView::OnDraw()函数。 void CTestMenuView::OnDraw(CDC* pDC) { CTestMenuDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here Redraw(pDC); }
步骤7:分别编写步骤3添加的3个函数,程序如下:
void CMymenuView::OnFontcolor1() { // TODO: Add your command handler code here m_FontColor=RGB(250,0,0); CDC * pDC=GetDC(); Redraw(pDC); }
void CMymenuView::OnFontcolor2() { // TODO: Add your command handler code here m_FontColor=RGB(0,250,0); CDC * pDC=GetDC(); Redraw(pDC); }
void CMymenuView::OnFontcolor3() { // TODO: Add your command handler code here m_FontColor=RGB(0,0,250); CDC * pDC=GetDC(); Redraw(pDC); }
步骤8:编写CTestMenuView::Redraw()函数 void CMymenuView::Redraw(CDC *pDC) { //设置文本颜色,显示测试内容 pDC->SetTextColor(m_FontColor); pDC->TextOut(30,30,\菜单测试程序!\
-- 31
}
步骤9:编译和运行程序,查看程序运行结果。
四、实验总结
1. 总结VC++ 6.0图形程序设计的基本方法和所涉及的基本内容。 2. 分析实验所得到的结果,你可以提出哪些改进?
-- 32
实验二 基本图形(元)生成技术(一)
直线生成算法
一、实验目的
在一个图形系统中,基本图形(也称为图元、图素等)的生成技术是最基本的,任何复杂的图形都是由基本图形组成的,基本图形生成的质量直接影响该图形系统绘图的质量。所以,需要设计出精确的基本图形生成算法,以确保图形系统绘图的精确性。本次实验的目的就是验证直线生成的三种扫描算法,并要求对基本算法进行扩充和改进,包括:利用Visual C++实现三种直线生成算法,验证算法的正确性;
二、实验任务
1. 理解三种直线生成算法思想,写出实现程序; 2. 添加鼠标功能,实现交互式画直线程序;
3. 将10个像素作为步距单位,编出Bresenham算法的示例。
三、基本知识和实验步骤
任务一:实现DDA画线程序
实验步骤:
1. 建立一个DDALine的工程文件; 2. 添加ddaline()成员函数
方法:在工作区中选择CLASSVIEW类窗口,右击CDDAlineView类,选择“add member function…”,定义如下的成员函数:
void ddaline(CDC* pDC,int x0,int y0,int x1,int y1,COLORREF color);
3. 编写自定义的成员函数ddaline()程序
void CDDALineView::ddaline(CDC* pDC, int x0, int y0, int x1, int y1, COLORREF color) {
int length,i; float x,y,dx,dy; length=abs(x1-x0); if (abs(y1-y0)>length) length=abs(y1-y0); dx=(x1-x0)/length; dy=(y1-y0)/length; x=x0+0.5;y=y0+0.5; for (i=1;i<=length;i++) {
-- 33
pDC->SetPixel((int)x,(int)y,color); x=x+dx;y=y+dy; } }
4.编写OnDraw()函数
void CDDALineView::OnDraw(CDC* pDC) {
CDDALineDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);
// TODO: add draw code for native data here ddaline(pDC,100,100,400,100,RGB(255,0,0)); ddaline(pDC,400,100,400,400,RGB(0,255,0)); ddaline(pDC,400,400,100,400,RGB(0,0,255)); ddaline(pDC,100,400,100,100,RGB(255,255,0)); ddaline(pDC,100,100,400,400,RGB(255,0,255)); ddaline(pDC,100,400,400,100,RGB(0,255,255));} }
5.编译、调试和运行程序,查看程序结果。
任务二、放大10倍后,算法演示程序
先画出(100,100)到(600,400)大小为10的网格,然后从(100,100)以10为单位,计算出直线上各个像素位置。 步骤:
1. 建立DDA2Line工程;
2. 在OnDraw()函数中画出网格,并调用DDA2Line()函数 void CDDA2LineView::OnDraw(CDC* pDC) {
CDDA2LineDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);
// TODO: add draw code for native data here //画网格 int gi,gj; //画横线
pDC->TextOut(90,90,\ pDC->MoveTo(100,100); for(gj=100;gj<=400;gj=gj+10) {
pDC->MoveTo(100,gj); pDC->LineTo(600,gj); }
//画竖线
pDC->MoveTo(100,100);
-- 34
for (gi=100;gi<=600;gi=gi+10) {
pDC->MoveTo(gi,100); pDC->LineTo(gi,400); }
pDC->TextOut(590,410,\
//画出像素点
DDA2line(pDC,100,100,600,400,RGB(255,0,0)); }
3.添加DDA2Line()成员函数
方法:在工作区中选择CLASSVIEW类窗口,右击CDDAlineView类,选择“add member function…”,定义如下的成员函数:
void DDA2Line(CDC* pDC,int x0,int y0,int x1,int y1,COLORREF color);
4.编写DDA2Line()函数
void CDDA2LineView::DDA2line(CDC *pDC, int x0, int y0, int x1, int y1, COLORREF color) {
int length,i,tx,ty; float x,y,dx,dy; length=abs(x1-x0); if (abs(y1-y0)>length) length=abs(y1-y0);
dx=(float)(x1-x0)/length; dy=(float)(y1-y0)/length; //char tbuf[20]; //sprintf(tbuf,\ //AfxMessageBox(tbuf); x=x0;y=y0;
for (i=0;i<=length;i=i+10) { tx=(int)((x+5)/10)*10; ty=(int)((y+5)/10)*10; pDC->SetPixel(tx,ty,color); pDC->Ellipse(tx-5,ty-5,tx+5,ty+5); x=x+dx*10;y=y+dy*10; } }
5.调试、运行程序
-- 35