我有一个与连接交互的WinForms应用程序。如果连接良好,我想显示一个绿色的(“一切都很好”)实心圆,否则,我想显示一个红色实心的圆。
我在工具箱中没有找到任何圆形元素,因此我认为必须自己绘制。
我创建了一个名为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);
}
}
每当我致电CheckSignedInState()
时如何在此图片框上绘画?
也许有比绘制更好的方法了吗? (我不想切换两个图像,因为可能会绘制更多状态)
答案 0 :(得分:1)
使用Label
控件绘制椭圆的简单示例。
您可以使用任何具有Paint()事件的控件来绘制一些形状。
也可以是Panel
,PictureBox
,Button
...
在类范围内声明的布尔变量(ClientIsSignedIn
)用于跟踪当前状态,如您的client.IsSignedIn
值所报告。
状态更改时,更新ClientIsSignedIn
和Invalidate()
提供视觉帮助的控件。
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);
}
答案 1 :(得分:0)
我认为不需要图片框。您可以尝试这样的事情:
[('O', 'terras'), ('O', 'ipsius'), ('O', 'Azar'), ('O', 'vocatas'), ('Places', 'Ta'), ('Places', 'Xellule'), ('O', 'et'), ('Places', 'Ginen'), ('Places', 'Chagem'), ('Places', 'in'), ('O', 'contrata'), ('Places', 'Deyr'), ('Places', 'Issafisaf')]
如果您需要在Picturebox上绘图,请务必调用picturebox.Invalidate();
答案 2 :(得分:-1)
您的代码可以正常工作,尽管看上去确实有点偏离中心,但是您不处置该对象的事实也可能引起问题。尝试将以下代码用作您的DrawClientStateIcon
方法
修改:下面的完整代码示例。我添加了一个按钮来切换客户端状态,这对我有用。
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();
}
}
}
答案 3 :(得分:-1)
您需要在“表单显示”事件中调用它。
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);
}