テキストとインジケータライトを表示するカスタムユーザーコントロールを作成し、以下のフォームに示すように、サービスの現在の実行状況を表示しました。これは、フォーム上で正常に動作しますが、私はそれをStatusStripコントロールに配置したいと思います。 StatusStripでのコントロールのサポートを有効にするためにToolStripControlHostから派生したクラスを作成しましたが、レンダリングはまったく行いません。コントロールの幅はゼロで、IDEで開いているときにフォームが点滅します。ToolStripにグラフィックをレンダリングする方法
私はToolStripRenderingクラスを見てきましたが、既存のサポートされているコントロールで使用することが意図されています。
これを達成する最もよい方法は何ですか?可能であれば?私のコードは以下の通りです。
私は.NET 4.0
ユーザーコントロールのコードでのVisual Studio 2010を使用しています:
public partial class ServiceStatus : UserControl
{
public ServiceStatus()
{
IndicatorSize = new System.Drawing.Size(20, 20);
Padding = new Padding(1, 1, 1, 1);
InitializeComponent();
}
public ServiceControllerStatus GetStatus()
{
using (var service = new ServiceController("MyService"))
{
return service.Status;
}
}
private void getStatusInfo(out string statusText, out Color indicatorColor)
{
try
{
ServiceControllerStatus status = GetStatus();
switch (status)
{
case ServiceControllerStatus.ContinuePending:
statusText = Properties.Resources.SERVICE_CONTINUE_PENDING;
indicatorColor = Color.Yellow;
break;
case ServiceControllerStatus.Paused:
statusText = Properties.Resources.SERVICE_PAUSED;
indicatorColor = Color.Yellow;
break;
case ServiceControllerStatus.PausePending:
statusText = Properties.Resources.SERVICE_PAUSE_PENDING;
indicatorColor = Color.Yellow;
break;
case ServiceControllerStatus.Running:
statusText = Properties.Resources.SERVICE_RUNNING;
indicatorColor = Color.Green;
break;
case ServiceControllerStatus.StartPending:
statusText = Properties.Resources.SERVICE_START_PENDING;
indicatorColor = Color.Yellow;
break;
case ServiceControllerStatus.Stopped:
statusText = Properties.Resources.SERVICE_STOPPED;
indicatorColor = Color.Red;
break;
case ServiceControllerStatus.StopPending:
statusText = Properties.Resources.SERVICE_STOP_PENDING;
indicatorColor = Color.Yellow;
break;
default:
statusText = default(String);
indicatorColor = default(Color);
break;
}
}
catch
{
statusText = Properties.Resources.SERVICE_CANNOT_CONNECT;
indicatorColor = Color.Red;
}
}
public Size IndicatorSize { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
string statusText;
Color indicatorColor;
getStatusInfo(out statusText, out indicatorColor);
Size textSize = TextRenderer.MeasureText(e.Graphics, statusText, Font, new Size(int.MaxValue, int.MinValue), TextFormatFlags.NoPadding);
this.Size = new Size(Padding.Left + textSize.Width + 5 + IndicatorSize.Width + Padding.Right,
Padding.Top + Math.Max(textSize.Height, IndicatorSize.Height) + Padding.Bottom);
Point textStart = new Point(Padding.Left, (this.Size.Height-textSize.Height)/2);
Rectangle textBounds = new Rectangle(textStart, textSize) ;
Brush indicatorBrush = new SolidBrush(indicatorColor) ;
Point indicatorStart = new Point(textStart.X + textSize.Width + 5, (this.Size.Height-IndicatorSize.Height)/2) ;
Rectangle indicatorBounds = new Rectangle(indicatorStart, IndicatorSize) ;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
TextRenderer.DrawText(e.Graphics, statusText, Font, textBounds, ForeColor, TextFormatFlags.NoPadding);
e.Graphics.FillEllipse(indicatorBrush, indicatorBounds);
}
}
ツールストリップコントロールホストコード:
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public class ToolStripServiceStatus : ToolStripControlHost
{
public ToolStripServiceStatus()
: base(new ServiceStatus())
{
}
}
UCをホストするには、コンストラクタ経由でホストに渡すべきではありませんか? 'public ToolStripServiceStatus(ServiceStatus sStatus)' – TaW
@Taw、それは基本クラスのコンストラクタに渡されます – tdemay