WinForm 带下划线的TextBox

1、项目里,添加组件MoTextBox

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

namespace UFIDA.U8.Portal.Interface
{
    public partial class MoTextBox:System.Windows.Forms.TextBox  
    {  
       public MoTextBox()  
       {  
           this.Width = 180;  
           this.Top = 50;  
           this.Left = 30;  
           this.Height = 50;  
             
       }  
  
       protected override void WndProc(ref Message m)  
       {  
           base.WndProc(ref m);  
           switch (m.Msg)  
           {  
               case WM_CTLCOLOREDIT://当开始编辑TextBox控件的绘制信息  
                   goto case WM_PAINT;  
               case WM_PAINT:  
                   IntPtr hDc = GetWindowDC(this.Handle);  
                   if (hDc.ToInt32() != 0)  
                   {  
  
                       using (Graphics g = Graphics.FromHdc(hDc))  
                       {  
                           DrowButtonLines(g);  
                           g.Dispose();  
                       }  
                   }  
                   m.Result = IntPtr.Zero;  
                   ReleaseDC(m.HWnd, hDc);  
                   break;  
           }  
  
       }  
  
       public void DrowButtonLines(Graphics g)  
       {  
           Pen p = new Pen(this.BackColor, 2);  
           g.DrawRectangle(p, 1, 1, this.Width - 2, this.Height - 2);  
           p = new Pen(Color.Black, 1);  
           g.DrawLine(p,0, this.Height - 1, this.Width, this.Height - 1);  
           p.Dispose();  
       }  
  
       public const int WM_PAINT = 0x000F;//开始编辑TextBox控件的绘制信息  
       public const int WM_CTLCOLOREDIT = 0x0133;  
  
       [DllImport("user32.dll")]  
       public static extern IntPtr GetWindowDC(IntPtr hWnd);//获取整个窗口的设备场景  
  
       //释放由调用GetDC或者GetWindowDC函数获取的指定设备场景,它对类或私有设备场景无效  
       [DllImport("user32.dll")]  
       public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  
    }  
}

2、然后将这个控件拖到窗体中即可


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值