应对.NET中绘制闪烁的几种解决方法

<!-- [if !mso]> <mce:style><!-- v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} --> <!-- [endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0mm 5.4pt 0mm 5.4pt; mso-para-margin:0mm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

<!-- [if !mso]> <mce:style><!-- v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} --> <!-- [endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0mm 5.4pt 0mm 5.4pt; mso-para-margin:0mm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

绘图 制控件 程中最 人郁 的便是 复杂绘 制的 闪烁问题 。 如果您 为该问题 所困 ,那 应该 注意以下几点:

1
, 使用 冲:
比如要向 Graphics g 形, 你可以在内存中使用一 张图 片作 为缓 冲,先将 制到 该图 片上, 制完成后再将 该图 制到 g 上。

Bitmapbuffer= new Bitmap( this . Width this . Height);
using (GraphicsbufferGrfx=Graphics . FromImage(buffer))
{

// 制到bufferGrfx
}
// 把缓冲图片绘制到 g
RectanglesrcRect=
this . bounds;
RectangledestRect=srcRect;
g
. DrawImage(buffer , destRect , srcRect , GraphicsUnit . Pixel);


不用担心两次 制会 来效率上的 失, 其速度与直接 制到 g 上几乎一 快。

2
base . OnPaint(e);
如果所有的 制操作都由自己来完成,那 在覆盖 OnPaint 方法 不要 使用 base . OnPaint(e);

3
e . ClipRectangle
运用剪 区域, 很重要 , 很 简单 地,比如 形区域只有很小一个部分需要更新你就没有必要更新全部 形区域。
具体 来就是,只有那些和剪 区域相交 ( 或被剪 区域包含 ) 的区域才需要 制。

注意这里是e.ClipRectangle而不是g.Clip,g.Clip表示的是该画布的绘图区域,其和e.ClipRectangle(本次描画的剪辑区域)没有本质联系。



// Block 象是程序的最小
foreach (Blockbk in this . Blocks)
{

if ( e . ClipRectangle . IntersectsWith(bk . Bounds))

{ //drawbk; }
}


4
Refrush() Invalidate() Update()
不要 易使用Refrush 来刷新 形。
在刷新之前,
先确定要刷新的区域,如果要刷新的区域只是 形区域的一部分, 应该 使用Invalidate() 来使 区域无效 ,然后使用Update 来更新 个无效区域便可。

5 SetStyle();
在以上几点之前, 应该 在控件的构造器 ( 或其他初始化方法 ) 置控件 式:

This . SetStyle(ControlStyles . OptimizedDoubleBuffer , true );

this . SetStyle(ControlStyles . AllPaintingInWmPaint , true );

里的 ControlStyles 有好几个枚 ,具体 置那些 ,以及它 的相互 系, 参考 MSDN 明文档。

6 DoubleBuffered
有好几 种标 准控件是内置双 冲的,比如Form PictureBox 等, 您可以将它 DoubleBuffered true 来减少 闪烁 ( 当然不要寄希望于 仅仅设 属性便万事大吉了, 不然以上 5 点不是白 )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值