PanelEnhanced.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using System.Drawing.Drawing2D;
  12. namespace ProjectBase.Controls
  13. {
  14. public partial class PanelEnhanced : DevExpress.XtraEditors.XtraUserControl
  15. {
  16. private Color _borderColor;
  17. private int _borderWidth = 1;
  18. private DashStyle _borderStyle = DashStyle.Solid;
  19. private int _opacity = 125;
  20. public PanelEnhanced()
  21. {
  22. InitializeComponent();
  23. }
  24. #region Property
  25. [Category("Custom"), Description("Border Color")]
  26. public Color BorderColor
  27. {
  28. set { _borderColor = value; }
  29. get { return _borderColor; }
  30. }
  31. [Category("Custom"), Description("Border Width"), DefaultValue(1)]
  32. public int BorderWidth
  33. {
  34. set
  35. {
  36. if (value < 0) value = 0;
  37. _borderWidth = value;
  38. }
  39. get { return _borderWidth; }
  40. }
  41. [Category("Custom"), Description("Border Style"), DefaultValue(DashStyle.Solid)]
  42. public new DashStyle BorderStyle
  43. {
  44. set { this._borderStyle = value; this.Invalidate(); }
  45. get { return this._borderStyle; }
  46. }
  47. [Bindable(true), Category("Custom"), DefaultValue(125), Description("背景的透明度. 有效值0-255")]
  48. public int Opacity
  49. {
  50. get { return _opacity; }
  51. set
  52. {
  53. if (value > 255) value = 255;
  54. else if (value < 0) value = 0;
  55. _opacity = value;
  56. this.Invalidate();
  57. }
  58. }
  59. #endregion
  60. protected override void OnPaintBackground(PaintEventArgs e)
  61. {
  62. //base.OnPaintBackground(e);
  63. return;
  64. }
  65. protected override void OnPaint(PaintEventArgs e)
  66. {
  67. //使用双缓冲
  68. this.DoubleBuffered = true;
  69. //背景重绘移动至此
  70. if (this.BackgroundImage != null)
  71. {
  72. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  73. e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height), 0, 0,
  74. this.BackgroundImage.Width, this.BackgroundImage.Height, System.Drawing.GraphicsUnit.Pixel);
  75. }
  76. if (this._opacity > 0)
  77. {
  78. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this._opacity, this.BackColor)),
  79. this.ClientRectangle);
  80. }
  81. if (this._borderWidth > 0)
  82. {
  83. Pen pen = new Pen(this._borderColor, this._borderWidth);
  84. pen.DashStyle = this._borderStyle;
  85. e.Graphics.DrawRectangle(pen, e.ClipRectangle.Left, e.ClipRectangle.Top, this.Width - 1, this.Height - 1);
  86. pen.Dispose();
  87. }
  88. base.OnPaint(e);
  89. }
  90. }
  91. }