Drawing controls similar to brushes

Source: Internet
Author: User
Tags bool data structures

The source code runs as shown below:

Presumably everyone has used windows with its own brush, which is a small and easy-to-use software. In my spare time, I simulated the brush myself to develop a similar program (certainly not as rich as a paintbrush). It mainly completes the function of drawing straight line, curve, Circle, ellipse, rectangle, polygon, support operation of Clipboard, support Undo, Repeat, Save as bitmap file, open bitmap file. This example is developed with MFC and will eventually be converted to controls for ease of use.

Set up a single document project demo, the following will be divided into four parts to introduce the implementation of related functions.

First, drawing function

This program contains a variety of entities: lines, curves, circles, ellipses, rectangles, and so on, using different entity classes are implemented, these entity classes are derived from the same base class: Cdrawobject. This can greatly simplify the process of processing different entities.

Although different entities have different representations and data structures, they have many of the same characteristics, such as the color definition function, the entity drawing function, and so on, in the base class, these common data members and member functions are described. The entity base class is defined as follows:

class CDrawObject : public CObject
{
private:
  COLORREF m_PenColor;//图元颜色
  int  m_iPenWidth; //画笔宽度
  bool m_bFill;   //是否填充
public:
  bool m_bSelected;
  long m_nStyle;//图元类型
  CDrawObject(){m_bSelected = false;};
  void SetPenColor(COLORREF color);//设置图元颜色
  COLORREF GetPenColor();//获得图元颜色
  void SetPenWidth(int width) {m_iPenWidth = width;};
  int GetPenWidth() {return m_iPenWidth;};
  void SetFill(bool fill) { m_bFill = fill;};
  bool GetFill() {return m_bFill;};
  virtual void Draw(CDC* pDC) {};
  virtual void MoveAt(CDC* pDC, long x, long y) {};
  virtual  void EndPoint(CDC* pDC) {};
  virtual void NewPoint(long x, long y){};
  //图形对象第一点坐标,如果返回false则结束绘图
  virtual int AddPoint(long x, long y){return 0;};
};

The specific implementation function of each entity, please refer to the source code.

The realization of drawing function is mainly done in the view class. First create the appropriate menu and toolbar buttons to set the entity's style, color, brush thickness, padding, and so on. Also overload the OnLButtonDown (press the left mouse button), OnMouseMove (mouse move), OnLButtonUp (release the left mouse button) three functions. The creation process is as follows:

1, press the left button to create a new instance of the entity class;

2, tracking the mouse movement to modify the entity, to obtain the visual effect of WYSIWYG;

3, Release the left button, draw the end.

Second, clipboard operation

The functions associated with the clipboard operation are mainly as follows:

Open clipboard: OpenClipboard ();

Empty clipboard: EmptyClipboard ();

Save to Clipboard: SetClipboardData (Cf_bitmap, BITMAP. Getsafehandle ());

Remove the contents of the Clipboard: GetClipboardData (CF_BITMAP);

Close clipboard: CloseClipboard ();

As for the realization of the visual effect, should have used "rubber band", here stole a little lazy, directly draw a rectangle:). The cut and copy implementations are basically the same, and you need to clear the selected rectangular area when cutting. When pasting, you need to first determine if there is anything in the Clipboard.

Iii. Withdrawal and repetition

In order to undo and repeat, I define a class stack of my own, which is similar to a stack that defines the size of the stack at initialization time, pops up the top of the stack, adds new elements, and so on, and saves a pointer m_icurpos that represents the current position. The pointer moves forward when it is undone, moves backwards when it repeats, and if there is a new operation after the undo, the current length should be changed to M_icurpos, that is, the element after m_icurpos in the stack is invalid. As for what is saved in the stack, the Saveinstack () function is invoked after each operation to save the contents of the screen to a hbitmap type of variable. This method is a bit stupid, but I can't think of a better way: (. But the actual effect of this method is still good. )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.