WinForm程序中的类TextBox的自定义控件, 添加失去焦点的功能
原理:
一、在控件的后台代码中, 添加布尔类型的属性CanFocus
二、在控件的构造函数中, 注册Enter事件的处理方法. 并在处理方法中,根据CanFocus属性的值来决定是否可以丢失焦点, 如果可以则调用Windows消息的发送类.
三、在处理方法中,调用User32.dll类库, 发送window消息.
示例代码:
//Windows消息的发送方法
//WMMessageHepler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Runtime.InteropServices;
using System.Windows.Forms; namespace YXT.Common
{
public class WMMessageHepler
{
private const int WM_KILLFOCUS = 0x0008; //发送失去焦点的Window消息
public static void SendBlurMsg(IntPtr hWnd)
{
PostMessage(hWnd, WM_KILLFOCUS, , );
} [DllImport("user32.dll")]
private static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam); [DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
}
}
//自定义控件的关键代码
//WatermarkTextBox.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YXT.Common; namespace YXT.Client.Controls
{
[ToolboxBitmap(typeof(TextBox))]
public class WatermarkTextBox : TextBox
{ public WatermarkTextBox()
{
this.BorderStyle = BorderStyle.FixedSingle;
this.Enter += new EventHandler(WatermarkTextBox_Enter);
} void WatermarkTextBox_Enter(object sender, EventArgs e)
{
if (this.SetBlur)
{
this.OnBlur();
}
} private Color _borderColor = Color.Red;
private string _watermarkTitle;
private Color _watermarkColor = Color.DarkGray;
private bool _setBlur = false; /// <summary>
/// 控件是否失去焦点
/// </summary>
public bool SetBlur
{
get { return this._setBlur; }
set
{
this._setBlur = value;
Invalidate();
}
} /// <summary>
/// 水印字体提示
/// </summary>
public string WatermarkTitle
{
get { return _watermarkTitle; }
set
{
_watermarkTitle = value;
Invalidate();
}
} /// <summary>
/// 边框颜色
/// </summary>
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
Invalidate();
}
} /// <summary>
/// 水印颜色
/// </summary>
public Color WatermarkColor
{
get { return _watermarkColor; }
set
{
_watermarkColor = value;
Invalidate();
}
} private void OnBlur()
{
WMMessageHepler.SendBlurMsg(this.Handle);
} protected override void WndProc(ref Message m)
{ base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85)
{
if (this.BorderStyle == BorderStyle.FixedSingle)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
using (var pen = new Pen(_borderColor))
{
g.DrawRectangle(pen, , , this.Width - , this.Height - );
WmPaint(ref m);
}
}
}
}
} private void WmPaint(ref Message m)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (Text.Length ==
&& !string.IsNullOrEmpty(_watermarkTitle)
&& !Focused)
{
TextFormatFlags format =
TextFormatFlags.EndEllipsis |
TextFormatFlags.VerticalCenter; if (RightToLeft == RightToLeft.Yes)
{
format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
} TextRenderer.DrawText(
graphics,
_watermarkTitle,
Font,
base.ClientRectangle,
_watermarkColor,
format);
}
}
}
}
}
//使用示例
//方法使用示例
ControlsEdintor(string controlsName)
{
//控件编辑时 修改样式
foreach (var value in this.Controls)
{
var textBox = value as WatermarkTextBox;
if (controlsName == "编辑")
{
if (textBox != null)
{
textBox.BorderStyle = BorderStyle.FixedSingle;
textBox.ReadOnly = false;
textBox.SetBlur = false;
}
}
else
{
if (textBox != null)
{
textBox.BorderStyle = BorderStyle.None;
textBox.ReadOnly = true;
textBox.SetBlur = true;
}
}
}
}
WinForm程序中的类TextBox的自定义控件, 添加失去焦点的功能的更多相关文章
- 在Winform程序中使用Spire.Pdf实现页面添加印章处理
在一些场合,我们往往需要使用印章来给每页文档加盖一个印章,以表示该文档经过某个部门的认证的,常规的做法就是打印文档后盖章,如果需要电子档再行扫描一下.这样的的处理,如果文档很多,且仅仅需要电子文档的就 ...
- winform程序中为无边框窗体手动添加窗体拖动代码
Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置 Point oldFormPoint;//记录开始移动窗口前窗体位置 // ...
- 在Winform程序中设置管理员权限及为用户组添加写入权限
在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行程序具有管理员权限或者设置运行程序的目录具有写入的权限,如果是在操作系统里面,我们可以设置运行程序以管理员身 ...
- (转)在Winform程序中设置管理员权限及为用户组添加写入权限
本文转载自:http://www.cnblogs.com/wuhuacong/p/5645172.html 在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行 ...
- WinForm程序中两份mdf文件问题的解决
在项目中用程序中嵌入mdf文件的方式来进行SQLServer数据库开发非常方便,用来发布开源项目等很方便,点击就可以运行,免部署,特别是在教学中用起来更加方便,老师不用先将数据库文件detach再发给 ...
- 在C#中winform程序中应用nlog日志工具
在C#中winform程序中应用nlog日志工具,配置文件简单应用. 文件名 nlog.config,请注意修改属性为"始终复制",发布时候容易遇到不存在文件的错误提示. 通过Nu ...
- Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)
Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题) 2016-12-04 20:11 362人阅读 评论(4) 收藏 举报 分类: Halco ...
- C#中Winform程序中如何实现多维表头【不通过第三方报表程序】
问题:C#中Winform程序中如何实现多维表头. 在网上搜了很多方法,大多数方法对于我这种新手,看的都不是很懂.最后在新浪博客看到了一篇比较易懂的文章:[DataGridView二维表头与合并单元格 ...
- .NET混合开发解决方案7 WinForm程序中通过NuGet管理器引用集成WebView2控件
系列目录 [已更新最新开发文章,点击查看详细] WebView2组件支持在WinForm.WPF.WinUI3.Win32应用程序中集成加载Web网页功能应用.本篇主要介绍如何在WinForm ...
随机推荐
- 【MySQL】drop大表
利用硬链接和truncate降低drop table对线上环境的影响 众所周知drop table会严重的消耗服务器IO性能,如果被drop的table容量较大,甚至会影响到线上的正常. 首先,我们看 ...
- js产生随机数函数
函数: //产生随机数函数 function RndNum(n){ var rnd=""; for(var i=0;i<n;i++) rnd+=Math.floor(Math ...
- Struts2基础使用教程:OGNL
取自<JAVAWEB整合开发王者归来> 是一种类似EL的语言,比EL强大的多 能访问对象的方法,例如list.size() 能访问静态属性与静态方法,方法是在类名前.方法前加上@.如@ja ...
- Error 2103 “Unhandled Error in Silverlight Application“ 解决办法
当调试SilverLight项目时,如果出现如下错误: 当调试页面时出现如下错误: 解决办法为:打开工程属性,在Startup object:处选择相应的启动应用程序.
- super语句
1.super是对当前对象的父类对象的默认引用, 2.super必须出现在子类中(方法或构造函数),而不是其他位置 3.super无法访问private成员属性和方法
- C++设计模式-Decorator装饰模式
Decorator装饰模式作用:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. UML图如下: Component是定义一个对象接口,可以给这些对象动态地添加职责. ...
- 基于JS功能强大的日期插件Kalendae
开发中需要一个日期插件,可以在zepto下使用,可以选择日期段,可以设置不可选日期 找到一个完全满足的,并且基于JS不依赖于任何库. 在线演示:http://chipersoft.com/Kalend ...
- DataGridView导出到Excel的三个方法
#region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...
- Mysql常见四种索引的使用
提到MySQL优化,索引优化是必不可少的.其中一种优化方式 --索引优化,添加合适的索引能够让项目的并发能力和抗压能力得到明显的提升. 我们知道项目性能的瓶颈主要是在"查(select)&q ...
- 类的高级:访问修饰符、封装、静态类成员static、内部类;
访问修饰符: 公开访问(public):对所有子类,非子类访问: 受保护的(protected):只有同包子类.非子类.不同包子类可访问,不同包非子类不可访问: 私有的(private):只有本类可访 ...