C# 文本框鼠标移动滑过效果

上图:




OK,

代码贴上:网上找到的例子 修改了 边框BorderStyle 加了一个All,和边框线条宽度。

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

namespace mmDesk.MainForm.Controls
{
public class LTextBox : TextBox
{
public LTextBox()
{
//设置允许透明背景色
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.LineType = BorderType.None;
}

protected override CreateParams CreateParams
{
get
{
System.Windows.Forms.CreateParams ps = base.CreateParams;
ps.ExStyle = ps.ExStyle | 0x20;
return ps;
}
}


#region 预先定义消息

[Flags]
public enum BorderType : int
{
/// <summary>
/// 不绘制
/// </summary>
None = 0,
/// <summary>
/// 左侧
/// </summary>
Left = 1,
/// <summary>
/// 上侧
/// </summary>
Top = 2,
/// <summary>
/// 右侧
/// </summary>
Right = 4,
/// <summary>
/// 底部
/// </summary>
Bottom = 8,
/// <summary>
/// 全部
/// </summary>
All=16
}

#endregion

#region 属性

BorderType _lineType;

/// <summary>
/// 要绘制的边框
/// </summary>
[Description("要绘制的边框"), DefaultValue(BorderType.None)]
public BorderType LineType
{
get
{ return _lineType; }

set { _lineType = value; this.Invalidate(); }
}


Color _lineColor;

/// <summary>
/// 绘制的边框颜色
/// </summary>
[Description("绘制的边框颜色")]
public Color LineColor
{
get
{
return _lineColor;
}
set
{
_lineColor = value;
this.Invalidate();
}
}

int _lineWidth;

/// <summary>
/// 绘制的边框宽度
/// </summary>
[Description("绘制的边框宽度")]
public int LineWidth
{
get
{
return _lineWidth;
}
set
{
_lineWidth = value;
this.Invalidate();
}
}

#endregion


[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);

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

/// <summary>
/// 重写消息处理
/// </summary>
/// <param name="m">消息</param>
protected override void WndProc(ref Message m)
{

base.WndProc(ref m);

IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) return;

if (this.BorderStyle == BorderStyle.None) return;

switch (m.Msg)
{
case 0xf:
case 0x85:
case 0x133:

Graphics g = Graphics.FromHdc(hDC);

Pen p = new Pen(LineColor);
p.Width = LineWidth;
Pen b = new Pen(BackColor);

g.DrawLine(((LineType & BorderType.Bottom) == BorderType.Bottom) ? p : b, 0, this.Size.Height - 1, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Left) == BorderType.Left) ? p : b, 0, 0, 0, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Right) == BorderType.Right) ? p : b, this.Size.Width - 1, 0, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Top) == BorderType.Top) ? p : b, 0, 0, this.Size.Width - 1, 0);
g.DrawRectangle(((LineType & BorderType.All) == BorderType.All) ? p : b, 0, 0, this.Size.Width - 1, this.Size.Height - 1);

g.Dispose();
m.Result = IntPtr.Zero;
ReleaseDC(m.HWnd, hDC);
break;
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值