What is the proper way to draw a line with mouse in C#

前端 未结 4 1075
盖世英雄少女心
盖世英雄少女心 2021-01-12 04:22

This is my drawing code to draw a custom line with mouse onto a Chart. Can you please help me to do it proper way ?

namespace Grafi
    {
        public p         


        
4条回答
  •  时光说笑
    2021-01-12 05:03

    You need to store your line somewhere.

    The steps you need to take are:

    1. Create somewhere to store your points in the main class, e.g . an ArrayList> - where each ArrayList contains the list of points in one line.
    2. wait for mousedown events, and create an array for a new line (e.g a new ArrayList) at the end of the list of lines
    3. wait for mousemoved events, and add a point to the last line in your list, whenever the mouse is dragged. ask to update your window here.
    4. in your paint, iterate through all lines, and draw each point of each line in the array.
    5. to clear the drawing, simply replace array with a blank list, and update the window.

    If you don't store your lines somewhere, they will be lost. Does this make sense?

    The other way of storing lines is by using a Canvas object, where the pixel-map of what is drawn is remembered and automatically drawn. If you don't mind not having your line data as vector points, and you might also want to use images or colours, then this might be a better approach.

提交回复
热议问题