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 ...
随机推荐
- 用户输入函数--raw_input、input
1.raw_input python2.7用户输入字符串的话用raw_input.如果使用input输入字符串的话需要先把字符串放到变量中才可,但是用input输入数字的话是可以直接输入的,所以说在p ...
- AES--高级数据加密标准
AES--高级数据加密标准 对称密码体制的发展趋势将以分组密码为重点.分组密码算法通常由密钥扩展算法和加密(解密)算法两部分组成.密钥扩展算法将b字节用户主密钥扩展成r个子密钥.加密算法由一个密码学上 ...
- ln命令小陷阱
一个文件夹下面的文件结构是 -dir1 -assetsdir -subdir1 -subdir2 这个时候如果我想在subdir1和subdir2下面分别创建assetsdir的链接文件夹的话,应该怎 ...
- HDU 1754 I Hate It(线段树单点替换+区间最值)
I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- linux下解压大于4G文件提示error: Zip file too big错误的解决办法
error: Zip file too big (greater than 4294959102 bytes)错误解决办法.zip文件夹大于4GB,在centos下无法正常unzip,需要使用第三方工 ...
- mybatis一级缓存和二级缓存
1.一级缓存:session级别 执行以下操作之后一级缓存消失: 1)执行了session.clearCache(); 2)执行了CUD操作后 3)执行了session.close() 2.二级缓存: ...
- [Linux编程]__read_mostly变量含义
1.定义 __read_mostly原语将定义的变量为存放在.data.read_mostly段中,原型在include/asm/cache.h 中定义: #define __read_mostly ...
- Windows系统
1. 更改XP登录界面 怎样启用XP的经典登录界面 第一步:用管理员账号登录系统. 第二步:运行gpedit.msc启动组策略编辑器,找到"计算机配置"--"管理模板&q ...
- Repeater控件使用中的一些小问题
网页上用来展示列表的数据,发现还是Repeater比GridView,DetailView之类的要灵活些,所以近期用到了就总结下遇到的一些情况,保留下来以备之后查阅,不用现问度娘了... 自己摸索的, ...