Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC10.0
Hi
 
I am developing an application, a drawing tool, in which I have struck half the way.
This tool is one drawing tool basically. I have used GDI for drawing to Device context.
I have various shapes drawing requirements like lines, circles, polygons etc.
 
The drawing part I have done on the onDraw() event and working.
 
Once the shapes are drawn, later I wants to modify the same object.
 
For that how to select the previous shapes with mouse and then drag, move, resize etc.
 
Please help me in this.
 
Thanks
Posted 16-May-13 20:08pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I can’t suggest a method to identify the drawn object from DC and modify the same.
 
But you can implement the above requirements (change shape of drawn objects) by following design.
Prepare separate classes for each graphic item, like Rectangle, Line, Triangle etc. These classes should be derived from a common base class, say it GraphicBase.
class GraphicBase
{
public:
    GraphicBase(void);
    ~GraphicBase(void);
 
    virtual void Draw();
    virtual bool IsInside( POINT ptMousePos);
    virtual void MouseMove( POINT ptMousePos);
};
 
// Derived from GraphicBase to draw a Rectangle
class GraphicRectangle: public GraphicBase
{
public:
    GraphicRectangle(void);
    ~GraphicRectangle(void);
 
    void Draw();
    bool IsInside(POINT ptMousePos);
    void MouseMove( POINT ptMousePos);
 
private:
    RECT m_Position; // Position of graphic item. This wil be used to draw Rectangle.
    
};
Here Draw() is used to draw a graphic object. IsInside() is used to identify whether an object is inside the graphic object. MouseMove() LButtonDown() and LButtonUp is used to resize the graphic item.
Main class( Dlg class) will hold all graphic items in a list, and whenever a mouse down recives in Dlg class, it will call IsInside() of all graphic items to idntify the graphic item need to be resized. Say it as m_pActiveGraphicItem. After that mouse move messages will send to the active graphic item, m_pActiveGraphicItem. The change in mouse movement is identified to apply resize action of the graphic item. Whenever mouse up received in Dlg class, it will stop resize action[By assigning m_pActiveGraphicItem as NULL].
After each mouse movement, invalidating the entire client area to redraw all graphic items.
OnPaint() of Dlg class will be like this
Dlg::OnPaint()
{
for( i=0;m_GraphicItems.size(); i++)
{
M_pGraphicItems[i]->Draw(); // To draw graphic item
}
}
GraphicRectangle::MouseMove() should recalculate m_Position to redraw the rectangle in new size. The intended DC should be shared to all Graphic items in any method, as static member function, or keep it as a protected member in GraphicBase.
 
IsInside() of each graphic item should return whether incoming point is inside the graphic item or not. Please use HRGN or other method to identify the mouse position is inside the region of graphic item.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100