突出显示标签 Windows 窗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/702754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highlighting labels Windows Forms
提问by cweston
Is there any way to make a labelon a .NET Windows form to be highlightableto allow for the text to be copied. 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.
有什么方法可以使.NET Windows 表单上的标签突出显示以允许复制文本。我试图用一个看起来像标签的文本框来做到这一点,但这会导致光标闪烁。
回答by Samuel
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;
回答by Henk Holterman
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?
如果您希望它成为具有所有键盘和快捷键支持的可预测、表现良好和标准的控件,您只需要一个文本框。然后闪烁的光标是一个正常的有用功能,为什么要打它?
回答by BlueMonkMN
It's not unusual for selectable static text to show a flashing 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 资源管理器中获取任何文件的属性并选择该窗口中的任何数据,您还会看到一个闪烁的光标。
回答by Paul Ruane
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
回答by alexn
You have the HideCaret function in User32.dll. Use it like this:
您在 User32.dll 中有 HideCaret 函数。像这样使用它:
[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.
这将阻止插入符号在文本框具有焦点时显示。
回答by Jay Riggs
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:
需要考虑的一件事是继续使用标签,然后使用以下方法以编程方式将内容(标签的文本)复制到剪贴板中:
Clipboard.SetText(yourLabel.Text);