Label 和 TextBox 之间的(技术)区别是什么?
What is the (technical) difference between a Label and a TextBox?
我在网上看到很多答案都说 Label 的文本不能像 TextBox
的内容那样 selected/copied,
但是 无法复制 Label
的文本的根本原因是什么?
Windows本身可以找到光标位置下的文字,为什么WinForm Label
控件不能呢?
为了让用户 select 或复制控件的文本,控件必须允许您将焦点设置到它,方法是单击或 tabing控件。
Label 设计不允许这样做。
Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.
因此,虽然标签和文本框都继承自 System.Windows.Control,但它们是不同的东西,用于不同的目的。就像橙子和苹果都是水果,但又不同。
但是,如果您正在创建一个应用程序并且想要看起来像标签的东西,但允许用户select(但不编辑)文本,那么您可以使用具有以下属性集的 TextBox:
Backcolor = Control
ReadOnly = true
BorderStyle = none
如下图...
或者,如果你有一个应用程序并且想从标签之类的东西中获取文本,你可以使用 Win32 API 函数 GetWindowText
, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms 是在所有这些之上的抽象。
关于获取鼠标光标下 window 的句柄,请参阅 this question。
我在网上看到很多答案都说 Label 的文本不能像 TextBox
的内容那样 selected/copied,
但是 无法复制 Label
的文本的根本原因是什么?
Windows本身可以找到光标位置下的文字,为什么WinForm Label
控件不能呢?
为了让用户 select 或复制控件的文本,控件必须允许您将焦点设置到它,方法是单击或 tabing控件。
Label 设计不允许这样做。
Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.
因此,虽然标签和文本框都继承自 System.Windows.Control,但它们是不同的东西,用于不同的目的。就像橙子和苹果都是水果,但又不同。
但是,如果您正在创建一个应用程序并且想要看起来像标签的东西,但允许用户select(但不编辑)文本,那么您可以使用具有以下属性集的 TextBox:
Backcolor = Control
ReadOnly = true
BorderStyle = none
如下图...
或者,如果你有一个应用程序并且想从标签之类的东西中获取文本,你可以使用 Win32 API 函数 GetWindowText
, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms 是在所有这些之上的抽象。
关于获取鼠标光标下 window 的句柄,请参阅 this question。