using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; namespace HotelPms.Share.Windows.Component { public partial class FunctionKeyBar : System.Windows.Forms.Control { private float m_Split = 5; StringFormat format = new StringFormat(); RectangleF[] cellsRectTitle = new RectangleF[12]; RectangleF[] cellsRectKey = new RectangleF[12]; private string[] m_KeyContext = new string[] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty}; /// /// F1の機能名 /// public string KeyContextF01 { set { m_KeyContext[0] = value; Invalidate(); } get { return m_KeyContext[0]; } } /// /// F2の機能名 /// public string KeyContextF02 { set { m_KeyContext[1] = value; Invalidate(); } get { return m_KeyContext[1]; } } /// /// F3の機能名 /// public string KeyContextF03 { set { m_KeyContext[2] = value; Invalidate(); } get { return m_KeyContext[2]; } } /// /// F4の機能名 /// public string KeyContextF04 { set { m_KeyContext[3] = value; Invalidate(); } get { return m_KeyContext[3]; } } /// /// F5の機能名 /// public string KeyContextF05 { set { m_KeyContext[4] = value; Invalidate(); } get { return m_KeyContext[4]; } } /// /// F6の機能名 /// public string KeyContextF06 { set { m_KeyContext[5] = value; Invalidate(); } get { return m_KeyContext[5]; } } /// /// F7の機能名 /// public string KeyContextF07 { set { m_KeyContext[6] = value; Invalidate(); } get { return m_KeyContext[6]; } } /// /// F8の機能名 /// public string KeyContextF08 { set { m_KeyContext[7] = value; Invalidate(); } get { return m_KeyContext[7]; } } /// /// F9の機能名 /// public string KeyContextF09 { set { m_KeyContext[8] = value; Invalidate(); } get { return m_KeyContext[8]; } } /// /// F10の機能名 /// public string KeyContextF10 { set { m_KeyContext[9] = value; Invalidate(); } get { return m_KeyContext[9]; } } /// /// F11の機能名 /// public string KeyContextF11 { set { m_KeyContext[10] = value; Invalidate(); } get { return m_KeyContext[10]; } } /// /// F12の機能名 /// public string KeyContextF12 { set { m_KeyContext[11] = value; Invalidate(); } get { return m_KeyContext[11]; } } private Color m_CaptionBeginColor = Color.Maroon; public Color CaptionBeginColor { get { return m_CaptionBeginColor; } set { m_CaptionBeginColor = value; Invalidate(); } } private Color m_CaptionEndColor = Color.Gold; public Color CaptionEndColor { get { return m_CaptionEndColor; } set { m_CaptionEndColor = value; Invalidate(); } } public FunctionKeyBar() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.Opaque | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.Selectable | ControlStyles.UserMouse, true); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; } protected override void OnPaint(PaintEventArgs e) { float width = (float)(this.Width - this.Padding.Left - this.Padding.Right - 2 * m_Split); float height = (float)(this.Height - this.Padding.Top - this.Padding.Bottom); float cellWidth = width / 12F; float cellHeight = height / 2F; float x = this.Padding.Left; for (int i = 0; i < 12; i++) { //上 cellsRectTitle[i] = new RectangleF(new PointF(x, this.Padding.Top), new SizeF(cellWidth, cellHeight)); //下 cellsRectKey[i] = new RectangleF(new PointF(x, this.Padding.Top + cellHeight), new SizeF(cellWidth, cellHeight)); x += cellWidth; if ((i + 1) % 4 == 0) { x += m_Split; } } e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height); //e.Graphics.FillRectangles(Brushes.Maroon, cellsRectTitle); for (int i = 0; i < 12; i++) { using (LinearGradientBrush brush = new LinearGradientBrush(cellsRectTitle[i], m_CaptionBeginColor, m_CaptionEndColor, LinearGradientMode.Vertical)) { brush.SetSigmaBellShape(0.4f, 0.45f); //创建基于钟形曲线的渐变过渡过程。 e.Graphics.FillRectangle(brush, cellsRectTitle[i]); } } e.Graphics.DrawRectangles(System.Drawing.Pens.Black, cellsRectTitle); for (int i = 0; i < 12; i++) { string key = string.Format("F{0}", i + 1); e.Graphics.DrawString(key, this.Font, Brushes.White, cellsRectTitle[i], format); if (m_KeyContext[i].Length > 0) { using (LinearGradientBrush brush = new LinearGradientBrush(cellsRectKey[i], Color.FromArgb(50, 169, 200), Color.FromArgb(0, 109, 189), LinearGradientMode.Vertical)) { brush.SetSigmaBellShape(0.4f, 0.45f); //创建基于钟形曲线的渐变过渡过程。 e.Graphics.FillRectangle(brush, cellsRectKey[i]); } } else { e.Graphics.FillRectangle(new SolidBrush((m_KeyContext[i].Length > 0) ? Color.FromArgb(0, 109, 189) : this.BackColor), cellsRectKey[i].X, cellsRectKey[i].Y, cellsRectKey[i].Width, cellsRectKey[i].Height); } e.Graphics.DrawRectangle(System.Drawing.Pens.Black, cellsRectKey[i].X, cellsRectKey[i].Y, cellsRectKey[i].Width, cellsRectKey[i].Height); if (m_KeyContext[i].Length > 0) { e.Graphics.DrawString(m_KeyContext[i], this.Font, Brushes.Yellow, cellsRectKey[i], format); } } base.OnPaint(e); } } }