TtT23
TtT23

Reputation: 7030

Ellipse does not stay drawn

I'm trying to draw an ellipse on a particular location of the window upon detecting mousedown event.

The ellipse is drawn without any issue but it disappears immediately after it is drawn.

I think it has to do with the application continuously processing WM_PAINT message but the application not drawing the ellipse on each paint message.

How do I make it so that the ellipse stays drawn on a particular coordinate of the window?

    private void rtbLogicCode_MouseDown(object sender, MouseEventArgs e)
    {
        Point p = new Point(e.X, e.Y);
        if (p.X < 39 && p.Y < 817)
        {
            LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(0, p.Y, 15, 15), Color.LightPink, Color.Red, 45);

            System.Drawing.Graphics formGraphics = rtbLogicCode.CreateGraphics();
            formGraphics.FillEllipse(lgb, 0, p.Y, 15, 15);
            this.Invalidate();
            lgb.Dispose();
            formGraphics.Dispose();
        }
        ...
    }

Upvotes: 1

Views: 148

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490108

The usual pattern would be for your WM_MOUSEDOWN handler to just record the location and invalidate the window. Then the WM_PAINT handler retrieves the information and draws appropriately.

Upvotes: 3

Related Questions