namespace MyTextBoxOne
{
//使用时必须把文本框的BorderStyle为FixedSingle才能使用
//一些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint方法将不起作用.
//所有这里并没有使用重载OnPaint方法绘制TextBox边框。
public class TextBoxTwo : TextBox
{
//获取的当前进程,以便重绘控件
[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); //是否启用热点效果
private bool _HotTrack = true;
//边框颜色
private Color _BorderColor = Color.Gray;
//热点边框颜色
private Color _HotColor = Color.FromArgb(, , , );
//是否鼠标经过
private bool _IsMouseOver = false;
        #region 属性
/// <summary>
/// 是否启用热点效果
/// </summary>
[Category("行为"),
Description("获得或设置一个值,指示当前鼠标经过控件时控件边框是否发生变化。只在控件的BorderStyle为FixedString时有效"),
DefaultValue(true)]
public bool HotTrack
{
get { return _HotTrack; }
set
{
this._HotTrack = value;
//该值发生变化时重绘控件,在设计模式下,更改该属性时,如果不调用该语句,则不能立即看到设计试图中该控件相应的变化
this.Invalidate();
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Category("外观"),
Description("获得或设置控件的边框颜色"),
DefaultValue(typeof(Color), "black")]
public Color BorderColor
{
get { return this._BorderColor; }
set
{
this._BorderColor = value;
this.Invalidate();
}
}
/// <summary>
/// 热点时边框颜色
/// </summary>
[Category("外观"),
Description("获得或设置当鼠标经过时控件的边框颜色。只在控件的BorderStyle为FixedSing时有效"),
DefaultValue(typeof(Color), "red")]
public Color HotColor
{
get { return this._HotColor; }
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion

---属性


        public TextBoxTwo() : base() {
this.BorderStyle = BorderStyle.FixedSingle;
this.Width = ;
this.Height = ;
} /// <summary>
/// 鼠标移动到该控件上时
/// </summary>
protected override void OnMouseMove(MouseEventArgs e)
{
this._IsMouseOver = true;
//如果启用HotTrack,则开始重绘,
//如果不加判断,则当鼠标在控件上移动时,控件边框会不断重绘,导致控件边框闪烁。下同
if (this._HotTrack)
{
this.Invalidate();
}
base.OnMouseMove(e);
}
/// <summary>
///当 鼠标从该控件 移开时
/// </summary>
protected override void OnMouseLeave(EventArgs e)
{
this._IsMouseOver = false;
if (this._HotTrack)
{
this.Invalidate();
}
base.OnMouseLeave(e);
}
/// <summary>
/// 控件获得焦点 时
/// </summary>
protected override void OnGotFocus(EventArgs e)
{
if (this._HotTrack)
{
this.Invalidate();
}
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
if (this._HotTrack)
{
this.Invalidate();
}
base.OnLostFocus(e);
}
/// <summary>
/// 获得操作系统消息
/// </summary>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
//拦截系统消息,获得当前控件进程以便重绘。
//
//MSDN:重写 OnPaint 将禁止修改所有控件的外观
//那些由 Windows 完成所有绘图的控件(例如 TextBox)从不调用他的OnPaint方法 //因此将永远不会使用自定义码。请参见你要修改的特定控件的文档,
//查看 OnPaint 方法是否可用。如果某个控件未将 OnPaint 作为成员方法列出,
//则你无法通过重写此方法改变外观。 //MSDN:要了解可用的Message.Msg,Message.LParam 和 Message.WParam 值,
//请参考位于 MSND 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为 2个像素
Pen p = new Pen(this._BorderColor, );
if (this.HotTrack)
{
if (this.Focused)
{
p.Color = this._HotColor;
}
}
else
{
if (this._IsMouseOver)
{
p.Color = this._HotColor;
}
else
{
p.Color = this._BorderColor;
}
}
//绘制边框
Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(p, , , this.Width - , this.Height - );
p.Dispose();
}
//返回结果
m.Result = IntPtr.Zero;
//释放
ReleaseDC(m.HWnd, hDC);
} }
}
}

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

  1. Winform改变Textbox边框颜色

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  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. ASP.NET MVC全局观

    全局      首先我们来看一副图片,由于这幅图是我自己画的,不是摘自微软官方,所以如果有什么不到位的地方还望海涵! 首先,用户通过Web浏览器向服务器发送一条url请求,这里请求的url不再是xxx ...

  2. C#结构函数与base关键字

    //声明父类 class ProductsFather { public double Price { get; set; } public int Count { get; set; } publi ...

  3. Dom4j之xPath

    XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...

  4. ST表入门学习poj3264 hdu5443 hdu5289 codeforces round #361 div2D

    ST算法介绍:[转自http://blog.csdn.net/insistgogo/article/details/9929103] 作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 方 ...

  5. 网络流初步——增广路算法(EK)模板

    #include <iostream> #include <queue> #include<string.h> using namespace std; #defi ...

  6. LLVM对注释的新增支持 @ WWDC 2013

    很久之前我就在想:“我应该按照什么格式写注释,才能像Apple官方API那样按住Option键并点击函数名可以跳出文档说明”,如下图: 我理所当然地认为这个功能应该是根据现有注释的格式来进行排版的,于 ...

  7. MySQL 5.6 root密码丢失,使用mysqld --skip-grant-tables

    MySQL 5.6 root密码丢失,(window平台)使用mysqld –skip-grant-tables启动MySQL服务,出现警告: 1 [Warning] TIMESTAMP with i ...

  8. Python新手学习基础之函数-全局变量和局部变量

    全局变量和局部变量 我们通常把定义在函数外的变量成为全局变量,定义在函数内的变量称为局部变量,顾名思义,全局变量的作用域是整个代码段,局部变量的作用域就只是在它所属的代码段,变量只在它的作用域里有效. ...

  9. cf437C The Child and Toy

    C. The Child and Toy time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. 【HDU1712】ACboy needs your help(分组背包)

    将背包九讲往后看了看,学习了一下分组背包.来做几道入门题,试试手. #include <iostream> #include <cstring> #include <cs ...