-
MFC based paint
I'm making an MFC based paint as SDI in document view. I need help with making a interface for drawing rect , ellipse, etc.( I know the definition of the interface) . It could be helpfull if someone has a good example. I know the drawing functions but I'm not sure how to put all this together. This is what I have in mind:
///////////////////////////
[class IPick { }; // base interface, functions only
class PickCommon : public IPick {}; // common data members
class Line : public PickCommon {}; // line specific stuff
class Rect : public PickCommon {}; // rect specific stuff/]
/////////////////
Thanks a lot.
-
I'm confused. Do you need help with the actual drawing of shapes to the screen?
gg
-
I know how to draw I need an interface for the drawing and I don't know what functions I need in. I have to make a derived class for drawing like this.
////////////////
[ class Rect : public PickCommon {}; // rect specific stuff/]
This class is derived from this two and I need some functions in.
////////////////
[ class IPick { }; // base interface, functions only
class PickCommon : public IPick {}; // common data members /]
-
-
Yes this is helpfull but I need an interface.
I need all pure virtual functions in base class because I maybe don't need for every tool (line, rect, ellipse) some data members Then I will make derived class from interface and use his functions and add some data members I need.
I also need this class for saving in DocView class . My idea was to this in std::vector or std::list pointers on my bas class.
This is my implementation of View class so if this is helpfull I will be very greatfull.
Code:
// verz1View.cpp : implementation of the Cverz1View class
//
#include "stdafx.h"
#include "verz1.h"
#include "verz1Doc.h"
#include "verz1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Cverz1View
IMPLEMENT_DYNCREATE(Cverz1View, CView)
BEGIN_MESSAGE_MAP(Cverz1View, CView)
// 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)
//buttoni na toolbaru
ON_COMMAND(ID_TBBUTTON1,OnLine)
ON_COMMAND(ID_TBBUTTON2,OnRect)
ON_COMMAND(ID_TBBUTTON3,OnEllipse)
ON_COMMAND(ID_TBBUTTON4,OnPencil)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON1, OnSelectLine)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON2, OnSelectRect)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON3, OnSelectEllipse)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON4, OnSelectPencil)
ON_COMMAND(ID_TBBUTTON10,OnRed)
ON_COMMAND(ID_TBBUTTON11,OnGreen)
ON_COMMAND(ID_TBBUTTON12,OnBlue)
ON_COMMAND(ID_TBBUTTON13,OnBlack)
ON_COMMAND(ID_TBBUTTON14,OnWhite)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON10, OnSelectRed)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON11, OnSelectGreen)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON12, OnSelectBlue)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON13, OnSelectBlack)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON14, OnSelectWhite)
ON_COMMAND(ID_TBBUTTON5, OnBrushOn)
ON_COMMAND(ID_TBBUTTON6, OnBrushOff)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON5, OnSelectBrushOn)
ON_UPDATE_COMMAND_UI(ID_TBBUTTON6, OnSelectBrushOff)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
// Cverz1View construction/destruction
Cverz1View::Cverz1View() : bBrushOn(false)
{
CurBrush.lbColor = RGB(255,255,255);
CurBrush.lbHatch = BS_SOLID;
CurBrush.lbStyle = HS_CROSS;
CurPen.lopnColor = RGB(0,0,0);
CurPen.lopnStyle = PS_SOLID;
CurPen.lopnWidth.x = 1;
curTool = ID_TBBUTTON1 ;
curColor = ID_TBBUTTON10;
}
Cverz1View::~Cverz1View()
{
}
BOOL Cverz1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// Cverz1View drawing
void Cverz1View::OnDraw(CDC* pDC)
{
Cverz1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}
// Cverz1View printing
BOOL Cverz1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void Cverz1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void Cverz1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
// Cverz1View diagnostics
#ifdef _DEBUG
void Cverz1View::AssertValid() const
{
CView::AssertValid();
}
void Cverz1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
Cverz1Doc* Cverz1View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Cverz1Doc)));
return (Cverz1Doc*)m_pDocument;
}
#endif //_DEBUG
// Cverz1View message handlers
//////////////////////////////////////////////////////////////////////////////////////////////
void Cverz1View::OnLine() {
curTool = ID_TBBUTTON1;
}
void Cverz1View::OnRect() {
curTool = ID_TBBUTTON2;
}
void Cverz1View::OnEllipse() {
curTool = ID_TBBUTTON3;
}
void Cverz1View::OnPencil() {
curTool = ID_TBBUTTON4;
}
void Cverz1View::OnRed() {
curColor = ID_TBBUTTON10;
CurBrush.lbColor = RGB(255,0,0);
CurPen.lopnColor = RGB(255,0,0);
}
void Cverz1View::OnGreen() {
curColor = ID_TBBUTTON11;
CurBrush.lbColor = RGB(0,255,0);
CurPen.lopnColor = RGB(0,255,0);
}
void Cverz1View::OnBlue() {
curColor = ID_TBBUTTON12;
CurBrush.lbColor = RGB(0,0,255);
CurPen.lopnColor = RGB(0,0,255);
}
void Cverz1View::OnBlack() {
curColor = ID_TBBUTTON13;
CurBrush.lbColor = RGB(0,0,0);
CurPen.lopnColor = RGB(0,0,0);
}
void Cverz1View::OnWhite() {
curColor = ID_TBBUTTON14;
CurBrush.lbColor = RGB(255,255,255);
CurPen.lopnColor = RGB(255,255,255);
}
void Cverz1View::OnBrushOn()
{
bBrushOn=true; //select brush
CurBrush.lbStyle=BS_SOLID;
}
void Cverz1View::OnBrushOff()
{
bBrushOn=false; //select null brush
CurBrush.lbStyle=BS_NULL;
}
void Cverz1View::OnSelectBrushOn(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(bBrushOn);
}
void Cverz1View::OnSelectBrushOff(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(!bBrushOn);
}
/////////////////////////////////////////////////////////////////
// postavljanje flagova za oznaku pritisnutog buttona
void Cverz1View::OnSelectLine(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curTool == ID_TBBUTTON1 ? 1 : 0);
}
void Cverz1View::OnSelectRect(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curTool == ID_TBBUTTON2 ? 1 : 0);
}
void Cverz1View::OnSelectEllipse(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curTool == ID_TBBUTTON3 ? 1 : 0);
}
void Cverz1View::OnSelectPencil(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curTool == ID_TBBUTTON4 ? 1 : 0);
}
void Cverz1View::OnSelectRed(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curColor == ID_TBBUTTON10 ? 1 : 0);
}
void Cverz1View::OnSelectGreen(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curColor == ID_TBBUTTON11 ? 1 : 0);
}
void Cverz1View::OnSelectBlue(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curColor == ID_TBBUTTON12 ? 1 : 0);
}
void Cverz1View::OnSelectBlack(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curColor == ID_TBBUTTON13 ? 1 : 0);
}
void Cverz1View::OnSelectWhite(CCmdUI *pCmdUI) {
pCmdUI->SetCheck(curColor == ID_TBBUTTON14 ? 1 : 0);
}
void Cverz1View::OnLButtonDown(UINT nFlags, CPoint point)
{
Cverz1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// saving mouse position on button down
Point.x = point.x;
Point.y = point.y;
// tu bi išla logika za crtanje
switch(curTool) {
}
CView::OnLButtonDown(nFlags, point);
}
void Cverz1View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonUp(nFlags, point);
}
void Cverz1View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}