简体   繁体   English

高亮标签 Windows Forms

[英]Highlighting labels Windows Forms

Is there any way to make a label on a .NET Windows form to be highlightable to allow for the text to be copied.有什么方法可以使.NET Windows 表格上的 label 可突出显示以允许复制文本。 I have attempted to do this with a text box that was made to look like a label, but this results in a flashing cursor.我试图用一个看起来像 label 的文本框来执行此操作,但这会导致 cursor 闪烁。

I think this is pretty darn close:我认为这非常接近:

textBox.BackColor = System.Drawing.SystemColors.Control;
textBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
textBox.ReadOnly = true;
textBox.Text = "This is selectable text";
textBox.MouseUp += new MouseEventHandler(
                          delegate(object sender, MouseEventArgs e)
                             { HideCaret((sender as Control).Handle); });

[DllImport("User32.dll")]
static extern Boolean HideCaret(IntPtr hWnd);

And if you need it to span more than one line:如果您需要它跨越多行:

textBox.Multiline = true;

If you want it to be a predictable, well behaved and standard control with all the keyboard and shortcut support you simply need a textbox.如果您希望它是一个可预测的、行为良好的标准控件,并具有所有键盘和快捷键支持,您只需要一个文本框。 And then the flashing cursor is a normal helpful feature, why fight it?然后闪烁的 cursor 是一个正常有用的功能,为什么要打呢?

It's not unusual for selectable static text to show a flashing cursor.可选的 static 文本显示闪烁的 cursor 并不罕见。 If you get the properties of any file in Windows Explorer and select any data in that window, you'll also see a flashing cursor.如果您在 Windows Explorer 和 select 中获得任何文件的属性 window 中的任何数据,您还将看到闪烁的 Z17091A907A894036。

I have done this previously, a couple of years back, I think I used this Win API call (but with a regular text box): http://www.dreamincode.net/forums/showtopic35107.htm我以前做过,几年前,我想我用过这个 Win API 调用(但有一个常规文本框): http://www.dreamincode.net/forums/showtopic35107.htm

You have the HideCaret function in User32.dll.您在 User32.dll 中有 HideCaret function。 Use it like this:像这样使用它:

[DllImport("User32.dll")]
static extern bool HideCaret(IntPtr hWnd);

private void textBox_Enter(object sender, EventArgs e)
{
    HideCaret(textBox.Handle);
}

This will prevent the caret from showing when textbox has focus.这将防止插入符号在文本框具有焦点时显示。

One thing to consider is to go ahead and use a label, but then programmatically copy content (the Label's text) into the clipboard using:要考虑的一件事是提前 go 并使用 label,然后使用以下方式以编程方式将内容(标签的文本)复制到剪贴板中:

Clipboard.SetText(yourLabel.Text);

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

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