请教有写WinForm控件经验的朋友.
.NET控件库里没有Animate控件,我要升级一个原来VB6的程序到VB.NET,想把其中用到的所有VB6的ActiveX控件都升级到.NET的控件,可是惟独没有Animate,所以我就想自己写一个.NET的Animate控件.
我想ComCtl32.dll里包含了windows标准控件库(包含Animate),那些.NET的标准控件(比如TreeView等等)应该就是对这个标准控件的一个包装而已.所以我想自己包装一个Animate.但我没有找到如何从System.Window.Forms.Form继承写自定义WinForm控件的标准方法,该重写或实现哪些方法.
下面我贴自己写的这个控件,现在可以工作但还有一些问题,请高手帮忙看看.
主要问题是
1)Style ACS_TRANSPARENT没有起到应有作用.(如果设置ACS_TRANSPARENT,动画的背景色应该和父窗口的背景色一致,可是现在的问题是设置ACS_TRANSPARENT,动画背景变为白色)
MSDN对ACS_TRANSPARENT的解释:Allows you to match an animation's background color to that of the underlying window, creating a "transparent" background. The control will send a WM_CTLCOLORSTATIC message to its parent. Use SetBkColor to set the background color for the device context to an appropriate value. The control interprets the upper-left pixel of the first frame as the animation's default background color. It will remap all pixels with that color to the value you supplied in response to WM_CTLCOLORSTATIC.
2)包含控件的窗口最小化,然后返回后,要过好几秒动画才能恢复
代码如下:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Text
Namespace CEInet.Windows.Forms
Public Class Animation
Inherits Control
'Animation ClassName
Private Const CNAnimate As String = "SysAnimate32"
Private Const WM_USER As Integer = &H400
'Animation Message
Private Const ACM_PLAY As Integer = WM_USER + 101
Private Const ACM_STOP As Integer = WM_USER + 102
Private Const ACM_OPEN As Integer = WM_USER + 103
'Animation Control Styles
Private Const ACS_AUTOPLAY As Integer = &H4
Private Const ACS_CENTER As Integer = &H1
Private Const ACS_TRANSPARENT As Integer = &H2
Private _bAutoPlay As Boolean = False
Private _bCenter As Boolean = False
Private _iBackStyle As Integer = 0
Public Property AutoPlay() As Boolean
Get
Return _bAutoPlay
End Get
Set(ByVal Value As Boolean)
_bAutoPlay = Value
End Set
End Property
Public Property Center() As Boolean
Get
Return _bCenter
End Get
Set(ByVal Value As Boolean)
_bCenter = Value
End Set
End Property
Public Property BackStyle() As Integer
Get
Return _iBackStyle
End Get
Set(ByVal Value As Integer)
_iBackStyle = Value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
End If
MyBase.Dispose(disposing)
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim p As System.Windows.Forms.CreateParams
p = MyBase.CreateParams()
p.ClassName = Me.CNAnimate
If AutoPlay Then p.Style = p.Style Or ACS_AUTOPLAY
If Center Then p.Style = p.Style Or ACS_CENTER
If BackStyle = 0 Then p.Style = p.Style Or ACS_TRANSPARENT
Return p
End Get
End Property
Public Sub Open(ByVal fileName As String)
Dim sb As New StringBuilder(fileName)
WinAPI.SendMessage(Me.Handle, ACM_OPEN, 0, sb)
End Sub
Public Sub Play(Optional ByVal Repeat As Short = -1, Optional ByVal Start As Short = 0, Optional ByVal [End] As Short = -1)
WinAPI.SendMessage(Me.Handle, ACM_PLAY, Repeat, Start Or [End] << 16)
End Sub
Public Sub [Stop]()
WinAPI.SendMessage(Me.Handle, ACM_STOP, 0, 0)
End Sub
End Class
End Namespace