原理:

一、在控件的后台代码中, 添加布尔类型的属性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的自定义控件, 添加失去焦点的功能的更多相关文章

  1. 在Winform程序中使用Spire.Pdf实现页面添加印章处理

    在一些场合,我们往往需要使用印章来给每页文档加盖一个印章,以表示该文档经过某个部门的认证的,常规的做法就是打印文档后盖章,如果需要电子档再行扫描一下.这样的的处理,如果文档很多,且仅仅需要电子文档的就 ...

  2. winform程序中为无边框窗体手动添加窗体拖动代码

            Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置        Point oldFormPoint;//记录开始移动窗口前窗体位置        // ...

  3. 在Winform程序中设置管理员权限及为用户组添加写入权限

    在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行程序具有管理员权限或者设置运行程序的目录具有写入的权限,如果是在操作系统里面,我们可以设置运行程序以管理员身 ...

  4. (转)在Winform程序中设置管理员权限及为用户组添加写入权限

    本文转载自:http://www.cnblogs.com/wuhuacong/p/5645172.html 在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行 ...

  5. WinForm程序中两份mdf文件问题的解决

    在项目中用程序中嵌入mdf文件的方式来进行SQLServer数据库开发非常方便,用来发布开源项目等很方便,点击就可以运行,免部署,特别是在教学中用起来更加方便,老师不用先将数据库文件detach再发给 ...

  6. 在C#中winform程序中应用nlog日志工具

    在C#中winform程序中应用nlog日志工具,配置文件简单应用. 文件名 nlog.config,请注意修改属性为"始终复制",发布时候容易遇到不存在文件的错误提示. 通过Nu ...

  7. Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)

     Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题) 2016-12-04 20:11 362人阅读 评论(4) 收藏 举报  分类: Halco ...

  8. C#中Winform程序中如何实现多维表头【不通过第三方报表程序】

    问题:C#中Winform程序中如何实现多维表头. 在网上搜了很多方法,大多数方法对于我这种新手,看的都不是很懂.最后在新浪博客看到了一篇比较易懂的文章:[DataGridView二维表头与合并单元格 ...

  9. .NET混合开发解决方案7 WinForm程序中通过NuGet管理器引用集成WebView2控件

    系列目录     [已更新最新开发文章,点击查看详细] WebView2组件支持在WinForm.WPF.WinUI3.Win32应用程序中集成加载Web网页功能应用.本篇主要介绍如何在WinForm ...

随机推荐

  1. xml保存基本信息

    public static string getXML(string nodeName) { string strReturn = ""; try { string fileNam ...

  2. 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure

    相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...

  3. Linux文件权限

    Permission deny 权限 拒绝   查看权限 ls -a ls -la expression 查看文件夹里边东西的权限   用户群的分类 组群:一个操作系统可能几个人同时用 方便小组的文件 ...

  4. iOS开发 - OC - duplicate symbol _OBJC / undefind symbol 错误的相关处理

    前言: 作为一个iOS开发,相信大家都会遇到类似于 “duplicate symbol” 的程序报错. 对于很多新手来说,可能会有点手足无措,因为这种类型的报错一般并非是代码的逻辑错误,大部分情况下是 ...

  5. Window 端口查询

    1. Window环境下查询端口使用情况 方法1: 使用netstat [-参数]|findstr [端口号]来直接查询某个端口的具体使用情况 示例: netstat -ano|findstr &qu ...

  6. Java面向对象㈡ -- 继承与多态

    Java的继承是通过extends和implement来实现的,Java不支持多继承,但是Java支持多层继承以及多实现(接口).Java继承有一个关键字super是用来指向父类.Java继承衍生出覆 ...

  7. UEFI+GPT引导实践篇(一):切换到UEFI启动,准备安装介质

    如果只单纯比较UEFI引导和BIOS引导,那么毫无疑问UEFI引导要简单很多.不过现在的主板大都是同时兼容BIOS和UEFI引导方式,所以在实际操作前还需要确认一些东西.详见下文. 1.我的电脑支不支 ...

  8. JVM & Server & Connector & Context Relationship

  9. 转:中间人攻击利用框架bettercap测试

    0x00前言 上篇提到内网渗透很有趣,这次就从一款新工具说起: bettercap 0x01简介 bettercap可用来实现各种中间人攻击,模块化,便携.易扩展 0x02特点 提到中间人攻击,最知名 ...

  10. js back动作

    history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1):也是返回当前页的上一页,不过表单里的数据全部还在 history.back(0) 刷新 ...