如何自定义 ToolStripTextBox 的呈现?

编程入门 行业动态 更新时间:2024-10-08 06:26:35
本文介绍了如何自定义 ToolStripTextBox 的呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我非常喜欢 ToolStripProfessionalRenderer 样式,但我不喜欢它呈现 ToolStripTextBox 的方式.在这里,ToolStripSystemRenderer 在 IMO 上做得更好.现在有没有办法将两个渲染器的行为结合起来,为文本框使用系统样式,为其他所有内容使用专业样式?我已经成功地成功地使用专业风格的按钮和系统风格的其余部分(通过派生两个类).但是 ToolStrip 中的文本框似乎没有由渲染器处理.使用 .NET Reflector,这些文本框甚至似乎都没有 Paint 事件处理程序,尽管它是由 ToolStrip.OnPaint 方法调用的.我想知道绘制这样一个文本框的代码在哪里,以及如何将它配置为像所有其他文本框一样绘制文本框.

解决方案

如果你只是想要系统渲染,最简单的方法是使用 ToolStripControlHost 代替:

类 ToolStripSystemTextBox : ToolStripControlHost{公共工具条系统文本框:基础(新文本框()){}[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)][TypeConverter(typeof(ExpandableObjectConverter))]public TextBox TextBox { get { return Control as TextBox;} }}

我在这里采取了简单的方法,将底层的 TextBox 直接暴露给表单设计器,而不是委托其所有属性.显然,如果你愿意,你可以编写所有的属性委托代码.

另一方面,如果有人想做真正的自定义渲染,我会告诉你 ToolStripTextBox 是做什么的.它不是直接托管 TextBox,而是托管一个名为 ToolStripTextBoxControl 的私有派生类.此类重写其 WndProc 以便直接处理 WM_NCPAINT.然后它不是将实际绘图委托给渲染器,而是检查渲染器的类型,然后分支到 ToolStripTextBoxControl 内的不同渲染代码.太丑了.

I like the ToolStripProfessionalRenderer style quite a lot, but I do not like the way it renders a ToolStripTextBox. Here, ToolStripSystemRenderer does a better job IMO. Now is there a way to combine both renderers' behaviour to use system style for text boxes and pro style for everything else? I have successfully managed to use pro style for buttons and system style for the rest (by deriving both classes). But text boxes in a ToolStrip don't seem to be handled by the renderer. Using .NET Reflector, those text boxes don't even seem to have a Paint event handler, although it's called by the ToolStrip.OnPaint method. I'm wondering where's the code to paint such a text box at all and how it can be configured to draw a text box like all other text boxes.

解决方案

If you just want system rendering, the easiest approach is to use ToolStripControlHost instead:

class ToolStripSystemTextBox : ToolStripControlHost { public ToolStripSystemTextBox : base(new TextBox()) { } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [TypeConverter(typeof(ExpandableObjectConverter))] public TextBox TextBox { get { return Control as TextBox; } } }

I've taken the easy way out here and exposed the underlying TextBox directly to the form designer, instead of delegating all its properties. Obviously you can write all the property delgation code if you want.

On the other hand, if anyone wants to do truly custom rendering, I'll tell you what ToolStripTextBox does. Instead of hosting a TextBox directly, it hosts a private derived class called ToolStripTextBoxControl. This class overrides its WndProc in order to directly handle WM_NCPAINT. And then instead of delegating the actual drawing to the Renderer, it checks the Renderer's Type, and then branches to different rendering code inside of ToolStripTextBoxControl. It's pretty ugly.

更多推荐

如何自定义 ToolStripTextBox 的呈现?

本文发布于:2023-11-28 04:34:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   ToolStripTextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!