- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在生成一堆具有不同大小和位置的 RectangleF 对象。在 GDI+ 中用渐变画笔填充它们的最佳方法是什么?
在 WPF 中,我可以创建一个 LinearGradientBrush,设置开始和结束相对
点,WPF 将处理其余的事情。
但是在 GDI+ 中,渐变画笔构造函数需要绝对坐标中的位置,这意味着我必须为每个矩形创建一个画笔,这将是一个非常复杂的操作。
我错过了什么还是这确实是唯一的方法?
最佳答案
如果您只想声明画笔一次,则可以在应用渐变之前指定变换。请注意,使用转换将覆盖许多可以在 LinearGradientBrush
上指定的构造函数参数。
LinearGradientBrush.Transform Property (System.Drawing.Drawing2D)
要修改变换,请调用画笔对象上与所需矩阵运算相对应的方法。请注意,矩阵运算不可交换,因此顺序很重要。出于您的目的,您可能希望对矩形的每次呈现按以下顺序执行这些操作:缩放、旋转、偏移/平移。
LinearGradientBrush.ResetTransform Method @ MSDN
LinearGradientBrush.ScaleTransform Method (Single, Single, MatrixOrder) @ MSDN
LinearGradientBrush.RotateTransform Method (Single, MatrixOrder) @ MSDN
LinearGradientBrush.TranslateTransform Method (Single, Single, MatrixOrder) @ MSDN
请注意,系统级绘图工具实际上并不包含渐变画笔的库存定义,因此,如果您对制作多个画笔有性能问题,那么创建多个渐变画笔的开销不应超过GDI+/System.Drawing 维护定义渐变和样式所需的数据。您可能会根据需要为每个矩形创建一个画笔,而不必深入研究通过变换自定义画笔所需的数学。
Brush Functions (Windows) @ MSDN
以下是您可以在 WinForms 应用程序中测试的代码示例。此应用程序使用 45 度梯度的渐变画笔绘制图 block ,缩放到图 block 的最大尺寸(简单计算)。如果您摆弄值和变换,您可能会发现,如果您有不平凡的渐变定义,则不值得使用为所有矩形设置变换的技术。否则,请记住,您的转换是在世界级别应用的,在 GDI 世界中,y 轴是颠倒的,而在笛卡尔数学世界中,它是从下到上排序的。这也会导致角度顺时针应用,而在三角学中,当 y 轴向上时,角度会逆时针方向增加值。
using System.Drawing.Drawing2D;
namespace TestMapTransform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Rectangle rBrush = new Rectangle(0,0,1,1);
Color startColor = Color.DarkRed;
Color endColor = Color.White;
LinearGradientBrush br = new LinearGradientBrush(rBrush, startColor, endColor, LinearGradientMode.Horizontal);
int wPartitions = 5;
int hPartitions = 5;
int w = this.ClientSize.Width;
w = w - (w % wPartitions) + wPartitions;
int h = this.ClientSize.Height;
h = h - (h % hPartitions) + hPartitions;
for (int hStep = 0; hStep < hPartitions; hStep++)
{
int hUnit = h / hPartitions;
for (int wStep = 0; wStep < wPartitions; wStep++)
{
int wUnit = w / wPartitions;
Rectangle rTile = new Rectangle(wUnit * wStep, hUnit * hStep, wUnit, hUnit);
if (e.ClipRectangle.IntersectsWith(rTile))
{
int maxUnit = wUnit > hUnit ? wUnit : hUnit;
br.ResetTransform();
br.ScaleTransform((float)maxUnit * (float)Math.Sqrt(2d), (float)maxUnit * (float)Math.Sqrt(2d), MatrixOrder.Append);
br.RotateTransform(45f, MatrixOrder.Append);
br.TranslateTransform(wUnit * wStep, hUnit * hStep, MatrixOrder.Append);
e.Graphics.FillRectangle(br, rTile);
br.ResetTransform();
}
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
this.Invalidate();
}
}
}
这是输出的快照:
关于winforms - 绘制渐变矩形的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073939/
我有一个 winform 应用程序和一个可观察的设置,如下所示: Form form = new Form(); Label lb = new Label(); form.Controls.Add(l
在 Windows 窗体中实现多项选择选项的最佳方法是什么?我想从列表中强制执行单个选择,从默认值开始。 看起来 ComboBox 是一个不错的选择,但是有没有办法指定一个非空白的默认值? 我可以在代
如何在 WinForm 应用程序中保护我的 ConnectionString? 最佳答案 你不能。尽管您可以在 app.config 文件中加密连接字符串,但应用程序需要能够解密它,因此,始终可以检索
有谁知道像 DotNetBar 那样的 Winforms 面包屑样式导航。 http://www.devcomponents.com/dotnetbar/BreadCrumbHorizontalTre
我正在寻找在 Windows 窗体 C# 中实现多选启用列表框的方法。 有什么建议? 谢谢。 最佳答案 只需添加一个 ListBox控制和设置属性:SelectionMode = SelectionM
我有一个简单的 WinForms 应用程序,用于输入测试用例。自从我将此应用程序升级到 .NET 4.0 并将新的标签页添加到标签页控件以根据 XSD 架构验证 XML 以来,该应用程序一直随机崩溃。
在老式的 MFC 中,DDX 例程内置了表单条目的验证。例如,可以自动检查用户是否在用于数字输入的文本框中输入了字符串。 Winforms中有这样的机制吗?显然,可以为“onChange”等类型的事件
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
简单的问题,虽然办公室里似乎没有人知道,而且我找不到一个好的方法来问谷歌这个问题。在 winforms 中,如果您有一个处理事件的函数(在本例中是在 focusLost 上),那么该函数是否与触发该事
在 Winform 中,我有一个组框,其中有几个文本框控件。如果我删除组框,文本框也会被删除。它们以某种方式与 Groupbox 联系在一起,尽管我没有故意做任何事情来实现这种情况。问题 - 如何删除
我可以在哈希表中设置表单元素: $Hash = @{} $Hash.Main = New-Object System.Windows.Forms.Form $Hash.Main.Left = 0 $H
我是 Windows 开发新手。我开发了一个 WinForm 应用程序,它与串行设备通信并在图表上绘制数据。该应用程序应每天 7/7 24 小时运行。代码执行正确,但执行几个小时后,UI 卡住,操作系
有没有办法记录 Win Forms 应用程序中的所有点击?我想拦截点击并记录该操作以及导致该操作的控件的名称。 这可能吗? 提前致谢。 更新:我正在寻找一个应用程序范围的解决方案,是否没有办法将监听器
我不知道这是否会影响其他控件,但对于列表框和选中列表框,列表框的底部仅以一定的间隔随表单调整大小。 假设我有一个表单和一个列表框,该列表框与表单边缘的所有边都有 2px 间隙,并锚定在所有四个边上。现
我在大多数 WinForms 控件的设计 View 中看到“Tag”属性。我从未使用过此标签,并且想知道为什么我要使用它。 最佳答案 它允许您使用控件存储一些自己的数据。它在树控件中最有用,您可能希望
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果有,有人吗?鱼眼 (菜单/ Pane )窗体在 Ajax 应用程序中常见的控件: 例如:http://interface.eyecon.ro/demos/fisheye.html 用 google
这是 Determine Label Size based upon amount of text and font size in Winforms/C# 的倒数. 给定一个高度固定但宽度可变的矩形
我们公司正在研究为我们的开发团队采购 22"显示器。当前唯一的问题是我们的用户将使用较小的屏幕。 我们尝试使用屏幕网格工具(gridmove 和 nvidia 的 utils),但它们并不完全现实。
我有一个 winforms 应用程序,它在网络服务请求期间被锁定 我已经尝试使用 doEvents 来保持应用程序解锁,但它仍然不够响应, 我怎样才能绕过这个锁定,让应用程序始终响应? 最佳答案 最好
我是一名优秀的程序员,十分优秀!