简体   繁体   中英

How-to draw a circle on a changing background

There is a timer on the form and in its Tick event I've:

        this.BackColor = ColorTranslator.FromHtml("#" + ColorCodesBack[_index]);

        CurrentColor = ColorTranslator.FromHtml("#" + ColorCodesFore[_index]);
        SolidBrush sb = new SolidBrush(CurrentColor);

        g.FillEllipse(sb, this.Width/2 -200, this.Height/2 - 200, 200, 200);
        g.DrawImage(b, 150, 150);

The problem is just background changes on every tick and I don't see a Circle on the form.

What you should do is put your code in the forms Paint event. That will cause your code to redraw ever time the form has to repaint. Like running your mouse over the form or moving the form. Also where are you declaring your graphic object? Because the only way it will be drawn on your form is if you do:

Graphics g = this.CreateGraphics();

If you use the paint event you won't even need a timer object.

You should combine this code with the same tick event that is changing the backgroud. Otherwise you effectively have a race condition as to which "tick" will be fired first. Combining them will solve that problem.

The suggestion to update the background and the foreground at the same time is solid.

If you cannot control this, perhaps you could add a transparent control to your window on top of the background, and draw onto the transparent control? That way you would always be above in the Z-Order. This would also reduce your need to redraw regularly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM