简体   繁体   English

如何在不断变化的背景上画一个圆

[英]How-to draw a circle on a changing background

There is a timer on the form and in its Tick event I've: 窗体上有一个计时器,在它的Tick事件中,我有:

        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. 您应该做的是将代码放入Paint事件表单中。 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. 如果使用paint事件,则甚至不需要计时器对象。

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. 否则,您实际上将具有关于哪个“ as”将首先触发的竞赛条件。 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. 这样,您将始终位于Z顺序上方。 This would also reduce your need to redraw regularly. 这也将减少您定期重绘的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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