当前位置:   article > 正文

C# winfrom 设置控件透明背景色,比如TextBox_winform textbox 透明

winform textbox 透明
  1. public partial class Text : TextBox
  2. {
  3. public Text()
  4. {
  5. this.BorderStyle = BorderStyle.FixedSingle;
  6. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  7. InitializeComponent();
  8. }
  9. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  10. static extern IntPtr LoadLibrary(string lpFileName);
  11. protected override CreateParams CreateParams
  12. {
  13. get
  14. {
  15. CreateParams prams = base.CreateParams;
  16. if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
  17. {
  18. prams.ExStyle |= 0x020; // transparent
  19. prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
  20. }
  21. return prams;
  22. }
  23. }
  24. }
  1. public partial class TextBoxRua : Control
  2. {
  3. public TextBoxRua()
  4. {
  5. this.SetStyle(ControlStyles.UserPaint, true);
  6. this.SetStyle(ControlStyles.ResizeRedraw, true);
  7. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  8. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  9. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  10. m_tbx = new Text();
  11. m_tbx.Multiline = true;
  12. m_tbx.BorderStyle = BorderStyle.None;
  13. this.BackColor = Color.Transparent;
  14. m_tbx.GotFocus += (s, e) => { m_bFocus = true; this.Invalidate(); };
  15. m_tbx.LostFocus += (s, e) => { m_bFocus = false; this.Invalidate(); };
  16. m_tbx.KeyDown += (s, e) => this.OnKeyDown(e);
  17. m_tbx.KeyPress += (s, e) => this.OnKeyPress(e);
  18. m_tbx.KeyUp += (s, e) => this.OnKeyUp(e);
  19. m_tbx.TextChanged += (s, e) => this.OnTextChanged(e);
  20. //m_tbx.BackColorChanged += (s, e) => m_tbx.BackColor = Color.White;
  21. this.Controls.Add(m_tbx);
  22. }
  23. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  24. static extern IntPtr LoadLibrary(string lpFileName);
  25. protected override CreateParams CreateParams
  26. {
  27. get
  28. {
  29. CreateParams prams = base.CreateParams;
  30. if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
  31. {
  32. prams.ExStyle |= 0x020; // transparent
  33. prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
  34. }
  35. return prams;
  36. }
  37. }
  38. public override string Text
  39. {
  40. get { return m_tbx.Text; }
  41. set
  42. {
  43. if (m_tbx.Text == value) return;
  44. m_tbx.Text = value;
  45. }
  46. }
  47. public Text TextBox
  48. {
  49. get { return m_tbx; }
  50. }
  51. public override Color BackColor
  52. {
  53. get { return Color.Transparent; }
  54. set { }
  55. }
  56. public override Color ForeColor
  57. {
  58. get { return base.ForeColor; }
  59. set
  60. {
  61. if (base.ForeColor == value) return;
  62. base.ForeColor = value;
  63. m_tbx.ForeColor = value;
  64. }
  65. }
  66. private bool _ReadOnly;
  67. public bool ReadOnly
  68. {
  69. get { return m_tbx.ReadOnly; }
  70. set { m_tbx.ReadOnly = value; m_tbx.BackColor = Color.Transparent; }
  71. }
  72. private Text m_tbx;
  73. private bool m_bFocus;
  74. public event EventHandler TextChanged;
  75. protected virtual void OnTextChanged(EventArgs e)
  76. {
  77. if (this.TextChanged != null) this.TextChanged(this, e);
  78. }
  79. protected override void OnResize(EventArgs e)
  80. {
  81. m_tbx.Left = m_tbx.Top = 3;
  82. m_tbx.Width = this.Width - 6;
  83. m_tbx.Height = this.Height - 6;
  84. base.OnResize(e);
  85. }
  86. protected override void OnPaint(PaintEventArgs e)
  87. {
  88. Graphics g = e.Graphics;
  89. g.SmoothingMode = SmoothingMode.HighQuality;
  90. using (SolidBrush sb = new SolidBrush(Color.Transparent))
  91. {
  92. g.FillPath(
  93. sb,
  94. GetRoundRectPath(new Rectangle(0, 0, this.Width - 1, this.Height - 1), 5)
  95. );
  96. }
  97. using (LinearGradientBrush lgb = new LinearGradientBrush(
  98. new Point(0, 1), new Point(0, this.Height),
  99. Color.LightGray, Color.White))
  100. {
  101. g.DrawPath(
  102. new Pen(lgb),
  103. GetRoundRectPath(new Rectangle(0, 1, this.Width - 1, this.Height - 2), 5)
  104. );
  105. }
  106. using (LinearGradientBrush lgb = new LinearGradientBrush(
  107. new Point(0, 0), new Point(0, this.Height - 1),
  108. m_bFocus ? Color.DodgerBlue : Color.Gray, Color.FromArgb(20, 0, 0, 0)))
  109. {
  110. g.DrawPath(new Pen(lgb), GetRoundRectPath(new Rectangle(0, 0, this.Width - 1, this.Height - 2), 5));
  111. }
  112. base.OnPaint(e);
  113. }
  114. /// <summary>
  115. /// 适合调整参数radius...
  116. /// </summary>
  117. /// <param name="rect"></param>
  118. /// <param name="radius">radius 角度</param>
  119. /// <returns></returns>
  120. public GraphicsPath GetRoundRectPath(RectangleF rect, float radius)
  121. {
  122. return GetRoundRectPath(rect.X, rect.Y, rect.Width, rect.Height, radius);
  123. }
  124. public GraphicsPath GetRoundRectPath(float X, float Y, float width, float height, float radius)
  125. {
  126. GraphicsPath path = new GraphicsPath();
  127. path.AddLine(X + radius, Y, (X + width) - (radius * 2f), Y);
  128. path.AddArc((X + width) - (radius * 2f), Y, radius * 2f, radius * 2f, 270f, 90f);
  129. path.AddLine((float)(X + width), (float)(Y + radius), (float)(X + width), (float)((Y + height) - (radius * 2f)));
  130. path.AddArc((float)((X + width) - (radius * 2f)), (float)((Y + height) - (radius * 2f)), (float)(radius * 2f), (float)(radius * 2f), 0f, 90f);
  131. path.AddLine((float)((X + width) - (radius * 2f)), (float)(Y + height), (float)(X + radius), (float)(Y + height));
  132. path.AddArc(X, (Y + height) - (radius * 2f), radius * 2f, radius * 2f, 90f, 90f);
  133. path.AddLine(X, (Y + height) - (radius * 2f), X, Y + radius);
  134. path.AddArc(X, Y, radius * 2f, radius * 2f, 180f, 90f);
  135. path.CloseFigure();
  136. return path;
  137. }
  138. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/257694
推荐阅读
相关标签
  

闽ICP备14008679号