原理:

一、在控件的后台代码中, 添加布尔类型的属性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. rsync命令详解

    介绍 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部 ...

  2. 重载Python FTP_TLS 实现Implicit FTP Over TLS方式下载文件

    对于Python2.7来说,内置的FTP_TLS类并不支持Implicit FTP Over TLS加密方式的FTP Server操作,为支持Implicit FTP Over TLS加密方式,必须重 ...

  3. some code of c

    // // main.c // LineList // // Created by Rubert on 16/9/11. // Copyright © 2016年 Study. All rights ...

  4. Codeforces Round #381 (Div. 2)A. Alyona and copybooks(dfs)

    A. Alyona and copybooks Problem Description: Little girl Alyona is in a shop to buy some copybooks f ...

  5. Software Development Principle

    Every great piece of software begins with customer's big idea. As a professional softeware developer ...

  6. c# 进程间的通信实现之一简单字符串收发

       使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...

  7. C#封装好的Win32API

    Kernel.cs using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = Syst ...

  8. MySQL中tinytext、text、mediumtext和longtext详解

    一.数字类型 类型 范围 说明   Char(N) [binary] N=1~255 个字元binary :分辨大小写 固定长度 std_name cahr(32) not null VarChar( ...

  9. nyoj 881 小M的区间公约数

    点击打开链接 首先给的范围很大,是10^9.暴力解肯定超时(单用for循环到10^9都大约要2s-3s),首先写了个程序暴力的把两个数所有的约数都打印出来,最后发现所有的公约数都是最大公约数的约数,并 ...

  10. 如何将win7变为wifi热点

    以前经常使用connectify软件一键设置win7热点,但发现该软件影响开机速度,于是研究了一下win7自带的wifi功能,简单方便,分享如下: 1.打开命令提示符: [开始]/搜索框中输入“cmd ...