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 ...
随机推荐
- 动态作用域与this +apply和call +bind
词法作用域是一套关于引擎如何寻找变量以及会在何处找到变量的规则. (函数作用域和块作用域) JavaScript 中的作用域就是词法作用域,也就是静态作用域,由定义代码决定 动态作用域似乎暗示有很好的 ...
- 利用微软AntiXss Library过滤输出字符,防止XSS攻击
假如项目在前期没有过滤客户提交的字符,那么可以在输出的时候,对输出的字符进行过滤,防止出现XSS跨域攻击. 原理简单:利用ASP.NET API的管道原理,在MessageHandlers中添加一个自 ...
- CentOS7:Puppet推送Zabbix Agent
创建zabbix模块目录: $ mkdir -p /etc/puppet/modules/zabbix/{manifests,templates} 创建init.pp清单: $ cat /etc/pu ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- git上传到阿里云code
一.在阿里云code(kelude)注册用户,新建项目.二.安装Git后使用GitBash生产ssh key:(https://zhidao.baidu.com/question/1303468264 ...
- Access数据库的模糊查询到底是用*还是%
今天被用了一下Access数据库,结果被它的模糊查询给折腾了一上午,到底是用*还是%?特此记下来 事情是这样的,我用C#写了个小的窗体程序,访问Access数据库进行一个模糊查询,我先手工往Acces ...
- java 将字符串下载为文本文件
通过url访问方法即可进行下载 @RequestMapping("down") public String down(HttpServletRequest request,Http ...
- 实时监听输入框值变化:oninput & onpropertychange
结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. oninput 是 HTML5 的标准事件,对于检测 textarea, i ...
- Mbps、Kbps、bps、MB、KB
1:运营商带宽衡量单位: Mbps:百万位每秒 kbps:千位每秒 bps:位每秒 b:bit,比特 1Mbps=1000kbps=1,000,000bps 这些单位通常用来表示每秒传输的二进制位 2 ...
- C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC16 Boot ...