西安科技大学
《VC++程序设计》
实验报告
题 目 画图板 院、系(部) 计算机科学与技术学院 专业及班级 信计0902班 学 号 姓 名 HW 日 期 2012年12月13日
1 题目要求
做一个类似于windows的画图板
2 功能需求
1绘制直线; 2绘制矩形; 3绘制自由线; 4 绘制椭圆; 5填充图形;
6程序窗口左上角标题栏上DIY的一个的应用程序图标; 7为所画图形设置画笔颜色;
8为程序窗口右下角设置一个小的矩形框来显示当前时间;
3 总体设计
3.1 系统模块
1
俄罗斯方块 绘绘绘绘填程 制制制制充序 直矩椭自图窗 线 形 圆 由形 口线 左 上角 标题 栏上D IY 的一个 的应 用程序 图4 详细设计 1) 绘制直线、矩形、自由线、椭圆、填充图形 void CDibShowerView::MakeAllFlagsFalse() { bFillFlag=false; bEllipseFlag=false; bLineFlag=false; bDrawFlag=false;
bRectangleFlag=false;
2
为为所程画序图窗形口设右置下画角笔设颜置色 一个小的矩形框来显示当前时间 }
void CDibShowerView::OnToolsDrawfreehand() { }
void CDibShowerView::OnToolsEllipse() { }
void CDibShowerView::OnToolsFillfigure() { }
void CDibShowerView::OnToolsLine() { }
void CDibShowerView::OnToolsRectangle() {
3
// TODO: Add your command handler code here MakeAllFlagsFalse(); bDrawFlag=TRUE;
// TODO: Add your command handler code here MakeAllFlagsFalse(); bEllipseFlag=TRUE;
// TODO: Add your command handler code here MakeAllFlagsFalse(); bFillFlag=TRUE;
// TODO: Add your command handler code here MakeAllFlagsFalse(); bLineFlag=TRUE;
}
// TODO: Add your command handler code here MakeAllFlagsFalse(); bRectangleFlag=TRUE;
void CDibShowerView::OnUpdateToolsRectangle(CCmdUI* pCmdUI) { }
void CDibShowerView::OnUpdateToolsLine(CCmdUI* pCmdUI) { }
void CDibShowerView::OnUpdateToolsFillfigure(CCmdUI* pCmdUI) { }
void CDibShowerView::OnUpdateToolsEllipse(CCmdUI* pCmdUI) { }
void CDibShowerView::OnUpdateToolsDrawfreehand(CCmdUI* pCmdUI) { }
4
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(bRectangleFlag);
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(bLineFlag);
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(bFillFlag);
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(bEllipseFlag);
// TODO: Add your command update UI handler code here pCmdUI->SetCheck(bDrawFlag);
void CDibShowerView::OnLButtonDown(UINT nFlags, CPoint point) { }
void CDibShowerView::OnLButtonUp(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default Anchor.x=point.x; Anchor.y=point.y; OldPoint=point;
CView::OnLButtonDown(nFlags, point);
DrawTo.x=point.x;//记录直线结束点 DrawTo.y=point.y;
CClientDC*pDC=new CClientDC(this); CPen NewPen,*OldPen;
NewPen.CreatePen( PS_SOLID, 1, m_nClr ); //风格,宽度,颜色
OldPen = pDC->SelectObject(&NewPen); //选择新画笔
if (bLineFlag)//绘制直线 { }
if (bRectangleFlag)//绘制矩形 { }
if (bEllipseFlag)//绘制椭圆 {
pDC->SelectStockObject(NULL_BRUSH);
5
pDC->MoveTo(Anchor.x,Anchor.y); pDC->LineTo(DrawTo.x,DrawTo.y);
pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(Anchor.x,Anchor.y,DrawTo.x,DrawTo.y);
pDC->Ellipse(Anchor.x,Anchor.y,DrawTo.x,DrawTo.y);
}
if (bFillFlag) {
CColorDialog Dlg; COLORREF nClr;
if (Dlg.DoModal()==IDOK) { }
CBrush Brush;
Brush.CreateSolidBrush(nClr); pDC->SelectObject(&Brush);
nClr=Dlg.GetColor();
COLORREF FillColor=pDC->GetPixel(Anchor);
pDC->ExtFloodFill(Anchor.x,Anchor.y,FillColor,FLOODFILLSURFACE); }
pDC->SelectObject(OldPen); //还原系统画笔 }
void CDibShowerView::OnMouseMove(UINT nFlags, CPoint point) {
NewPen.DeleteObject(); //删除新画笔 delete pDC;
CView::OnLButtonUp(nFlags, point);
// TODO: Add your message handler code here and/or call default int nOldMode;
CClientDC *pDC=new CClientDC(this); if ((nFlags&MK_LBUTTON)&&bDrawFlag) {
6
}
2)为所画图形设置画笔颜色(这也是我对指导书程序改进的地方之一)
7
}
pDC->MoveTo(Anchor.x,Anchor.y); pDC->LineTo(point.x,point.y); Anchor=point;
if ((nFlags&MK_LBUTTON)&&bLineFlag) { }
if ((nFlags&MK_LBUTTON)&&bEllipseFlag) { }
delete pDC;
CView::OnMouseMove(nFlags, point);
nOldMode=pDC->GetROP2(); pDC->SetROP2(R2_NOT);
pDC->SelectStockObject(NULL_BRUSH);
pDC->Ellipse(OldPoint.x,OldPoint.y,Anchor.x,Anchor.y); pDC->Ellipse(Anchor.x,Anchor.y,point.x,point.y); OldPoint=point;
pDC->SetROP2(nOldMode);
nOldMode=pDC->GetROP2(); pDC->SetROP2(R2_NOT);
pDC->MoveTo(Anchor.x,Anchor.y); pDC->LineTo(OldPoint.x,OldPoint.y); pDC->MoveTo(Anchor.x,Anchor.y); pDC->LineTo(point.x,point.y); OldPoint=point;
pDC->SetROP2(nOldMode);
首先设置变量及定义一个新的画笔:
CPen NewPen,*OldPen;
NewPen.CreatePen( PS_SOLID, 1, m_nClr ); //风格,宽度,颜色 OldPen = pDC->SelectObject(&NewPen); //选择新画笔 ………
pDC->SelectObject(OldPen); //还原系统画笔 NewPen.DeleteObject(); //删除新画笔
其次对其设置一个消息响应函数: void CDibShowerView::OnColor() {
// TODO: Add your command handler code here CColorDialog Dlg; if (Dlg.DoModal()==IDOK) { } }
3)在应用程序的状态条上显示时间
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
m_nClr = Dlg.GetColor();
if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
TRACE0(\"Failed to create toolbar\\n\"); return -1; // fail to create }
8
}
void CMainFrame::OnTimer(UINT nIDEvent) {
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) {
TRACE0(\"Failed to create status bar\\n\"); return -1; // fail to create }
// TODO: Delete these three lines if you don't want the toolbar to // be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); //设置状态条的第三个框的宽度为100
m_wndStatusBar.SetPaneInfo(2,ID_SEPARATOR,SBPS_NORMAL,100); SetTimer(0,200,NULL); return 0;
// TODO: Add your message handler code here and/or call default //定义显示时间字符串 char strMsg[250]; //取得当前时间
CTime time=CTime::GetCurrentTime(); //将时间格式转换成字符串
sprintf(strMsg,\"北京时间:%02d:%02d:%02d\
time.GetHour(),time.GetMinute(),time.GetSecond()); //显示时间
m_wndStatusBar.SetPaneText(2,strMsg); CFrameWnd::OnTimer(nIDEvent);
9
}
4)程序窗口左上角标题栏上DIY的一个的应用程序图标
5 测试与实现
10
11
6 总结
这次的画图板是我个人单独完成的,在编写软件的过程中自己查阅资料以及参照了老师的上机指导书和课件,其中遇到一些问题,但是在同学们给了我很多帮助。虽然软件完成了,但是此次的画图板的编程中我对实验指导书有了一些完善,但是还是有一些功能没有实现,如新建、打开、保存一个图元文件等功能还不是很完善,在画好图形后可以保存但是在改变窗口大小后就刷新没了,像这些问题还没有解决,所以整体还是很粗糙,在以后的学习中我会利用我学到的只是继续完善它的。
12
因篇幅问题不能全部显示,请点此查看更多更全内容