这是一个用MFC的绘图程序,由于太多不能上传,我只能打程序粘贴在下面:
(1)
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "绘图程序.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
//
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
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
}
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);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// the CREATESTRUCT cs
return TRUE;
}
//
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
//
// CMainFrame message handlers
(2)
// stdafx.cpp : source file that includes just the standard includes
// 绘图程序.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
(3)
// 绘图程序.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "绘图程序.h"
#include "MainFrm.h"
#include "绘图程序Doc.h"
#include "绘图程序View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// CMyApp
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
//{{AFX_MSG_MAP(CMyApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
//
// CMyApp construction
CMyApp::CMyApp()
{
// Place all significant initialization in InitInstance
}
// The one and only CMyApp object
CMyApp theApp;
// CMyApp initialization
BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMyView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CMyApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
// CMyApp message handlers
(4)
// 绘图程序Doc.cpp : implementation of the CMyDoc class
//
#include "stdafx.h"
#include "绘图程序.h"
#include "绘图程序Doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CMyDoc
IMPLEMENT_DYNCREATE(CMyDoc, CDocument)
BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
//{{AFX_MSG_MAP(CMyDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CMyDoc construction/destruction
CMyDoc::CMyDoc()
{
}
CMyDoc::~CMyDoc()
{
}
BOOL CMyDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// (SDI documents will reuse this document)
return TRUE;
}
// CMyDoc serialization
void CMyDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
}
else
{
}
}
// CMyDoc diagnostics
#ifdef _DEBUG
void CMyDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMyDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
//
// CMyDoc commands
(5)
// 绘图程序View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "绘图程序.h"
#include "绘图程序Doc.h"
#include "绘图程序View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_COMMAND(ID_COLOR, OnColor)
ON_COMMAND(ID_FILL, OnFill)
ON_COMMAND(ID_LINE, OnLine)
ON_COMMAND(ID_RECT, OnRect)
ON_COMMAND(ID_ELLURECT, OnEllurect)
ON_COMMAND(ID_ELLIPSE, OnEllipse)
ON_COMMAND(ID_WIDTH1, OnWidth1)
ON_COMMAND(ID_WIDTH2, OnWidth2)
ON_COMMAND(ID_WIDTH3, OnWidth3)
ON_WM_SETCURSOR()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
//
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBitmap* poldbmp=m_pmdc->SelectObject (m_pbmp);
pDC->BitBlt (0,0,m_nmx,m_nmy,m_pmdc,0,0,SRCCOPY);
m_pmdc->SelectObject (poldbmp);
}
//
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
//
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
//
// CMyView message handlers
void CMyView::OnColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;//构造颜色通用对话框类CColordialog的对象dlg
dlg.m_cc.Flags |=CC_PREVENTFULLOPEN | CC_RGBINIT;
dlg.m_cc.rgbResult =m_color;
if(dlg.DoModal()==IDOK)
{
m_color=dlg.GetColor();//获取颜色对话框中的颜色给m_color
}
}
void CMyView::OnFill()
{
// TODO: Add your command handler code here
m_type=0;//m_type为当前选择图象类型
}
void CMyView::OnLine()
{
// TODO: Add your command handler code here
m_type=1;
}
void CMyView::OnRect()
{
// TODO: Add your command handler code here
m_type=2;
}
void CMyView::OnEllurect()
{
// TODO: Add your command handler code here
m_type=3;
}
void CMyView::OnWidth1()
{
// TODO: Add your command handler code here
m_width=1;
}
void CMyView::OnWidth2()
{
// TODO: Add your command handler code here
m_width=2;
}
void CMyView::OnWidth3()
{
// TODO: Add your command handler code here
m_width=3;
}
void CMyView::OnEllipse()
{
// TODO: Add your command handler code here
m_type=4;
}
BOOL CMyView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
switch(m_type)
{
case 0:
::SetCursor(AfxGetApp()->LoadCursor (IDC_CURSOR1));
break;
case 1:
::SetCursor(AfxGetApp()->LoadCursor (IDC_CURSOR2));
case 2:
::SetCursor(AfxGetApp()->LoadCursor (IDC_CURSOR3));
case 3:
::SetCursor(AfxGetApp()->LoadCursor (IDC_CURSOR4));
case 4:
::SetCursor(AfxGetApp()->LoadCursor (IDC_CURSOR5));
break;
default:
CView::OnSetCursor(pWnd, nHitTest, message);
break;
}
return TRUE;
//return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bdoing) return; //如果正在画图,则返回
SetCapture(); //捕捉鼠标信息
m_bdoing=true; //画图标识
m_pnew=point; //画图的起点
m_pold=point; //当前鼠标的临时点
if(m_type=1) //如果填充则如下
{
CBrush* poldbrush; //定义保存内存DC中原画刷的指针
CBitmap* poldbmp;
CBrush bfill;
bfill.CreateSolidBrush(m_color);
poldbrush=m_pmdc->SelectObject(&bfill);
poldbmp=m_pmdc->SelectObject(m_pbmp);
m_pmdc->ExtFloodFill(point.x,point.y,m_pmdc->GetPixel(point),FLOODFILLSURFACE);
Invalidate(FALSE);
m_pmdc->SelectObject(poldbrush);
m_pmdc->SelectObject(poldbmp);
m_bdoing=FALSE; //画图结束
}
//CView::OnLButtonDown(nFlags,point);
}
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bdoing)
{
CDC * pDC=GetDC();
CBitmap* poldbmp=m_pmdc->SelectObject(m_pbmp);
CPen pen;
pen.CreatePen(PS_SOLID,m_width,m_color);
CPen* poldpen=m_pmdc->SelectObject(&pen);
CBrush* poldbrush=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
CRect rectold(m_pold,m_pnew);
rectold.NormalizeRect();
rectold.InflateRect(m_width,m_width);
pDC->BitBlt(rectold.left,rectold.top,rectold.Width(),rectold.Height(),m_pmdc,rectold.left,rectold.top,SRCCOPY);
CRect rectnew(m_pold,point);
switch(m_type)
{
case 1:
pDC->MoveTo(m_pold);
pDC->LineTo(point);
break;
case 2:
pDC->Rectangle(rectnew);
break;
case 3:
pDC->RoundRect(m_pold.x,m_pold.y,point.x,point.y,10,10);
break;
case 4:
pDC->Ellipse(rectnew);
break;
default:
break;
}
m_pmdc->SelectObject(poldbmp);
pDC->SelectObject(poldpen);
pDC->SelectObject(poldbrush);
ReleaseDC(pDC);
m_pnew=point;
}
//CView::OnMouseMove(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bdoing)
{
m_bdoing=FALSE;
CBitmap*poldbmp=m_pmdc->SelectObject(m_pbmp);
CPen pen;
pen.CreatePen(PS_SOLID,m_width,m_color);
CPen*poldpen=m_pmdc->SelectObject(&pen);
CBrush* poldbrush=(CBrush*)m_pmdc->SelectStockObject(NULL_BRUSH);
CRect rect(m_pnew,m_pold);
switch(m_type)
{
case 1:
m_pmdc->MoveTo(m_pold);
m_pmdc->LineTo(point);
break;
case 2:
m_pmdc->Rectangle(rect);
break;
case 3:
m_pmdc->RoundRect(m_pnew.x,m_pnew.y,m_pold.x,m_pold.y,10,10);
break;
case 4:
m_pmdc->Ellipse(rect);
break;
default:
break;
}
Invalidate(FALSE);
m_pmdc->SelectObject(poldbmp);
m_pmdc->SelectObject(poldpen);
m_pmdc->SelectObject(poldbrush);
}
ReleaseCapture();
}
[此贴子已经被作者于2006-1-3 11:10:36编辑过]