gpt4 book ai didi

c# - 文本框 : Disable the 'Paste' option whilst allowing 'Cut' and 'Copy' on Right-click

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:12 25 4
gpt4 key购买 nike

我在 C# WinForms 中有一个 TextBox 组件。

当我右键单击它时,我想禁用粘贴选项,同时仍允许用户剪切和复制。

我研究了几个选项,但都没有完全满足我的要求 -

  1. 如果我使用此选项,它将阻止剪切和复制以及我不想要的粘贴。

    txt.ShortcutsEnabled = false;

  2. 如果我覆盖 TextBoxContextMenu,我将不得不在新的上下文菜单中自己编写剪切和复制功能。

    txt.ContextMenu = new ContextMenu();//或其他一些

是否有任何选项可以用来仅禁用默认上下文菜单的粘贴选项,同时保留剪切和复制?

最佳答案

假设粘贴菜单项始终是文本框上下文菜单中的第五个元素(从零开始,分隔符也算作项目),您可以子类化 TextBox 类(此处: CustomMenuTextBox) 并覆盖 WndProc 方法以禁用该特定菜单项:

public static class User32
{
[DllImport("user32.dll")]
public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
}

public class CustomMenuTextBox : TextBox
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0093 /*WM_UAHINITMENU*/ || m.Msg == 0x0117 /*WM_INITMENUPOPUP*/ || m.Msg == 0x0116 /*WM_INITMENU*/)
{
IntPtr menuHandle = m.Msg == 0x0093 ? Marshal.ReadIntPtr(m.LParam) : m.WParam;

// MF_BYPOSITION and MF_GRAYED
User32.EnableMenuItem(menuHandle, 4, 0x00000400 | 0x00000001);
}

base.WndProc(ref m);
}
}

基于 Add item to the default TextBox context menu .

关于c# - 文本框 : Disable the 'Paste' option whilst allowing 'Cut' and 'Copy' on Right-click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com