繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Web Service开发 >> 用SetStyle方法重绘你的窗体

用SetStyle方法重绘你的窗体

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:9  文字大小:【】【】【
简介:鲜为人知的SetStyle方法可以让你控制重绘一个窗体的方式。 by Ken Getz 注:Ken Getz在Orlando的VSLive!上同Brian Randell一起举办了一个主题为“Build a Rich Client App with Visual Studio .NET”的研讨会。...
关键字:窗体 SetStyle 方法

鲜为人知的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.Paint Dim path As New GraphicsPath() Dim pt As New PointF() Dim rct As Rectangle = Me.ClientRectangle path.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 = pt Dim 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 = Colors cb.Positions = Positions pgb.InterpolationColors = cb e.Graphics.FillRectangle(pgb, rct) Dim f As New StringFormat() f.Alignment = StringAlignment.Center e.Graphics.DrawString( _ Date.Now.ToLongTimeString, _ New Font("Tahoma", 13), Brushes.White, pt, f)End Sub

责任编辑:admin
相关文章