Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

System.Drawing and others in C#

Back to Forum: Tech Off
  • User profile image
    Angus

    I am currently working on an application, and one of its features uses the following code:

    private void Form1_MouseMove(object sender, MouseEventArgs e)

    {

    if (e.Button == MouseButtons.Left)

    {

    System.Drawing.Pen myPen;

    myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 3);

    System.Drawing.Graphics formGraphics = this.CreateGraphics();

    formGraphics.DrawRectangle(myPen, e.X, e.Y, 1, 1);

    myPen.Dispose();

    formGraphics.Dispose();

    }

    else

    {}

    }

    I am having a problem in that when the user drags the mouse too fast, it leaves gaps in what should be a complete line. If anyone has a solution I would be very grateful.

    Thanks in advance

    Angus Higgins

  • User profile image
    mule

    Record the position at last mousemove event and draw a line from that point to the new point, that way you'll get a continuous line.

     

  • User profile image
    mule

    private void Picture_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {

    if (e.Button == MouseButtons.Left) {

    _drawing = true;

    _lastPoint = new Point(e.X, e.Y);

    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Picture_MouseMove);

    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Picture_MouseUp);

    }

    }

    private void Picture_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {

    if (_drawing) {

    Point newPoint = new Point(e.X, e.Y);

    DrawLine(_lastPoint, newPoint);

    _lastPoint = newPoint;

    }

    }

    private void Picture_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {

    if (e.Button == MouseButtons.Left) {

    this.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.Picture_MouseUp);

    this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.Picture_MouseMove);

    if (_drawing) {

    Point newPoint = new Point(e.X, e.Y);

    DrawLine(_lastPoint, newPoint);

    }

    _drawing = false;

    }

    }

    private void DrawLine(Point from, Point to) {

    Graphics g = this.CreateGraphics();

    using (Pen p = new Pen(Color.Black)) {

    g.DrawLine(p, from, to);

    }

    }

  • User profile image
    BrianPeiris

    OK, i'm adding to the original question; Shouldn't all drawing be done in the OnPaint() event?
    If so, how would you implement this program properly?
    If not, is the code below legal/proper in every way?
    Also, the code below does not handle rezising/minimizing of the window, what is the best way to fix that?
    Here's How I got it to work, just paste it onto a form with the appropriate eventhandlers declared (Based on mule's code):

    bool _drawing = false;
    Point _lastPoint;
    Point _newPoint;
    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            //Are these really necessary?
            //this.MouseUp -= new //System.Windows.Forms.MouseEventHandler(this.Picture_MouseUp);
            //this.MouseMove -= new //System.Windows.Forms.MouseEventHandler(this.Picture_MouseMove);

            if (_drawing)
            {
                _newPoint = new Point(e.X, e.Y);
                DrawLine(_lastPoint, _newPoint, this.CreateGraphics());
            }
            _drawing = false;
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (_drawing)
        {
            _lastPoint = _newPoint;
            _newPoint = new Point(e.X, e.Y);
            DrawLine(_lastPoint, _newPoint, this.CreateGraphics());
        }
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _drawing = true;
            _lastPoint = new Point(e.X, e.Y);
            _newPoint = new Point(e.X, e.Y);

            //Again, are these really neccesary?
            //this.MouseMove += new  //System.Windows.Forms.MouseEventHandler(this.Picture_MouseMove);
            //this.MouseUp += new //System.Windows.Forms.MouseEventHandler(this.Picture_MouseUp);

        }
    }

    private void DrawLine(Point from, Point to)
    {
        Graphics g = this.CreateGraphics();
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        Pen p = new Pen(Color.Red, 5);//Just to make it look nice
        p.StartCap = System.Drawing.Drawing2D.LineCap.Round;//Just to make it look nice
        p.EndCap = System.Drawing.Drawing2D.LineCap.Round;//Just to make it look nice
        g.DrawLine(p, from, to);
        g.Dispose();
        p.Dispose();
    }

  • User profile image
    mule

    Shouldn't all drawing be done in the OnPaint() event?

    Not necessarily.  The OnPaint event is for handling the drawing of the control itself. If you want to draw on a surface in your application, then you can create a Graphics object anytime you like. As I understand it.


    If not, is the code below legal/proper in every way?

    Legal? I don't think it commits any cardinal sins.

    Also, the code below does not handle rezising/minimizing of the window, what is the best way to fix that?

    What are you trying to achive?  Are you trying to write a simple paint program?

    That's going to be a lot more work. Firstly you are going to have to set what the size of the image you're creating is, and keep a bitmap of everything you've drawn to the surface each time it changes - keep a history of say 8 of these for undos. You should then be repainting that bitmap when you resize / restore from minimized.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.