Want a drawn circle to follow my mouse in C #

First, I start in C #, so please be careful.

I am trying to circle follow my cursor. I do not want the traces left behind.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{

    drawCircle(e.X, e.Y);

}

private void drawCircle(int x, int y)
{
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);
    Graphics graphics = CreateGraphics();
    graphics.DrawEllipse(
        skyBluePen, x - 150, y - 150, 300, 300);
    graphics.Dispose();
    this.Invalidate();
}

This works fine as it draws it and is centered on the mouse for each mouse move. However, "this.Invalidate ();" wrong. He "learns" the form after each movement, so I can only see its glimpses. However, apart from this, each individual back circle remains on the screen.

How can I get one circle โ€œgracefullyโ€ by following my mouse without being too nervous and not preserving all past circles?

+3
source share
6 answers

- :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Point local = this.PointToClient(Cursor.Position);
        e.Graphics.DrawEllipse(Pens.Red, local.X-25, local.Y-25, 20, 20);
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Invalidate();
    }
}

, . .

+14

- ...

private int x = 0;
private int y = 0;

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    x = e.X;
    y = e.Y;

    this.Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);

    e.Graphics.DrawEllipse(skyBluePen, x - 150, y - 150, 300, 300);

}
+3

, .

, , , .

+1

- , , ( , ), , .

, (, , , .

using System;
using System.Drawing;
using System.Windows.Forms;

class C:Form
{
static void Main(){Application.Run(new C());}

private Point? _MousePosition = null;

protected override void OnMouseMove(MouseEventArgs e) {
 _MousePosition = e.Location;
 this.Invalidate();
}

protected override void OnPaint(PaintEventArgs e) {
 if(_MousePosition.HasValue) {
  using(Pen skyBluePen = new Pen(Brushes.DeepSkyBlue)) {
   e.Graphics.DrawEllipse(skyBluePen, _MousePosition.Value.X - 150, _MousePosition.Value.Y - 150, 300, 300);
  }
 }
}
}
+1

:

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);

, . . !

+1

-, , - , . , . .

, , , . , , - ( , ). , , ( Paint ).

private void DrawScene(Point mouseLocation)
{
     myGraphics.Clear(Color.White)
     myGraphics.DrawEllipse(skyBluePen, mouseLocation.X - 150, mouseLocation.Y - 150, 300, 300);
     myDrawingSurface.Refresh(); //myDrawingSurface can be a Form or a PictureBox or whatever you'd like.  Normally, you'd only Invalidate areas that have changed
}

private void myDrawingSurface_MouseMove(object sender, MouseEventArgs e)
{
    DrawScene(e.Location);
}

private void myDrawingSurface_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(myBitmap, 0, 0); //Can't remember the exact signature
}

"" - ImageBox Image , , Refresh PictureBox. . Paint .

. myBitmap myGraphics . , . , . . , .

0

Source: https://habr.com/ru/post/1712812/


All Articles