C# WinForm扩展控件TextBox底部下划线展示

在winform项目中,新建一个类 TextBoxLine ,然后编译项目,打开工具箱就可以看到控件了

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace mesScan.SystemControl
{
    [ToolboxItem(true)]
    public partial class TextBoxLine : TextBox
    {
        public TextBoxLine()
        {
           // InitializeComponent();

            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.BorderStyle = BorderStyle.None;
        }

        public TextBoxLine(IContainer container)
        {
            container.Add(this);

          //  InitializeComponent();

            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.BorderStyle = BorderStyle.None;
        }

        private bool m_DrawLine = false;
        public bool DrawLine
        {
            get
            {
                return this.m_DrawLine;
            }
            set
            {
                this.m_DrawLine = value;
                this.Invalidate();
            }
        }
        private Color _linecolor = Color.Black;
        /// <summary>
        /// 线条颜色
        /// </summary>
        public Color LineColor
        {
            get
            {
                return this._linecolor;
            }
            set
            {
                this._linecolor = value;
                this.Invalidate();
            }
        }

        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                if (this.DrawLine)
                {
                    IntPtr hDC = GetWindowDC(m.HWnd);
                    if (hDC.ToInt32() == 0)
                    {
                        return;
                    }
                    Graphics g = Graphics.FromHdc(hDC);
                    Pen p = new Pen(this._linecolor, 3.5f);
                    Point p1 = new Point(0, this.Height - 1);
                    Point p2 = new Point(this.Width, Height - 1);
                    g.DrawLine(p, p1, p2);
                    m.Result = IntPtr.Zero;
                    ReleaseDC(m.HWnd, hDC);
                }
            }
        }
    }
}

 使用 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值