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

 public partial class Text : TextBox
    {
        public Text()
        {
            this.BorderStyle = BorderStyle.FixedSingle;
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
        }
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr LoadLibrary(string lpFileName);

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams prams = base.CreateParams;
                if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
                {
                    prams.ExStyle |= 0x020; // transparent 
                    prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
                }
                return prams;
            }
        }  
    }
  public partial class TextBoxRua : Control
    {
        public TextBoxRua()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
            m_tbx = new Text();
            m_tbx.Multiline = true;
            m_tbx.BorderStyle = BorderStyle.None;
            this.BackColor = Color.Transparent;
            m_tbx.GotFocus += (s, e) => { m_bFocus = true; this.Invalidate(); };
            m_tbx.LostFocus += (s, e) => { m_bFocus = false; this.Invalidate(); };
            m_tbx.KeyDown += (s, e) => this.OnKeyDown(e);
            m_tbx.KeyPress += (s, e) => this.OnKeyPress(e);
            m_tbx.KeyUp += (s, e) => this.OnKeyUp(e);
            m_tbx.TextChanged += (s, e) => this.OnTextChanged(e);
            //m_tbx.BackColorChanged += (s, e) => m_tbx.BackColor = Color.White;
            this.Controls.Add(m_tbx);
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr LoadLibrary(string lpFileName);

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams prams = base.CreateParams;
                if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
                {
                    prams.ExStyle |= 0x020; // transparent 
                    prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
                }
                return prams;
            }
        }
        public override string Text
        {
            get { return m_tbx.Text; }
            set
            {
                if (m_tbx.Text == value) return;
                m_tbx.Text = value;
            }
        }

        public Text TextBox
        {
            get { return m_tbx; }
        }

        public override Color BackColor
        {
            get { return Color.Transparent; }
            set { }
        }

        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                if (base.ForeColor == value) return;
                base.ForeColor = value;
                m_tbx.ForeColor = value;
            }
        }

        private bool _ReadOnly;

        public bool ReadOnly
        {
            get { return m_tbx.ReadOnly; }
            set { m_tbx.ReadOnly = value; m_tbx.BackColor = Color.Transparent; }
        }

        private Text m_tbx;
        private bool m_bFocus;

        public event EventHandler TextChanged;

        protected virtual void OnTextChanged(EventArgs e)
        {
            if (this.TextChanged != null) this.TextChanged(this, e);
        }

        protected override void OnResize(EventArgs e)
        {
            m_tbx.Left = m_tbx.Top = 3;
            m_tbx.Width = this.Width - 6;
            m_tbx.Height = this.Height - 6;
            base.OnResize(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            using (SolidBrush sb = new SolidBrush(Color.Transparent))
            {
                g.FillPath(
                    sb,
                    GetRoundRectPath(new Rectangle(0, 0, this.Width - 1, this.Height - 1), 5)
                    );
            }
            using (LinearGradientBrush lgb = new LinearGradientBrush(
               new Point(0, 1), new Point(0, this.Height),
               Color.LightGray, Color.White))
            {
                g.DrawPath(
                    new Pen(lgb),
                    GetRoundRectPath(new Rectangle(0, 1, this.Width - 1, this.Height - 2), 5)
                    );
            }
            using (LinearGradientBrush lgb = new LinearGradientBrush(
               new Point(0, 0), new Point(0, this.Height - 1),
               m_bFocus ? Color.DodgerBlue : Color.Gray, Color.FromArgb(20, 0, 0, 0)))
            {
                g.DrawPath(new Pen(lgb), GetRoundRectPath(new Rectangle(0, 0, this.Width - 1, this.Height - 2), 5));
            }
            base.OnPaint(e);
        }

        /// <summary>
        /// 适合调整参数radius...
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius">radius 角度</param>
        /// <returns></returns>
        public GraphicsPath GetRoundRectPath(RectangleF rect, float radius)
        {
            return GetRoundRectPath(rect.X, rect.Y, rect.Width, rect.Height, radius);
        }

        public GraphicsPath GetRoundRectPath(float X, float Y, float width, float height, float radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddLine(X + radius, Y, (X + width) - (radius * 2f), Y);
            path.AddArc((X + width) - (radius * 2f), Y, radius * 2f, radius * 2f, 270f, 90f);
            path.AddLine((float)(X + width), (float)(Y + radius), (float)(X + width), (float)((Y + height) - (radius * 2f)));
            path.AddArc((float)((X + width) - (radius * 2f)), (float)((Y + height) - (radius * 2f)), (float)(radius * 2f), (float)(radius * 2f), 0f, 90f);
            path.AddLine((float)((X + width) - (radius * 2f)), (float)(Y + height), (float)(X + radius), (float)(Y + height));
            path.AddArc(X, (Y + height) - (radius * 2f), radius * 2f, radius * 2f, 90f, 90f);
            path.AddLine(X, (Y + height) - (radius * 2f), X, Y + radius);
            path.AddArc(X, Y, radius * 2f, radius * 2f, 180f, 90f);
            path.CloseFigure();
            return path;
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值