16,472
社区成员
发帖
与我相关
我的任务
分享
#include<afxwin.h>
#include<afxtempl.h>
#include"resource.h"
//可串行化的线段类//
class LineSegment:public CObject
{
public:
//起止线
int StartX,StartY,EndX,EndY;
LineSegment();
LineSegment(int,int,int,int);
LineSegment&operator=(LineSegment&);//为serialize准备的
void Serialize(CArchive&);
DECLARE_SERIAL(LineSegment)
};
IMPLEMENT_SERIAL(LineSegment,CObject,1)
LineSegment::LineSegment()
{
StartX=StartY=EndX=EndY=0;
}
LineSegment::LineSegment(int a,int b,int c,int d)
{
StartX=a;
StartY=b;
EndX=c;
EndY=d;
}
//重载操作符//
LineSegment&LineSegment::operator=(LineSegment& s)
{
StartX=s.StartX;
StartY=s.StartY;
EndX=s.EndX;
EndY=s.EndY;
return *this;
}
void LineSegment::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar<<StartX<<StartY<<EndX<<EndY;
}
else
{
ar>>StartX>>StartY>>EndX>>EndY;
}
}
///////////////////////////////////////////////////////
//文档//
class MyDocument:public CDocument
{
public:
CList<LineSegment,LineSegment&>LineList;
void Serialize(CArchive&);
void DeleteContents();
BOOL CanCloseFrame(CFrameWnd*);
DECLARE_SERIAL(MyDocument)
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_SERIAL(MyDocument,CDocument,1)
BEGIN_MESSAGE_MAP(MyDocument,CDocument)
END_MESSAGE_MAP()
//读取与存储有关的线段//
void MyDocument::Serialize(CArchive& ar)
{
int Counter;
POSITION pos;
LineSegment line;
CObject::Serialize(ar);
if(ar.IsStoring())
{
ar<<LineList.GetCount();
pos=LineList.GetHeadPosition();
while(pos!=NULL)
LineList.GetNext(pos).Serialize(ar);
}
else
{
ar>>Counter;
for(;Counter;Counter--)
{
line.Serialize(ar);
LineList.AddTail(line);
}
}
}
void MyDocument::DeleteContents()
{
LineList.RemoveAll();
}
//确认是否可以关闭窗口
BOOL MyDocument::CanCloseFrame(CFrameWnd* pFrame)
{
char *title;
if(IsModified())
{
title=new char[pFrame->GetWindowTextLength()+1];
pFrame->GetWindowText(title,pFrame->GetWindowTextLength()+1);
// 警告对话框
if(pFrame->MessageBox("Document not saved!\nwill you close it",title,MB_YESNO)==IDNO)
{
delete title;
return FALSE;
}
delete title;
}
return TRUE;
}
//////////////////////////////////////////////////////////
//View//
class MyView:public CScrollView
{
CClientDC *DragDC;
CPoint p1,p2;
public:
MyView():DragDC(NULL){}
~MyView(){delete DragDC;}
void OnDraw(CDC*);
void OnLButtonDown(UINT nFlags,CPoint point);
void OnLButtonUp(UINT nFlags,CPoint point);
void OnMouseMove(UINT nFlags,CPoint point);
int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_DYNCREATE(MyView)
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyView,CScrollView)
BEGIN_MESSAGE_MAP(MyView,CScrollView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
END_MESSAGE_MAP()
void MyView::OnLButtonDown(UINT nFlags,CPoint point)
{
CScrollView::OnLButtonDown(nFlags,point);
if(DragDC)
{delete DragDC;DragDC=NULL;}
//开始拖拽鼠标//
DragDC=new CClientDC(this);
DragDC->SetROP2(R2_NOT);
p1=p2=point;
//画线//
DragDC->MoveTo(p1);
DragDC->LineTo(p2);
SetCapture();
}
void MyView ::OnMouseMove(UINT nFlags,CPoint point)
{
CScrollView::OnMouseMove(nFlags,point);
if(DragDC)
{
DragDC->MoveTo(p1);
DragDC->LineTo(p2);
p2=point;
DragDC->MoveTo(p1);
DragDC->LineTo(p2);
}
}
void MyView::OnLButtonUp(UINT nFlags,CPoint point)
{
CPoint org;
CScrollView::OnLButtonDown(nFlags,point);
if(DragDC)
{
DragDC->MoveTo(p1);
DragDC->LineTo(p2);
p2=point;
DragDC->SetROP2(R2_COPYPEN);
DragDC->MoveTo(p1);
DragDC->LineTo(p2);
delete DragDC;
DragDC=NULL;
ReleaseCapture();
//获取client area左上角原点///
org=GetScrollPosition();
((MyDocument*)GetDocument())->LineList.AddTail(LineSegment(p1.x+org.x,p1.y+org.y,p2.x+org.x,p2.y+org.y));
GetDocument()->SetModifiedFlag(TRUE);
GetDocument()->UpdateAllViews(this);
}
}
void MyView::OnDraw(CDC* dc)
{
POSITION pos;
LineSegment line;
///获取文档///
MyDocument *doc=(MyDocument*)GetDocument();
pos=doc->LineList.GetHeadPosition();
while(pos!=NULL)
{
line=doc->LineList.GetNext(pos);
dc->MoveTo(line.StartX,line.StartY);
dc->LineTo(line.EndX,line.EndY);
}
}
int MyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CScrollView::OnCreate(lpCreateStruct))
return 1;
SetScrollSizes(MM_TEXT,CSize(4096,4096));
return 1;
}
///////////////////////////////////////////////////////////////////
//////mdi child frame
class MyFrameWindow:public CMDIChildWnd
{
public:
DECLARE_DYNCREATE(MyFrameWindow)
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyFrameWindow,CMDIChildWnd)
BEGIN_MESSAGE_MAP(MyFrameWindow,CMDIChildWnd)
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////////////////
/////MDI 主窗口
class MyMDIFrame:public CMDIFrameWnd
{
public:
DECLARE_DYNCREATE(MyMDIFrame)
DECLARE_MESSAGE_MAP()
protected:
afx_msg void CmExit();
};
IMPLEMENT_DYNCREATE(MyMDIFrame,CMDIFrameWnd)
BEGIN_MESSAGE_MAP(MyMDIFrame,CMDIFrameWnd)
ON_COMMAND(CM_EXIT,CmExit)
END_MESSAGE_MAP()
void MyMDIFrame::CmExit()
{
PostMessage(WM_CLOSE);
}
//////////////////////////////////////////////////////////////////
class MyApp:public CWinApp
{
CMultiDocTemplate *position;
public:
MyApp():CWinApp("hallo MDI"){}
BOOL InitInstance()
{
MyMDIFrame* Frame;
CMultiDocTemplate* pDocTemplate;
pDocTemplate=new CMultiDocTemplate(
IDR_MDICHILD,
RUNTIME_CLASS(MyDocument),
RUNTIME_CLASS(MyFrameWindow),
RUNTIME_CLASS(MyView));
AddDocTemplate(pDocTemplate);
Frame=new MyMDIFrame;
m_pMainWnd=Frame;
Frame->LoadFrame(IDR_MAINFRAME);
m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
LoadStdProfileSettings();
return TRUE;
}
DECLARE_MESSAGE_MAP()
}MyApplication;
BEGIN_MESSAGE_MAP(MyApp,CWinApp)
ON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)
END_MESSAGE_MAP()