使用GDI+绘图时的一个问题

redshiliu 2010-12-12 05:40:34
在使用GDI+绘图时使用了双缓冲,虽然绘制的时候不会出现闪烁,但是莫名其妙的是在你不做任何操作的时候Form_Paint事件居然会触发,导致了界面会出现闪烁,代码如下:

Option Explicit

Private Sub Form_Load()
InitGDIPlus
Timer1.Interval = 1
Timer1.Enabled = True
End Sub

Private Sub Form_Paint()
Debug.Print Time & ":paint"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
TerminateGDIPlus
End Sub

Private Sub Timer1_Timer()
Dim gpsMemory As Long
Dim bmpMemory As Long
Dim bmpBackGround As Long
Dim bmpMan As Long
Dim bmpCompass As Long
Dim Graphics As Long

GdipCreateBitmapFromFile StrPtr("d:\2.jpg"), bmpBackGround
GdipCreateBitmapFromFile StrPtr("d:\1.jpg"), bmpMan
GdipCreateBitmapFromFile StrPtr("d:\compass.png"), bmpCompass

GdipCreateBitmapFromScan0 Me.ScaleWidth, Me.ScaleHeight, 0, GpPixelFormat.PixelFormat32bppARGB, ByVal 0, bmpMemory
GdipGetImageGraphicsContext bmpMemory, gpsMemory
' GdipSetSmoothingMode gpsMemory, SmoothingModeAntiAlias

GdipDrawImageRectI gpsMemory, bmpBackGround, 0, 0, Me.ScaleWidth, Me.ScaleHeight
GdipDrawImageI gpsMemory, bmpMan, 0, 0
GdipDrawImageI gpsMemory, bmpCompass, 256, 256

GdipCreateFromHWND Me.hwnd, Graphics
GdipDrawImage Graphics, bmpMemory, 0, 0
GdipDisposeImage bmpMemory
GdipDisposeImage bmpBackGround
GdipDisposeImage bmpMan
GdipDisposeImage bmpCompass
GdipDeleteGraphics gpsMemory
GdipDeleteGraphics Graphics
End Sub

谁来告诉我为神马会触发Form_Paint事件?
...全文
149 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
东方之珠 2010-12-14
  • 打赏
  • 举报
回复
Timer里面写那么多代码,有问题??
redshiliu 2010-12-13
  • 打赏
  • 举报
回复
问题我找到了解决的方法就是把窗体的AutoRedraw设置为True就不会出现闪烁了。
你可以在Timer1_Timer事件里添加一个Debug.Print就可以知道Form_Paint事件是莫名奇妙不定时的出现
频率不高。
chinaboyzyq 2010-12-13
  • 打赏
  • 举报
回复
Paint 事件


在一个对象被移动或放大之后,或在一个覆盖该对象的窗体被移开之后,该对象部分或全部暴露时,此事件发生。

语法

Private Sub Form_Paint( )

Private Sub object_Paint([index As Integer])

Paint 事件语法包括下列部分:

部分 描述
object 一个对象表达式,其值是“应用于”列表中的一个对象。
index 一个整数,用来唯一地标识一个在控件数组中的控件。


说明

如果需要代码中各种图形方法的输出,则 Paint 事件过程就很有用。使用 Paint 过程,可以确保这样的输出在必要时能被重绘。

使用 Refresh 方法时,Paint 事件即被调用。如果 AutoRedraw 属性被设置为 True,重新绘图会自动进行,于是就不需要 Paint 事件。

如果 ClipControls 属性被设置为 False,在 Paint 事件过程中的绘图方法仅影响该窗体中新暴露的区域;否则,绘图方法将在该窗体未被控件覆盖的所有区域。( Image、Label、Line 和 Shape 控件除外。)

在 Resize 事件过程中使用 Refresh 方法可在每次调整窗体大小时强制对整个对象进行。

注意 对某些任务使用 Paint 事件过程能导致一个层叠事件。通常来说,在下列情况下,要避免用 Paint 事件过程:

移动一个窗体或控件,或者是调整其大小。


对影响大小或外观的任何变量进行改变,如:设置对象的 BackColor 属性等。


调用 Refresh 方法。
对上述这些任务来说,Resize 事件可能更为合适。
dbcontrols 2010-12-12
  • 打赏
  • 举报
回复
+1

[Quote=引用 1 楼 dafu2003 的回复:]
怎么说你?
分明是你把绘图过程写在了时钟事件中导致的,说什么触发paint.
把它移出去吧。
[/Quote]
dafu2003 2010-12-12
  • 打赏
  • 举报
回复
怎么说你?
分明是你把绘图过程写在了时钟事件中导致的,说什么触发paint.
把它移出去吧。

809

社区成员

发帖
与我相关
我的任务
社区描述
VB 多媒体
社区管理员
  • 多媒体
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧