二维绘图工具文档xjhWord文件下载.docx
《二维绘图工具文档xjhWord文件下载.docx》由会员分享,可在线阅读,更多相关《二维绘图工具文档xjhWord文件下载.docx(33页珍藏版)》请在冰点文库上搜索。
![二维绘图工具文档xjhWord文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/11/413e3ea7-6346-437d-87b8-c3fc780b4af9/413e3ea7-6346-437d-87b8-c3fc780b4af91.gif)
4.总结
4.1程序的问题和缺陷-------------------------------------------------------
4.2通过这次编写程序的认知和感触----------------------------------------
使用VC开发平台,MFC框架实现一个画图程序,尽可能多的实现Windows自带的画图功能,并扩展其功能。
在视图类中完成图形的绘制,显示,重绘,清空,撤消,设置画笔,设置字体,打开bmp图片。
在文档类保存了,画笔的信息。
1.1基本绘图功能:
1)用鼠标可以绘制图形,包括一下几种:
直线,带箭头的线,任意线,圆形,椭圆,矩形。
2)可以对画笔进行设置:
画笔的颜色,画笔的线宽,画笔的形状。
3)可以保存所有的图形到内存中,所有图形绘制后,拉动窗口发生重绘不改变。
4)可以将所有绘制的图形保存到文件中,并且可以读取。
5)可以“新建”空白绘图文档。
6)可以查看最近打开的文档。
7)可以打印用户绘制好后的图形,打印预览你绘制的图形。
8)可以完成一部分的“撤消”功能,
1.2附加功能:
(1)应用程序的标题栏上有程序的图标。
(2)可选择打开或关闭工具栏。
(3)可以打开一些文件中放的测试图片。
、
(4)有一个开机启动画面。
(5)有个绘图工具栏。
2.程序实现的主要代码
//实现文件
IMPLEMENT_SERIAL(CGraph,CObject,1)
//文档串行化的宏定义DECLARE_SERIAL(CGraph)
CGraph:
:
CGraph()
{
}
CGraph(UINTDrawIndex,CPointFirstPoint,CPointSecondPoint,COLORREFcolor,UINTLineWidth,intLineStyle)
{//5¸
ö
²
Î
Ê
ý
µ
Ä
¹
Ô
ì
º
¯
this->
m_DrawIndex=DrawIndex;
m_FirstPoint=FirstPoint;
m_SecondPoint=SecondPoint;
m_color=color;
m_nLineStyle=LineStyle;
m_nLineWidth=LineWidth;
~CGraph()
voidCGraph:
Draw(CDC*pDC)
{//CGraphµ
»
æ
·
½
·
¨
¸
ù
¾
Ý
m_DrawIndexµ
Í
¬
µ
÷
Ó
Ã
CPenhpen(m_nLineStyle,m_nLineWidth,m_color);
CBrush*pBrush=CBrush:
FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush*pOldBrush=pDC->
SelectObject(pBrush);
switch(m_DrawIndex)
{
case1:
pDC->
SelectObject(&
hpen);
MoveTo(m_FirstPoint);
LineTo(m_SecondPoint);
break;
case2:
case3:
{
floatflRlt;
flRlt=(float)((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.x-m_SecondPoint.x)+(m_FirstPoint.y-m_SecondPoint.y)*(m_FirstPoint.y-m_SecondPoint.y));
flRlt=sqrt(flRlt);
if(flRlt==0)
return;
floatflLength=15;
floatflAngle=100;
//É
è
Ö
Ð
Ï
ò
¼
Ç
¶
È
floattmpX=(float)(m_SecondPoint.x)+((float)(m_FirstPoint.x)-(float)(m_SecondPoint.x))*flLength/flRlt;
floattmpY=(float)(m_SecondPoint.y)+((float)(m_FirstPoint.y)-(float)(m_SecondPoint.y))*flLength/flRlt;
floatfl1X=(tmpX-(float)(m_SecondPoint.x))*cos(-flAngle/2)-(tmpY-(float)(m_SecondPoint.y))*sin(-flAngle/2)+(float)(m_SecondPoint.x);
floatfl1Y=(tmpY-(float)(m_SecondPoint.y))*cos(-flAngle/2)+(tmpX-(float)(m_SecondPoint.x))*sin(-flAngle/2)+(float)(m_SecondPoint.y);
floatfl2X=(tmpX-(float)(m_SecondPoint.x))*cos(flAngle/2)-(tmpY-(float)(m_SecondPoint.y))*sin(flAngle/2)+(float)(m_SecondPoint.x);
floatfl2Y=(tmpY-(float)(m_SecondPoint.y))*cos(flAngle/2)+(tmpX-(float)(m_SecondPoint.x))*sin(flAngle/2)+(float)(m_SecondPoint.y);
MoveTo(m_SecondPoint.x,m_SecondPoint.y);
LineTo(fl1X,fl1Y);
LineTo(fl2X,fl2Y);
MoveTo(fl1X,fl1Y);
}
case4:
Ellipse(CRect(m_FirstPoint,m_SecondPoint));
SelectObject(pOldBrush);
case5:
case6:
Rectangle(CRect(m_FirstPoint,m_SecondPoint));
}
Serialize(CArchive&
ar)
{//Ó
CArchive½
ø
þ
Ù
×
//>
>
Ë
³
<
Ò
ª
à
if(ar.IsStoring())
ar<
m_DrawIndex<
m_FirstPoint<
m_SecondPoint<
m_color<
m_nLineWidth<
m_nLineStyle;
else
ar>
m_DrawIndex>
m_FirstPoint>
m_SecondPoint>
m_color>
m_nLineWidth>
;
//任意曲线类
classCStroke:
publicCObject//È
â
ú
ß
À
public:
CStroke(UINTnPenWidth,COLORREFcolor);
protected:
CStroke();
DECLARE_SERIAL(CStroke)
//Attributes
UINTm_nPenWidth;
//onepenwidthappliestoentirestroke
COLORREFm_color;
//颜色
CArray<
CPoint,CPoint>
m_pointArray;
//seriesofconnectedpoints
//Operations
BOOLDrawStroke(CDC*pDC);
virtualvoidSerialize(CArchive&
ar);
};
实现文件CStroke.cpp
IMPLEMENT_SERIAL(CStroke,CObject,2)
CStroke:
CStroke()
//Thisemptyconstructorshouldbeusedbyserializationonly
CStroke(UINTnPenWidth,COLORREFcolor)
m_nPenWidth=nPenWidth;
m_color=color;
voidCStroke:
Serialize(CArchive&
ar)
if(ar.IsStoring())
ar<
(WORD)m_nPenWidth<
m_color;
m_pointArray.Serialize(ar);
WORDw;
ar>
w>
m_nPenWidth=w;
BOOLCStroke:
DrawStroke(CDC*pDC)
CPenpenStroke;
if(!
penStroke.CreatePen(PS_SOLID,m_nPenWidth,m_color))
returnFALSE;
CPen*pOldPen=pDC->
penStroke);
pDC->
MoveTo(m_pointArray[0]);
for(inti=1;
i<
m_pointArray.GetSize();
i++)
LineTo(m_pointArray[i]);
SelectObject(pOldPen);
returnTRUE;
//线形状和线宽设置对话框类
classCSettingDlg:
publicCDialog
CSettingDlg(CWnd*pParent=NULL);
//{{AFX_DATA(CSettingDlg)
enum{IDD=IDD_DIALOG1};
UINTm_nLineWidth;
//线宽
intm_nLineStyle;
//线形状
//线的颜色
//}}AFX_DATA
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CSettingDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);
//DDX/DDVsupport
//}}AFX_VIRTUAL
//Generatedmessagemapfunctions
//{{AFX_MSG(CSettingDlg)
afx_msgvoidOnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
实现文件CSettingDlg.cpp
CSettingDlg:
CSettingDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CSettingDlg:
IDD,pParent)
//{{AFX_DATA_INIT(CSettingDlg)
m_nLineWidth=0;
m_nLineStyle=-1;
//}}AFX_DATA_INIT
voidCSettingDlg:
DoDataExchange(CDataExchange*pDX)
CDialog:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSettingDlg)
DDX_Text(pDX,IDC_EDIT2,m_nLineWidth);
DDX_Radio(pDX,IDC_RADIO1,m_nLineStyle);
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CSettingDlg,CDialog)
//{{AFX_MSG_MAP(CSettingDlg)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//CSettingDlgmessagehandlers
OnPaint()
//这里添加了代码用于对话框的示例显示
CPaintDCdc(this);
//devicecontextforpainting
//TODO:
Addyourmessagehandlercodehere
UpdateData();
CPenpen(m_nLineStyle,m_nLineWidth,m_color);
//定义画笔
dc.SelectObject(&
pen);
CRectrect;
//定义矩形区域
GetDlgItem(IDC_STATIC1)->
GetWindowRect(&
rect);
//得到示例静态空间的矩形区域
ScreenToClient(&
///坐标转换
dc.MoveTo(rect.left+20,rect.top+rect.Height()/2);
dc.LineTo(rect.right-20,rect.top+rect.Height()/2);
//DonotcallCDialog:
OnPaint()forpaintingmessages
//添加成员变量CToolBarm_newToolBar一个新的绘图工具栏
//在CMainFrame中的OnCreat()函数中添加的代码:
CSplashWnd:
ShowSplashScreen(this);
//启动画面
Sleep(1000);
//程序暂停
//------------------新建画图工具栏-----------------------------------//
if(!
m_newToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|
CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||
!
m_newToolBar.LoadToolBar(IDR_DRAW_TOOLBAR))
TRACE0("
Failedtocreatetoolbar\n"
);
return-1;
//failtocreate
m_newToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&
m_newToolBar);
CMFCDoc.h中
virtual~CMFCDoc();
CPenm_penCur;
//pencreatedaccordingto
CTypedPtrList<
CObList,CStroke*>
m_strokeList;
//currentuser-selectedpenwidth
//颜色«
CPen*GetCurrentPen();
CStroke*NewStroke();
voidInitDocument();
//初始化任意曲线的画笔
voidDeleteContents();
//删除任意曲线
BOOLOnOpenDocument(LPCTSTRlpszPathName);
CMFCDoc.cpp中
CStroke*CMFCDoc:
NewStroke()
{//Ð
Â
newStrokeº
POSITIONpos=GetFirstViewPosition();
CMFCView*pView=(CMFCView*)GetNextView(pos);
//获取视类的指针
//获取视类的画笔宽度和颜色
m_nPenWidth=pView->
m_nLineWidth;
m_color=pView->
m_color;
CStroke*pStrokeItem=newCStroke(m_nPenWidth,m_color);
m_strokeList.AddTail(pStrokeItem);
SetModifiedFlag();
//Markthedocumentashavingbeenmodified,for
//purposesofconfirmingFileClose.
returnpStrokeItem;
BOOLCMFCDoc:
OnNewDocument()
{/新建空白文档
CDocument:
OnNewDocument())
addreinitializationcodehere
//(SDIdocumentswillreusethisdocument)
/新建空白文档就是把当前的数据全部删除掉
POSITIONpos=GetFirstViewPositi