繁体   English   中英

如何始终显示下划线字符? (C#Windows表格)

[英]How to always show underline character? (C# Windows Form)

我正在制作一个看起来像记事本的查找对话框的对话框。 我注意到记事本的“查找”对话框的下划线字符始终显示(我必须按ALT键才能在我的对话框中看到此内容)。 如何始终显示这样的下划线字符?

我尝试在Form_Load事件上使用SendKeys.Send(“%”)但没有任何反应。

还有一个问题,当我在子窗体上按ALT键时,它也会显示父窗体的下划线字符。 怎么避免呢?

这是Notepad的查找对话框: 在此输入图像描述

我很确定这不是关于Ease of Acess中心,因为记事本的主要形式并不总是显示这一点。

在记事本对话框中看到“查找”下划线中的n是一个故意的错误。 该对话框实际上不是记事本的一部分,它内置于Windows中 底层的winapi调用是FindText()。 该功能通常是一堆错误,一个核心问题是在UI处于“显示下划线”状态后创建新窗口无法正常工作,新窗口也不在该状态。 据推测,故意错误是基于用户有可能使用Alt键来显示对话框的假设。 如果他按下Ctrl + F,那就太好了。

Windows对话框可能只是通过简单地使用DrawText()绘制“Find”字符串并忽略DT_NOPREFIX选项来实现。 您可以使用TextRenderer.DrawText()执行相同操作,省略TextFormatFlags.HidePrefix选项。

不完全是WinFormsy,你喜欢Label控件而不是代码。 这是可以攻击的,你必须故意发送消息,将UI置于你自己的对话框的“show underlines”状态。 在OnHandleCreated()方法的覆盖中执行此操作:

    protected override void OnHandleCreated(EventArgs e) {
        const int WM_UPDATEUISTATE = 0x0128;
        base.OnHandleCreated(e);
        SendMessage(this.label1.Handle, WM_UPDATEUISTATE, new IntPtr(0x30002), IntPtr.Zero);
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

其中“label1”是您想要显示下划线的控件。 如果有的话,重复其他控件。 应该通过将消息发送到表单来工作,这不起作用是堆错误的一部分。 呸。

FWIW: 通过改变系统选项,如重复推荐的解决这个问题。 这是非常不合理的。

您可以使用RichTextBox控件和扩展方法:

public static class FontHelper
{
    public static void Underline(this RichTextBox txtBox, int underlineStart, int length)
    {
        if (underlineStart > 0)
        {
            txtBox.SelectionStart = underlineStart;
            txtBox.SelectionLength = length;
            txtBox.SelectionFont = new Font(txtBox.SelectionFont, FontStyle.Underline);
            txtBox.SelectionLength = 0;
        }
    }        
}

richTextBox1.Text = "Search for";
richTextBox1.Underline(7, 1);   // index and length of underlying text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM