重新发明标签控件
我需要从头开始重新发明/重新创建标签控件,以添加我自己的魔力。是的,我知道你在想什么(如果你不这么想,你不应该这样想吗?)。
有人能指出我正确的方向吗?
谢谢。
重新创建标签的全部目的是我想要完全控制它如何绘制到屏幕上,这样我也可以拥有它的 KeyDown 事件处理程序。例如,用户可以像编辑 TextBox 控件的内容一样编辑标签的内容。
另外,我不能简单地使用 TextBox 控件,因为它几乎需要(甚至更多)工作才能获得我想要的结果。
I need to reinvent/recreate the Label Control from scratch to add my own mojoness to it. Yes, I know what you're thinking (and if you're not thinking that, shouldn't you be?).
Can somebody point me in the right direction?
Thank you.
The whole purpose for recreating the label is that I want full control over how it is drawn onto screen, and so that I can have KeyDown Event Handlers for it, too. For example, the user can edit the contents of a label the same way they would edit the contents of a TextBox control.
Also, I cannot just simply use a TextBox control, as it would require almost, if not more work to get my desired result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不直接延长当前的时间呢?
class MyMojoLabel :标签 // 类似的东西
Why not just extend the current one?
class MyMojoLabel : Label // Kind of thing
一个好的起点也许是了解微软如何实现该标签。
为了更好地深入了解,您应该使用 Reflector 查看类或 Label 控件的调试源代码。
A good starting point would maybe to understand how Microsoft implemented the label.
To get a better in-depth look you should take a look with Reflector into the class or debugging the source code of the Label control.
这个怎么样?
How about this?
创建一个继承自 Control 的类。使用 SetStyle() 调用启用用户绘画和双缓冲,并重写 OnPaint() 以及您需要的任何其他方法。
Create a class that inherits from Control. Use the SetStyle() call to enable user painting and double-buffering, and override the OnPaint(), and any other methods that you need.
创建您自己的标签控件非常简单,您只需从 Control 开始并重写 OnPaint() 即可。去byte就是把它变成一个也有聚焦行为的控件。并允许用户编辑文本。完成后,您将重新发明 TextBox 控件。这比看起来要难得多。
首先集中注意力,这是最棘手的问题。用户不太可能想要频繁地聚焦控件。也许是某种秘密握手,例如双击。当您检测到一个时,您可以创建一个 TextBox 控件并将其放在标签前面。并在失去焦点时将其丢弃,更新标签的 Text 属性。或者一个显示小型编辑对话框的简单上下文菜单。
使用双击编辑方法的示例:
Creating your own label control is simple enough, you just need to start with Control and override OnPaint(). What is going to byte is to turn it into a control that also has focusing behavior. And allows the user to edit the text. By the time you're done, you'll have re-invented the TextBox control. Which is a lot harder than it looks.
Focus on the focusing first, that's the trickiest problem. It isn't very likely that the user will want to focus the control frequently. Maybe some kind of secret handshake, like a double-click. When you detect one, you could create a TextBox control and put it in front of the label. And dispose it when it loses focus, updating the label's Text property. Or a simple context menu that displays a small editing dialog.
An example that uses the double-click to edit approach: