钩住默认的“粘贴"WinForms TextBox 控件的事件 [英] hook on default "Paste" event of WinForms TextBox control

查看:28
本文介绍了钩住默认的“粘贴"WinForms TextBox 控件的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改"所有粘贴到 TextBox 的文本,以某种结构化的方式显示.我可以通过拖放、ctrl-v 来完成,但如何使用默认上下文菜单粘贴"来完成?

解决方案

虽然我通常不建议使用低级 Windows API,而且这可能不是唯一的方法,但它确实可以解决问题:

>

使用系统;使用 System.Windows.Forms;公共类 ClipboardEventArgs : EventArgs{公共字符串 ClipboardText { 获取;放;}公共剪贴板事件参数(字符串剪贴板文本){剪贴板文本 = 剪贴板文本;}}类 MyTextBox : 文本框{公共事件 EventHandler粘贴;私有常量 int WM_PASTE = 0x0302;protected override void WndProc(ref Message m){if (m.Msg == WM_PASTE){var evt = 粘贴;如果(evt != null){evt(this, new ClipboardEventArgs(Clipboard.GetText()));//不要让基础控件再次处理该事件返回;}}base.WndProc(ref m);}}静态类程序{///<总结>///应用程序的主要入口点.///</总结>[STAThread]静态无效主(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);var tb = new MyTextBox();tb.Pasted += (sender, args) =>MessageBox.Show("粘贴:" + args.ClipboardText);var form = new Form();form.Controls.Add(tb);Application.Run(form);}}

归根结底,WinForms 工具包不是很好.它是一个围绕 Win32 和通用控件的精简包装器.它公开了 80% 最有用的 API.其他 20% 经常丢失或没有以明显的方式暴露.如果可能的话,我建议从 WinForms 转向 WPF,因为 WPF 似乎是一个更好的 .NET GUI 架构框架.

I need to "modify" all pasted into TextBox text to be shown in some structured way. I can do it with drag-n-drop, ctrl-v, but how to do it with default context's menu "Paste"?

解决方案

While I would normally not suggest dropping to low level Windows API, and this may not be the only way of doing this, it does do the trick:

using System;
using System.Windows.Forms;

public class ClipboardEventArgs : EventArgs
{
    public string ClipboardText { get; set; }
    public ClipboardEventArgs(string clipboardText)
    {
        ClipboardText = clipboardText;
    }
}

class MyTextBox : TextBox
{
    public event EventHandler<ClipboardEventArgs> Pasted;

    private const int WM_PASTE = 0x0302;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE)
        {
            var evt = Pasted;
            if (evt != null)
            {
                evt(this, new ClipboardEventArgs(Clipboard.GetText()));
                // don't let the base control handle the event again
                return;
            }
        }

        base.WndProc(ref m);
    }
}

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var tb = new MyTextBox();
        tb.Pasted += (sender, args) => MessageBox.Show("Pasted: " + args.ClipboardText);

        var form = new Form();
        form.Controls.Add(tb);

        Application.Run(form);
    }
}

Ultimately the WinForms toolkit is not very good. It is a thin-ish wrapper around Win32 and the Common Controls. It exposes the 80% of the API that is most useful. The other 20% is often missing or not exposed in a way that is obvious. I would suggest moving away from WinForms and to WPF if possible as WPF seems to be a better architected framework for .NET GUIs.

这篇关于钩住默认的“粘贴"WinForms TextBox 控件的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆