用MFC实现和windows画图工具类似的工具

画图软件的基本功能包括基本图形绘制和填充(直线、椭圆、三角形、四边形、多边形),更改图元绘制条件(改变线条颜色、线型、填充方式)和图元的保存功能。要求:实现所见即所得绘图功能;实现图元选择功能;实现图元移动功能;实现画笔和画刷风格认定功能;实现图元修改功能;实现图形文件的存储功能。要求使用VC编写程序实现画图工具
用VC++里面的MFC做成窗体样式,有完整代码的,并且实现功能,我愿意出500分!我的邮箱是sxllyliu@gmail.com.请把整个文件发到这里,谢谢!

第1个回答  2008-04-22
我只会写简单的操作,给你做个参考吧
// DrawView.cpp : implementation of the CDrawView class
//

#include "stdafx.h"
#include "Draw.h"

#include "DrawDoc.h"
#include "DrawView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDrawView

IMPLEMENT_DYNCREATE(CDrawView, CView)

BEGIN_MESSAGE_MAP(CDrawView, CView)
//{{AFX_MSG_MAP(CDrawView)
ON_WM_CREATE()
ON_COMMAND(ID_DRAW_LINECOLOR, OnDrawLinecolor)
ON_COMMAND(ID_DRAW_FILLCOLOR, OnDrawFillcolor)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_DRAW_LINE,ID_DRAW_FILL, OnSelectDrawType)
ON_UPDATE_COMMAND_UI_RANGE(ID_DRAW_LINE,ID_DRAW_FILL, OnUpdateSelectDrawType)

ON_COMMAND_RANGE(ID_DRAW_LINEWIDTH_ONE,ID_DRAW_LINEWIDTH_FIVE, OnDrawLineWidth)
ON_UPDATE_COMMAND_UI_RANGE(ID_DRAW_LINEWIDTH_ONE,ID_DRAW_LINEWIDTH_FIVE,OnUpdateDrawLineWidth)
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction

CDrawView::CDrawView()
{
// TODO: add construction code here
m_pMemDC = new CDC;
m_pBitmap = new CBitmap;
m_nDrawType = -1;
m_nLineWidth = 1;
m_cLineColor = RGB(0,0,0);
m_cFillColor = RGB(0,0,255);
m_bDrawing = false;

}

CDrawView::~CDrawView()
{
delete m_pMemDC;
delete m_pBitmap;
}

BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing

void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);

pDC->BitBlt(0,0,m_nMaxX, m_nMaxY, m_pMemDC, 0,0, SRCCOPY);

m_pMemDC->SelectObject(pOldBitmap);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawView printing

BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics

#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}

void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers

int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
m_nMaxX = GetSystemMetrics(SM_CXSCREEN);
m_nMaxY = GetSystemMetrics(SM_CYSCREEN);

CDC * pDC = GetDC();
m_pMemDC->CreateCompatibleDC(pDC);
m_pBitmap->CreateCompatibleBitmap(pDC, m_nMaxX, m_nMaxY);

CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);

CBrush brush;
brush.CreateStockObject(WHITE_BRUSH);

CRect rect(-1,-1,m_nMaxX, m_nMaxY);
m_pMemDC->FillRect(rect,&brush);

m_pMemDC->SelectObject(pOldBitmap);
ReleaseDC(pDC);

return 0;
}

void CDrawView::OnSelectDrawType(UINT nID)
{
m_nDrawType = nID - ID_DRAW_LINE;
}

void CDrawView::OnUpdateSelectDrawType(CCmdUI * pCmdUI)
{
int nID = pCmdUI->m_nID - ID_DRAW_LINE;
if(nID == m_nDrawType)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}

void CDrawView::OnDrawLineWidth(UINT nID)
{
m_nLineWidth = nID - ID_DRAW_LINEWIDTH_ONE + 1;
}

void CDrawView::OnUpdateDrawLineWidth(CCmdUI * pCmdUI)
{
int nID = pCmdUI->m_nID - ID_DRAW_LINEWIDTH_ONE + 1;
if(nID == m_nLineWidth)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}

void CDrawView::OnDrawLinecolor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags |= CC_PREVENTFULLOPEN | CC_RGBINIT;
dlg.m_cc.rgbResult = m_cLineColor;
if(dlg.DoModal() == IDOK)
{
m_cLineColor = dlg.GetColor();
}

}

void CDrawView::OnDrawFillcolor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags |= CC_PREVENTFULLOPEN | CC_RGBINIT;
dlg.m_cc.rgbResult = m_cFillColor;
if(dlg.DoModal() == IDOK)
{
m_cFillColor = dlg.GetColor();
}

}

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
m_bDrawing = true;
m_ptStart = point;
m_ptOld = point;
if(m_nDrawType == 3)
{
CBrush * pOldBrush;
CBitmap * pOldBitmap;

CBrush brFill;
brFill.CreateSolidBrush(m_cFillColor);
pOldBrush = m_pMemDC->SelectObject(&brFill);
pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);

m_pMemDC->ExtFloodFill(point.x,point.y, m_pMemDC->GetPixel(point), FLOODFILLSURFACE);
Invalidate(false);
m_pMemDC->SelectObject(pOldBrush);
m_pMemDC->SelectObject(pOldBitmap);
m_bDrawing = false;
}

CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDrawing)
{
CDC * pDC = GetDC();
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
CPen pen;
pen.CreatePen(PS_SOLID, m_nLineWidth, m_cLineColor);
CPen * pOldPen = pDC->SelectObject(&pen);

CBrush * pOldBrush = (CBrush *) pDC->SelectStockObject(NULL_BRUSH);

CRect rectOld(m_ptStart, m_ptOld);

rectOld.NormalizeRect();

rectOld.InflateRect(m_nLineWidth, m_nLineWidth);

pDC->BitBlt(rectOld.left, rectOld.top, rectOld.Width(), rectOld.Height(),m_pMemDC, rectOld.left, rectOld.top, SRCCOPY);

CRect rectNew(m_ptStart,point);

switch(m_nDrawType)
{
case 0:
pDC->MoveTo(m_ptStart);
pDC->LineTo(point);
break;
case 1:
pDC->Rectangle(rectNew);
break;
case 2:
pDC->Ellipse(rectNew);
break;
default:
break;
}

m_pMemDC->SelectObject(pOldBitmap);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);

ReleaseDC(pDC);

m_ptOld = point;
}

CView::OnMouseMove(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDrawing)
{
m_bDrawing = false;

CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
CPen pen;
pen.CreatePen(PS_SOLID, m_nLineWidth, m_cLineColor);
CPen * pOldPen = m_pMemDC->SelectObject(&pen);

CBrush * pOldBrush = (CBrush *) m_pMemDC->SelectStockObject(NULL_BRUSH);

CRect rectNew(m_ptStart,point);

switch(m_nDrawType)
{
case 0:
m_pMemDC->MoveTo(m_ptStart);
m_pMemDC->LineTo(point);
break;
case 1:
m_pMemDC->Rectangle(rectNew);
break;
case 2:
m_pMemDC->Ellipse(rectNew);
break;
default:
break;
}
Invalidate(false);

m_pMemDC->SelectObject(pOldBitmap);
m_pMemDC->SelectObject(pOldPen);
m_pMemDC->SelectObject(pOldBrush);

}

ReleaseCapture();
CView::OnLButtonUp(nFlags, point);
}

BOOL CDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
switch(m_nDrawType)
{
case 0:
case 1:
case 2:
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
break;
case 3:
::SetCursor(AfxGetApp()->LoadCursor(ID_CURSOR_FILL));
break;
default:
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
break;
}

return CView::OnSetCursor(pWnd, nHitTest, message);
}
好久以前的了,要的话留下邮箱,我发给你~本回答被提问者采纳
第2个回答  2008-04-23
能实现这些的 就是要money的了
第3个回答  2008-04-22
吹牛!最大加分是20分!
呵呵,你是中间人还是初初初学者?哪有这么问的?

用MFC设计一个程序,类似画图板软件。要求能用鼠标画出各种图形,最好可...
作:将图片保存成BMP文件

编写一个类似于微软“画图板”的程序
我们也做这个 一共分六组 有函数曲线 画图板 俄罗斯方块 五子棋 扫雷 计算器,你用MFC类好好查查怎么调用相关函数即可 ,画图板可不是最难的那个呀

急求用MFC编写的画图板源代码
以下这个源代码就是用MFC写的,可以模拟Windows的画图程序。以后可以多去codeproject这个开源网站找找。http:\/\/www.codeproject.com\/Articles\/8494\/DrawTools

MFC 自绘工具栏
ass Wizard,然后弹出对话框你就可以在右侧Messages里点选command,再点击添加函数按钮就行了。工具栏的图标只要它的ID和对应的菜单项ID一样就行了。23、初始化函数是CView的派生类的PreCreateWindow,画图就在OnDraw,OnDraw是在窗口发生变化时重绘,但初始化要画图的话我都是在OnDraw里做的,可以用一...

用mfc如何实现 windows画图程序中的颜色板呢(调色板)
COLORREF clr;CColorDialog dlg;if(dlg.domodal==IDOK)clr=ccd.GetColor();这样是调用调色板的一种方法,本质上就是调用vc自带的资源CColorDialog类似的还有CFontDialog等

VC++中HPEN的用法
Windows提供了多种绘图工具,设备上下文来使用。它提供了钢笔画线,刷填写的内饰,和字体绘制文本。 MFC提供了图形对象类相当于Windows中的画图工具。下表列出了可用的类和等效的Windows图形设备接口(GDI)句柄类型。编程为Windows GDI一般文献适用于Microsoft基础类封装了GDI图形对象。类适用于Windows GDI对象...

如何用C++实现画图板
然后添加一个Image控件,用于装图片 和处理图片。基本就完了,再就是实现画圈 写文本 变大小 什么的 就用Image里的方法 和TCanvas这个类来实现。你说的这些方法都可以实现的。还有就是界面的设计,这个也很容易实现的,可以做的和 WINDOWS系统的 画图工具一样。说来说去实现很简单,但是不会有人直接...

学会MFC和C++能做点什么项目?
能做很多事情呀,比如windows上的大多数软件(QQ,浏览器,office等等)都是C++写的,不过现在很少用MFC写,MFC虽然比较不错、但是比较臃肿。很多服务器也是用C++写的,比如游戏服务器,web服务器(IIS,apache等等)。假如你C++够厉害,用C++写个网站也是可以的。

我想在基于对话框的MFC程序中画图,画图程序可以不写在OnPaint函数中吗...
1.可以不在OnPaint中 2.其机制是:a.windows是多窗口的,你的程序不应该把图画到其它地方去,所以你要取得窗口的专门管绘图的一个句柄,叫做 HDC b.你用MFC,要取它的话用这句: CDC* pDC=this->GetDC(); (某窗口的DC,例如本窗口就是this)c.然后就可以向DC中画了,例如:pDC->LineTo(100,100...

MFC中OnDraw和OnPaint的区别
其实DC是一个数据结构,它包含输出设备(不单指你的纯屏显示器,还包括打印机之类的输出设备)的绘图属性的描述。MFC提供了CPaintDC类和CWindwoDC类来实时的响应,而CPaintDC支持重画。当视图变得无效时(包括大小的改变,移动,被遮盖等等),Windows 将 WM_PAINT 消息发送给它。该视图的OnPaint 处理...

相似回答