using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace TextDemo
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} /// <summary>
/// 添加一个继承自textbox的新类,重写WndProc方法,在重写的方法里重绘边框就可以了
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
borderDrawer borderDrawer1 = new borderDrawer();
borderDrawer1.DrawBorder(ref m, this.Width, this.Height);
}
} #region 第一种方法,使用时必须把文本框的BorderStyle为FixedSingle才能使用
[ToolboxItem(true)]
public class TextBoxXP : System.Windows.Forms.TextBox
{
/// <summary>
/// 获得当前进程,以便重绘控件
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); /// <summary>
/// 是否启用热点效果
/// </summary>
private bool _HotTrack = true; /// <summary>
/// 边框颜色
/// </summary>
private Color _BorderColor = Color.Black; /// <summary>
/// 热点边框颜色
/// </summary>
private Color _HotColor = Color.FromArgb(0x33, 0x5E, 0xA8); /// <summary>
/// 是否鼠标MouseOver状态
/// </summary>
private bool _IsMouseOver = false; #region 属性
/// <summary>
/// 是否启用热点效果
/// </summary>
[Category("行为"),
Description("获得或设置一个值,指示当鼠标经过控件时控件边框是否发生变化。只在控件的BorderStyle为FixedSingle时有效"),
DefaultValue(true)]
public bool HotTrack
{
get
{
return this._HotTrack;
}
set
{
this._HotTrack = value;
//在该值发生变化时重绘控件,下同
//在设计模式下,更改该属性时,如果不调用该语句,
//则不能立即看到设计试图中该控件相应的变化
this.Invalidate();
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Category("外观"),
Description("获得或设置控件的边框颜色"),
DefaultValue(typeof(Color), "#A7A6AA")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
/// <summary>
/// 热点时边框颜色
/// </summary>
[Category("外观"),
Description("获得或设置当鼠标经过控件时控件的边框颜色。只在控件的BorderStyle为FixedSingle时有效"),
DefaultValue(typeof(Color), "#335EA8")]
public Color HotColor
{
get
{
return this._HotColor;
}
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion 属性 /// <summary>
///
/// </summary>
public TextBoxXP()
: base()
{
} /// <summary>
/// 鼠标移动到该控件上时
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
//鼠标状态
this._IsMouseOver = true;
//如果启用HotTrack,则开始重绘
//如果不加判断这里不加判断,则当不启用HotTrack,
//鼠标在控件上移动时,控件边框会不断重绘,
//导致控件边框闪烁。下同
//谁有更好的办法?Please tell me , Thanks。
if (this._HotTrack)
{
//重绘
this.Invalidate();
}
base.OnMouseMove(e);
}
/// <summary>
/// 当鼠标从该控件移开时
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
this._IsMouseOver = false; if (this._HotTrack)
{
//重绘
this.Invalidate();
}
base.OnMouseLeave(e);
} /// <summary>
/// 当该控件获得焦点时
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(EventArgs e)
{ if (this._HotTrack)
{
//重绘
this.Invalidate();
}
base.OnGotFocus(e);
}
/// <summary>
/// 当该控件失去焦点时
/// </summary>
/// <param name="e"></param>
protected override void OnLostFocus(EventArgs e)
{
if (this._HotTrack)
{
//重绘
this.Invalidate();
}
base.OnLostFocus(e);
} /// <summary>
/// 获得操作系统消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{ base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
//拦截系统消息,获得当前控件进程以便重绘。
//一些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint方法将不起作用.
//所有这里并没有使用重载OnPaint方法绘制TextBox边框。
//
//MSDN:重写 OnPaint 将禁止修改所有控件的外观。
//那些由 Windows 完成其所有绘图的控件(例如 Textbox)从不调用它们的 OnPaint 方法,
//因此将永远不会使用自定义代码。请参见您要修改的特定控件的文档,
//查看 OnPaint 方法是否可用。如果某个控件未将 OnPaint 作为成员方法列出,
//则您无法通过重写此方法改变其外观。
//
//MSDN:要了解可用的 Message.Msg、Message.LParam 和 Message.WParam 值,
//请参考位于 MSDN Library 中的 Platform SDK 文档参考。可在 Platform SDK(“Core SDK”一节)
//下载中包含的 windows.h 头文件中找到实际常数值,该文件也可在 MSDN 上找到。
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == )
{
return;
} //只有在边框样式为FixedSingle时自定义边框样式才有效
if (this.BorderStyle == BorderStyle.FixedSingle)
{
//边框Width为1个像素
System.Drawing.Pen pen = new Pen(this._BorderColor, ); ; if (this._HotTrack)
{
if (this.Focused)
{
pen.Color = this._HotColor;
}
else
{
if (this._IsMouseOver)
{
pen.Color = this._HotColor;
}
else
{
pen.Color = this._BorderColor;
}
}
}
//绘制边框
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(pen, , , this.Width - , this.Height - );
pen.Dispose();
}
//返回结果
m.Result = IntPtr.Zero;
//释放
ReleaseDC(m.HWnd, hDC);
}
}
}
#endregion /// <summary>
/// 第二种方法
/// </summary>
class borderDrawer : System.Windows.Forms.TextBox
{
private Color borderColor = Color.Red; // 设置默认的边框颜色
private static int WM_NCPAINT = 0x0085; // WM_NCPAINT message
private static int WM_ERASEBKGND = 0x0014; // WM_ERASEBKGND message
private static int WM_PAINT = 0x000F; // WM_PAINT message
[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
//释放DC
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
/// <summary>
/// 重绘边框的方法
/// </summary>
/// <param name="message"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public void DrawBorder(ref Message message, int width, int height)
{
if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND ||
message.Msg == WM_PAINT)
{
IntPtr hdc = GetDCEx(message.HWnd, (IntPtr), | 0x0020); if (hdc != IntPtr.Zero)
{
Graphics graphics = Graphics.FromHdc(hdc);
Rectangle rectangle = new Rectangle(, , width, height);
ControlPaint.DrawBorder(graphics, rectangle,
borderColor, ButtonBorderStyle.Solid); message.Result = (IntPtr);
ReleaseDC(message.HWnd, hdc);
}
}
}
}
}

Winform改变Textbox边框颜色的更多相关文章

  1. Winform改变Textbox边框颜色(转)

    namespace MyTextBoxOne { //使用时必须把文本框的BorderStyle为FixedSingle才能使用 //一些控件(如TextBox.Button等)是由系统进程绘制,重载 ...

  2. 改变edittext边框颜色

    转载自:点击打开链接 第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditTex ...

  3. C# WinForm修改Panel边框颜色

    private void panel1_Paint(object sender, PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics, thi ...

  4. winform设置button的边框颜色,或取消边框颜色,不显示边框

    // winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现. 一.设置按钮本身属性 buttonBubufx.FlatStyle = Fl ...

  5. C#在Winform中改变Textbox高度三种方法

    最近在做C# Winform项目,需要有一个能动态调整大小的Textbox,并且要是单行的.试了几次,单行模式的Textbox不能直接改高度.于是搜索了一下,整理出几个改变高度的方法. 1.将Text ...

  6. C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法

    C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 1.新建组件这里可以自定义一个Panel控件起名为PanelEx 2.增加一个BoderColor属性和BoderSize属性 pr ...

  7. C# WinForm窗体控件GroupBox修改边框颜色控件

    C# WinForm窗体控件GroupBox修改边框颜色控件 1.新建组件这里可以自定义一个GroupBox控件起名为GroupBoxEx 2.增加一个BoderColor属性 private Col ...

  8. 整理悬浮在列表中a元素时改变a元素上下边框颜色的问题。

    整理一下当悬浮在a元素上时a的上下边颜色改变,并且里面的内容不会移动,下面是PSD图效果区域: 刚开始我先给A元素加了上下边框和颜色,利用a:hover改变a元素上下的边框颜色,但是第一个a元素的下边 ...

  9. Winform拖拽改变无边框窗体大小

    大家在进行Winform开发过程中,很容易就可以完成一个窗口的布局工作,但现在的软件界面美化效果一个比一个好,很多软件都是无边框的,于是乎,你是不是也感叹:winform的带边框的窗体如此丑陋,我一定 ...

随机推荐

  1. ServiceFabric极简文档-1.3删除群集

    删除群集 若要删除群集,请运行包文件夹中的 RemoveServiceFabricCluster.ps1 Powershell 脚本,并传入 JSON 配置文件的路径. 可以选择性地指定删除日志的位置 ...

  2. 第三章.定制专属的kali

    1.更新升级 • apt-get update • apt-get upgrade • apt-get dis-upgrade   2.根据个人喜好需求安装软件包 • 库 • Apt-get命令 • ...

  3. JS高级程序设计第4章--精简版

    前言:纯手打!!!按照自己思路重写!!!这次是二刷了,想暑假做一次完整的笔记,但用本子来写笔记的话太贵了,可能哪天还丢了..所以还是博客好== 第四章:变量.作用域和内存问题 4.1 基本类型和引用类 ...

  4. 安卓开发之详解getChildFragmentManager和getsupportFragmentManager和getFragmentManager详解

    安卓开发之详解getChildFragmentManager和getsupportFragmentManager和getFragmentManager详解 getFragmentManager()所得 ...

  5. 机器学习-特征值,svd分解

    求矩阵的秩 设 ,已知r(A)=2,则参数x,y分别是 解:任意三阶子式=0,有二阶子式≠0,但是这些子式比较多,可以使用初等变换,因为初等变换不改变矩阵的秩,可以将矩阵通过初等行(列)变换,化为行阶 ...

  6. Uncaught ReferenceError: xxx is not defined 解决办法

    VM11371:1 Uncaught ReferenceError: inputSize is not defined    at <anonymous>:1:1 解决办法(根据需求调整) ...

  7. Java面试题 从源码角度分析HashSet实现原理?

    面试官:请问HashSet有哪些特点? 应聘者:HashSet实现自set接口,set集合中元素无序且不能重复: 面试官:那么HashSet 如何保证元素不重复? 应聘者:因为HashSet底层是基于 ...

  8. C#写好的类库dll在别人调用的时候也能看到注释的方法

    1.用///的方法添加注释 2.项目的属性里面,要选上"生成XML注释文档" 菜单 Project -> 'xxxx' Properties -> Build -> ...

  9. eclipse(java windows)

    百度云:链接:http://pan.baidu.com/s/1i4Zjv97    密码:u0qh 官方下载网址:http://www.eclipse.org/downloads/eclipse-pa ...

  10. D3学习之画布制作

    最近大半个月都和d3斗争,学习艰辛(呜呜……)如果觉得作者写的对你有用,可以打赏作者哦!owo 起言:结合自己的学习之路,我认为要想使用d3画图搞清楚布局很重要,层次分明,就给了你很大的灵活性,写起代 ...