[英]draw filled circle in WinForms C#
I have a WinForms application that interacts with a connection.我有一个与连接交互的 WinForms 应用程序。 If the connection is fine I want to show a green ("everything is fine") filled circle, if not I want to show a red filled circle.如果连接良好,我想显示一个绿色(“一切都很好”)实心圆圈,否则我想显示一个红色实心圆圈。
I found no circle element in the toolbox so I think I have to draw it on my own.我在工具箱中没有找到圆形元素,所以我想我必须自己绘制它。
I created a picture box called picBoxClientState
and started with this code我创建了一个名为picBoxClientState
的图片框并从这段代码开始
public partial class FrmMain : Form
{
public void CheckSignedInState()
{
// some other code
DrawClientStateIcon(client.IsSignedIn);
}
private void DrawClientStateIcon(bool isSignedIn)
{
Point rectangleLocation = picBoxClientState.Location;
Size rectangleSize = picBoxClientState.Size;
Rectangle rectangle = new Rectangle(rectangleLocation, rectangleSize);
Color iconColor = isSignedIn ? Color.Green : Color.Red;
SolidBrush iconBrush = new SolidBrush(iconColor);
Graphics graphics = picBoxClientState.CreateGraphics();
graphics.FillEllipse(iconBrush, rectangle);
}
}
How can I draw on this picturebox whenever I call CheckSignedInState()
?每当我调用CheckSignedInState()
时,如何在此图片框上绘制?
Maybe there is a better way instead of drawing?也许有更好的方法而不是绘图? (I don't want to toggle two images because there might be more states to draw) (我不想切换两个图像,因为可能有更多的状态要绘制)
A simple example using a Label
control to draw an ellipse.一个使用Label
控件绘制椭圆的简单示例。
You can use any control that has a Paint event to draw shapes.您可以使用任何具有Paint 事件的控件来绘制形状。
It could also be a Panel
, a PictureBox
, a Button
...它也可以是Panel
、 PictureBox
、 Button
......
A bool
variable ( clientIsSignedIn
) declared at Class scope is used to keep track of the current status, as reported by your client.IsSignedIn
value.在 Class 范围内声明的bool
变量 ( clientIsSignedIn
) 用于跟踪当前状态,如您的client.IsSignedIn
值所报告的client.IsSignedIn
。
When the status changes, update clientIsSignedIn
and Invalidate()
the Control that provides the visual aid.当状态改变时,更新clientIsSignedIn
和Invalidate()
提供视觉帮助的 Control。
bool clientIsSignedIn = false;
public void CheckSignedInState()
{
// some other code
clientIsSignedIn = client.IsSignedIn;
lblVisualStatus.Invalidate();
}
private void lblVisualStatus_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillEllipse((clientIsSignedIn) ? Brushes.Green : Brushes.Red, ((Control)sender).ClientRectangle);
}
I don't think picture box is necessary.我不认为图片框是必要的。 You can try something like this:你可以尝试这样的事情:
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new
System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(200, 200, 30, 30));
myBrush.Dispose();
formGraphics.Dispose();
}
If you need to draw on picturebox be sure to call picturebox.Invalidate();如果你需要在picturebox上绘制一定要调用picturebox.Invalidate();
your code works, it did seem to draw a bit off center though but also the fact that you weren't disposing of the object could potentially cause issues too.您的代码有效,但它似乎确实偏离了中心,但您没有处理该对象的事实也可能导致问题。 Try using the code below as your DrawClientStateIcon
method尝试使用下面的代码作为您的DrawClientStateIcon
方法
Edit : full code example below.编辑:下面的完整代码示例。 I added a button to toggle the state of the client and this works for me.我添加了一个按钮来切换客户端的状态,这对我有用。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public class Client
{
public bool IsSignedIn { get; set; }
}
Client client = new Client()
{
IsSignedIn = false
};
public Form1()
{
InitializeComponent();
}
public void CheckSignedInState()
{
// some other code
DrawClientStateIcon(client.IsSignedIn);
}
private void DrawClientStateIcon(bool isSignedIn)
{
Point rectangleLocation = picBoxClientState.Location;
Size rectangleSize = picBoxClientState.Size;
Rectangle rectangle = new Rectangle(rectangleLocation, new Size(rectangleSize.Width / 2, rectangleSize.Height / 2));
Color iconColor = isSignedIn ? Color.Green : Color.Red;
using (SolidBrush iconBrush = new SolidBrush(iconColor))
{
using (Graphics graphics = picBoxClientState.CreateGraphics())
{
graphics.FillEllipse(iconBrush, rectangle);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
client.IsSignedIn = !client.IsSignedIn;
CheckSignedInState();
}
}
}
You need to call it in Form Shown event.您需要在 Form Shown 事件中调用它。
private void FrmGraphics_Shown(object sender, EventArgs e)
{
DrawClientStateIcon(true);
}
private void DrawClientStateIcon(bool isSignedIn)
{
Point rectangleLocation = picBoxClientState.Location;
Size rectangleSize = picBoxClientState.Size;
Rectangle rectangle = new Rectangle(rectangleLocation, rectangleSize);
Color iconColor = isSignedIn ? Color.Green : Color.Red;
SolidBrush iconBrush = new SolidBrush(iconColor);
Graphics graphics = base.CreateGraphics();
graphics.FillEllipse(iconBrush, rectangle);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.