视觉风格独立绘图 [英] visual styles independent drawing

查看:82
本文介绍了视觉风格独立绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#winforms创建看起来像真实控件的自定义控件.

Using C# winforms, i want to create custom controls that looks like the real ones.

有许多类可以用来绘制看起来像真实控件的控件:ControlPaintVisualStyleRendererButtonRendererCheckBoxRendererComboBoxRendererGroupBoxRendererProgressBarRendererRadioButtonRendererScrollBarRendererTabRendererTextBoxRendererTextRendererToolStripProfessionalRendererToolStripRendererToolStripSystemRendererTrackBarRenderer.

There are a lot of classes that can be used to draw controls that looks like the real ones: ControlPaint, VisualStyleRenderer, ButtonRenderer, CheckBoxRenderer, ComboBoxRenderer, GroupBoxRenderer, ProgressBarRenderer, RadioButtonRenderer, ScrollBarRenderer, TabRenderer, TextBoxRenderer, TextRenderer, ToolStripProfessionalRenderer, ToolStripRenderer, ToolStripSystemRenderer, TrackBarRenderer.

考虑视觉样式时出现的问题:我想成为独立的视觉样式.含义:我不在乎用户是否允许使用视觉样式,我希望它能正常工作.如果用户启用了视觉样式,则我想使用视觉样式来绘制它,否则我想不使用视觉样式来绘制它.

The problems coming when considering visual styles: I want to be visual styles independent. Meaning: I dont care if user allowing visual-styles or not, i want it to work. if user enabled visual-styles, i want to draw it using visual-styles, otherwise i want to draw it without visual-styles.

根据MSDN文档,唯一与视觉样式无关的类是ButtonRendererCheckBoxRendererGroupBoxRendererRadioButtonRenderer.这意味着对于所有其他情况,我需要检查自己是否启用了视觉样式,并使用不同的代码来绘制零件.

By the MSDN documentation, the only classes that are visual-styles independent are ButtonRenderer, CheckBoxRenderer, GroupBoxRenderer, RadioButtonRenderer. That means that for all other cases i need to check myself if visual-styles enabled and use different code to draw the parts.

假设我想自己绘制一个Tab控件部件. TabRenderer类具有所有所需的功能,但是只有在用户启用了视觉样式时它才起作用.否则,我需要使用ControlPaint类进行绘制,但是它使用完全不同的模型,没有ControlPaint.DrawTab()方法或类似的方法,并且我需要弄清楚需要绘制的矩形类型,以便看起来像一个真实标签.那很烦人.

Suppose i want to draw a Tab control parts myself. TabRenderer class has all needed functionality for that, but it works only if user enabled visual styles. otherwise I need to use ControlPaint class to draw, but it uses completely different model, there is no ControlPaint.DrawTab() method or something like that, and i need to figure out what rectangle types i need to draw so it will look like a real tab. that`s annoying.

内置控件(包括Tab控件)已经具有此功能,可以使用或不使用视觉样式进行绘制. Microsoft为什么不向自定义控件创建者公开此功能?自定义控件的创建者为什么要受苦?

The built-in controls, including the Tab control, already have this functionality of drawing themselves with or without visual styles. Why doesn`t microsoft exposing this functionality for the custom control creators? Why are custom control creators should suffer?

推荐答案

不幸的是,这很痛苦,但这是编写自定义控件的成本.我在许多WinForm控件中看到Microsoft这样做的方式是检查Application.RenderWithVisualStyes和Control.UseVisualStyleBackColor.根据这些值,它们将相应地绘制控件.

It's a pain unfortunately, but that is the cost for writing custom controls. The way I have seen Microsoft do this in many of the WinForm controls, is to check Application.RenderWithVisualStyes, and Control.UseVisualStyleBackColor. Depending on those values they will paint the control accordingly.

他们使用的一种常见模式是创建称为MyControlRenderer.cs的内部类.在此渲染器内部,魔术实际上发生了.如果查看NET REF代码,您会发现它们还使用System.Windows.Forms.VisualStyles命名空间中的VisualStyleRenderer类,并且还有一些实际的绘制代码.

A common pattern they use is to create internal classes called MyControlRenderer.cs. Inside this renderer is where the magic actually happens. If you look at the NET REF code, you will see that they also use VisualStyleRenderer class from System.Windows.Forms.VisualStyles namespace, and there is some actual paint code as well.

您将需要同时使用这些各种类和工具,以及同时进行自己的工作.在具有和不具有视觉样式的机器上测试您的控件.

You will need to utilize a mix of these various classes and tools, and a mix of also doing your own work. Testing your control on a machine with, and without visual styles.

当我希望下班回家时,我还会发布更多信息.

When I hope home from work I will post a bit more.

这篇关于视觉风格独立绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆