C#-屏蔽文本框的粘贴功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShieldPaste
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }


        private void Frm_Main_Load(object sender, EventArgs e)
        {
            TextBoxx tb = new TextBoxx();//创建文本框对象
            tb.Width = Width;//设置文本框宽度
            tb.Height = Height;//设置文本框高度
            tb.Location = new Point(0, 0);//设置文本框起始位置
            tb.Multiline = true;//设置文本框为多行
            Controls.Add(tb);//将文本框添加到控件集合
        }
    }
    class TextBoxx : TextBox
    {
        public const int WM_PASTE = 0x0302;//粘贴消息信息
        protected override void WndProc(ref Message m)//重写处理消息方法
        {
            if (m.Msg != WM_PASTE)//屏蔽粘贴消息信息
            {
                base.WndProc(ref m);//调用基类消息处理方法
            }
        }
    }
}
 

在WPF中,我们可以使用控件的属性来实现文本框和密码框的水印效果(也称为背景文字提示)。下面是实现该效果的一些方法: 方法一:使用控件的Placeholder属性 WPF的文本框和密码框控件都有一个名为Placeholder的属性,该属性可以用来显示文本框或密码框中的默认提示文本。当用户在文本框或密码框中输入内容后,该提示文本就会自动隐藏。 例如,以下代码演示了在WPF中如何使用Placeholder属性来实现文本框的水印效果: ```xml <TextBox Placeholder="请输入文本"></TextBox> ``` 同样地,我们可以在密码框中使用Placeholder属性来实现密码框的水印效果: ```xml <PasswordBox Placeholder="请输入密码"></PasswordBox> ``` 方法二:使用控件的Adorner WPF中的Adorner是一种用于在控件上添加装饰元素的机制。我们可以使用Adorner来实现文本框和密码框的水印效果。 以下是实现该效果的一般步骤: 1.定义一个自定义控件,例如WatermarkTextBox。 2.在WatermarkTextBox中定义一个名为Watermark的依赖属性。该属性表示水印提示文本。 3.重写WatermarkTextBox的OnRender方法,使其在控件上绘制水印提示文本。 4.在WatermarkTextBox的TextChanged事件中判断文本框中是否有内容,如果有则隐藏水印提示文本,否则显示水印提示文本。 以下是WatermarkTextBox的示例代码: ```csharp public class WatermarkTextBox : TextBox { public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register( "Watermark", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(default(string))); public string Watermark { get { return (string)GetValue(WatermarkProperty); } set { SetValue(WatermarkProperty, value); } } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); if (string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Watermark)) { var formattedText = new FormattedText( Watermark, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Foreground); drawingContext.DrawText(formattedText, new Point(2, 2)); } } protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); if (string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Watermark)) { InvalidateVisual(); } } } ``` 使用WatermarkTextBox时,只需要在XAML中设置Watermark属性即可: ```xml <local:WatermarkTextBox Watermark="请输入文本"></local:WatermarkTextBox> ``` 同样地,我们也可以定义一个WatermarkPasswordBox类来实现密码框的水印效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值