TransparentPanel.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace IntelligentControlForsx.MyControls
  9. {
  10. public class TransparentPanel : Panel
  11. {
  12. public TransparentPanel()
  13. {
  14. SetStyle(ControlStyles.SupportsTransparentBackColor
  15. | ControlStyles.Opaque,
  16. true);
  17. BackColor = Color.Transparent;
  18. }
  19. protected override CreateParams CreateParams
  20. {
  21. get
  22. {
  23. CreateParams cp = base.CreateParams;
  24. /* Window extended styles
  25. #define WS_EX_DLGMODALFRAME 0x00000001L
  26. #define WS_EX_DRAGDETECT 0x00000002L
  27. #define WS_EX_NOPARENTNOTIFY 0x00000004L
  28. #define WS_EX_TOPMOST 0x00000008L
  29. #define WS_EX_ACCEPTFILES 0x00000010L
  30. #define WS_EX_TRANSPARENT 0x00000020L*/
  31. cp.ExStyle = cp.ExStyle | 0x20;
  32. return cp;
  33. }
  34. }
  35. protected override void OnPaint(PaintEventArgs e)
  36. {
  37. if (BackColor != Color.Transparent)
  38. {
  39. var bounds = new Rectangle(0, 0, Width - 1, Height - 1);
  40. //const int alpha = 165;
  41. using (var bckColor = new SolidBrush(Color.FromArgb(_alpha, BackColor)))
  42. {
  43. e.Graphics.FillRectangle(bckColor, bounds);
  44. }
  45. }
  46. base.OnPaint(e);
  47. }
  48. protected override void OnBackColorChanged(EventArgs e)
  49. {
  50. Parent.Invalidate(this.Bounds, true);
  51. base.OnBackColorChanged(e);
  52. }
  53. protected override void OnParentBackColorChanged(EventArgs e)
  54. {
  55. Invalidate();
  56. base.OnParentBackColorChanged(e);
  57. }
  58. private int _alpha = 168;
  59. [Browsable(true), Category("Data")]
  60. public int Alpha
  61. {
  62. get
  63. {
  64. return _alpha;
  65. }
  66. set
  67. {
  68. _alpha = value;
  69. this.Invalidate();
  70. }
  71. }
  72. }
  73. }