gpt4 book ai didi

C# 停止按钮在单击时获得焦点

转载 作者:可可西里 更新时间:2023-11-01 13:50:21 24 4
gpt4 key购买 nike

我有几个按钮,单击它们时我不希望它们获得焦点,也不希望空格键再次“按下”它们。

我想要与 Windows 计算器中的按钮相同的功能。

Google 和搜索堆栈一切似乎都与表单有关,例如。 Make a form not focusable in C#

我知道我应该重写 WndProc 但不确定如何处理我应该捕获/忽略哪些消息等。据我所知:

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}

最佳答案

我今天处理了这个问题,以下是对我来说最简单的答案。我不想使用 this.Focus() 因为我需要焦点保持不变。

http://social.msdn.microsoft.com/Forums/windows/en-US/f1babeac-4bd9-498f-b19b-90b9fed0d751/c-stop-button-from-gaining-focus-on-click

创建您自己的无法选择的按钮类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YourNameSpaceHere {
class NoSelectButton : Button{

public NoSelectButton() {

SetStyle(ControlStyles.Selectable, false);

}
}
}

现在,使用 NoSelectButton 而不是系统版本更新设计文件。每个实例应该位于两个位置。

注意:Visual Studio 设计器可能会暂时中断其预览,直到您按下“开始”。

关于C# 停止按钮在单击时获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741544/

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