当前位置:萝卜系统下载站 > 技术开发教程 > 详细页面

用SetStyle办法重绘你的窗体

用SetStyle办法重绘你的窗体

更新时间:2022-07-07 文章作者:未知 信息来源:网络 阅读次数:

鲜为人知的SetStyle方法可以让你控制重绘一个窗体的方式。
by Ken Getz
注:Ken Getz在Orlando的VSLive!上同Brian Randell一起举办了一个主题为“Build a Rich Client App with Visual Studio .NET”的研讨会。本篇技巧选自该研讨会的资料。
运用GDI+和Windows窗体,我们可以很容易地创建一个渐变色(gradient)来填充一个区域。运用.NET Framework提供的简单的方法,你可以创建linear gradients(线型渐变填充)或path gradients(路径渐变填充)。然而,真正的问题是这些复杂的背景图形是资源密集型(resource-intensive)的。最近,我正在做一个模拟的时钟演示程序,用一个渐变色来填充钟面。每一秒,当时钟重绘它的钟面来显示时钟指针的正确位置时,它也重绘了整个背景渐变色。即使在一台很快的机器上,这种方法也并不很好。我将向你介绍SetStyle方法,它可以让你指定如何、何时来重绘你的窗体。
首先,用下面的代码做试验,你可以从中得到一些着色变换的乐趣。修改一个新窗体的Paint事件使它包含该代码:

Private Sub frmMain_Paint( _ ByVal sender As Object, _ ByVal e As PaintEventArgs) _ Handles MyBase.PaintDim path As New GraphicsPath()Dim pt As New PointF()Dim rct As Rectangle = Me.ClientRectanglepath.AddRectangle(rct)Dim pgb As New PathGradientBrush(path)pt = New PointF( _ CType(Me.ClientSize.Width / 2, Single), _ CType(Me.ClientSize.Height / 2, Single))pgb.CenterPoint = ptDim Colors() As Color = _ {Color.Red, Color.Orange, Color.Yellow, _ Color.Green, _ Color.Blue, Color.Indigo, Color.Violet}Dim Positions() As Single = _ {0.0, 0.1, 0.2, 0.4, 0.6, 0.8, 1}Dim cb As ColorBlend = New ColorBlend()cb.Colors = Colorscb.Positions = Positionspgb.InterpolationColors = cbe.Graphics.FillRectangle(pgb, rct)Dim f As New StringFormat()f.Alignment = StringAlignment.Centere.Graphics.DrawString( _ Date.Now.ToLongTimeString, _ New Font("Tahoma", 13), Brushes.White, pt, f)End Sub

温馨提示:喜欢本站的话,请收藏一下本站!

本类教程下载

系统下载排行

网站地图xml | 网站地图html