110,502
社区成员
发帖
与我相关
我的任务
分享
// 按楼主的思路
// 初始化panel
private void button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Label l = new Label();
l.AutoSize = false;
l.Width = 100;
l.Height = 50;
l.BackColor = Color.White;
l.Left = (i % 4) * 110;
l.Top = (i / 4) * 60;
l.Name = "L" + i;
l.Text = "L" + i;
panel1.Controls.Add(l);
}
}
//更新标签上的文字,仅举个例子,操作没问题
private void button5_Click(object sender, EventArgs e)
{
panel1.Controls["L0"].Text = "Good";
panel1.Controls["L5"].Text = "Afternoon";
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace MyControls
{
/// <summary>
/// 房态控件,应用于实时显示房间状态
/// Author:Py
/// </summary>
public class RoomStatePanel : Panel
{
string m_roomno;
[Description("房间号码")]
public string RoomNo
{
get { return m_roomno; }
set
{
m_roomno = value;
label1.Text = value;
}
}
RoomState rs = RoomState.Unknown;
/// <summary>
/// 房态
/// </summary>
[Description("房态")]
public RoomState State
{
get { return rs; }
set { rs = value; }
}
#region 屏蔽Size等属性
[Browsable(false)]
public Size Size
{
get { return base.Size; }
set { base.Size = value; }
}
[Browsable(false)]
public int Width
{
get { return base.Width; }
set { base.Width = value; }
}
public int Height
{
get { return base.Height; }
set { base.Height = value; }
}
#endregion
Label label1 = new Label();
public RoomStatePanel()
: base()
{
BorderStyle = BorderStyle.None;
label1.Location = new Point(8, 8);
label1.AutoSize = true;
label1.BackColor = Color.Transparent;
label1.Font = new Font("黑体", 14.25F, FontStyle.Bold);
label1.ForeColor = Color.White;
Height = 40;
Width = 80;
Margin = new Padding(5);
Controls.Add(label1);
this.SizeChanged += new EventHandler(RoomStatePanel_SizeChanged);
}
/// <summary>
/// 禁止变更大小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void RoomStatePanel_SizeChanged(object sender, EventArgs e)
{
Height = 40;
Width = 80;
}
#region 重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
BackColor = Color.Transparent;
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 1);
Rectangle rect = ClientRectangle;
rect.Location = new Point(2, 2);
rect.Width = ClientRectangle.Width - 4;
rect.Height = ClientRectangle.Height - 4;
switch (State)
{
case RoomState.Dirty:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(140, 0, 0)), rect, 3);
break;
case RoomState.Immediately:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(255, 138, 0)), rect, 3);
break;
case RoomState.LodgingClear:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(0, 174, 0)), rect, 3);
break;
case RoomState.LodgingDirty:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(0, 109, 0)), rect, 3);
break;
case RoomState.OK:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(0, 174, 231)), rect, 3);
break;
case RoomState.Service:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(198, 0, 0)), rect, 3);
break;
case RoomState.Temp:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(0, 174, 231)), rect, 3);
break;
case RoomState.Unknown:
FillRoundRectangle(g, new SolidBrush(Color.FromArgb(198, 0, 0)), rect, 3);
break;
}
DrawRoundRectangle(g, p, rect, 3);
p.Dispose();
g.Dispose();
e.Graphics.Dispose();
}
void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.DrawPath(pen, path);
}
}
void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
{
using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
{
g.FillPath(brush, path);
}
}
GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
#endregion
}
/// <summary>
/// 房态
/// </summary>
public enum RoomState
{
/// <summary>
/// OK房
/// </summary>
OK,
/// <summary>
/// 在住干净房
/// </summary>
LodgingClear,
/// <summary>
/// 在住脏房
/// </summary>
LodgingDirty,
/// <summary>
/// 空脏房
/// </summary>
Dirty,
/// <summary>
/// 末知
/// </summary>
Unknown,
/// <summary>
/// 维修房
/// </summary>
Service,
/// <summary>
/// 临时态房
/// </summary>
Temp,
/// <summary>
/// 将到
/// </summary>
Immediately
}
}