Vb.net 使用ApplicationEvents.vb中的PictureBox_Paint

Vb.net 使用ApplicationEvents.vb中的PictureBox_Paint,vb.net,arguments,single-instance,Vb.net,Arguments,Single Instance,我目前正在开发一个小型的单实例应用程序,该应用程序由外部应用程序使用命令行参数调用。我会尽量弄清楚的;) 因此,我有一个正在运行的应用程序,它将执行我正在开发的应用程序。此新应用程序基于命令行参数具有不同的选项,并且是单个实例。因此,每次其他应用程序调用新应用程序时,它都会根据该命令行参数更新正在运行的实例(这不容易解释,我希望很清楚) 除了一件事,一切都很好。 申请表使用图片盒显示图像。其中一个命令行参数将使该图像淡入另一个。为此,我在Form1.vb中使用了PictureBox1_Paint

我目前正在开发一个小型的单实例应用程序,该应用程序由外部应用程序使用命令行参数调用。我会尽量弄清楚的;) 因此,我有一个正在运行的应用程序,它将执行我正在开发的应用程序。此新应用程序基于命令行参数具有不同的选项,并且是单个实例。因此,每次其他应用程序调用新应用程序时,它都会根据该命令行参数更新正在运行的实例(这不容易解释,我希望很清楚)

除了一件事,一切都很好。 申请表使用图片盒显示图像。其中一个命令行参数将使该图像淡入另一个。为此,我在Form1.vb中使用了PictureBox1_Paint Private Sub,但在ApplicationEvents.vb中也需要它,但我无法让它工作

如果我在ApplicationEvents.vb中尝试以下代码,就会收到错误消息“BC30506 Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量。”

如果删除“Handles PictureBox1.Paint”,则不再显示错误消息,但不会重新绘制PictureBox

有什么想法吗

谢谢

============================================================================

嘿,很抱歉回复晚了,但我一直很忙:)

因此,我尝试了下面的解决方案,但无法使其工作…:(

这是我到目前为止为Form1.vb和ApplicationEvents.vb编写的代码(删除了许多不必要的内容)。你能帮助我如何应用解决方案吗

Form1.vb

Public Class Form1

    Dim TickTime As Integer = My.Settings.TransitionTime / 100
    Private _fadeOpacity As Single = 0
    Private CurrentImage As Bitmap
    Private NextImage As Bitmap

    Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
        Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
        opacity = Math.Max(0, Math.Min(opacity, 1.0F))
        Using ia As New Imaging.ImageAttributes
            Dim cm As New Imaging.ColorMatrix
            cm.Matrix33 = opacity
            ia.SetColorMatrix(cm)
            Dim destpoints() As PointF = {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
            Using g As Graphics = Graphics.FromImage(bmp2)
                g.DrawImage(bmp, destpoints,
                New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
            End Using
        End Using
        Return bmp2
    End Function

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If _fadeOpacity < 100 AndAlso CurrentImage IsNot Nothing Then
            e.Graphics.DrawImageUnscaled(CurrentImage, Point.Empty)
        End If
        If _fadeOpacity > 0 AndAlso NextImage IsNot Nothing Then
            Using fadedImage As Bitmap = FadeBitmap(NextImage, _fadeOpacity)
                e.Graphics.DrawImageUnscaled(fadedImage, Point.Empty)
            End Using
        End If
    End Sub

    Private Sub StartTransition(ImageName As String)
        ' In settings I have a parameter for the transition type, CUT or DISSOLVE. This sends the new image to load in the approriate function
        Select Case My.Settings.TransitionType
            Case 0
                Transition_CUT(ImageName)
            Case 1
                Transition_Dissolve(ImageName)
        End Select
    End Sub

    Private Sub Transition_CUT(ImageName As String)
        ' Replaced the image in PictureBox1
        CurrentImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        Me.PictureBox1.Refresh()
    End Sub

    Private Sub Transition_Dissolve(ImageName As String)
        ' Fade between current image and new image over the defined time in My.Settings (in ms)
        NextImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        'fade to newimage
        Dim TickTime As Integer = My.Settings.TransitionTime / 100
        For i = 0 To 1 Step 0.01
            _fadeOpacity = CSng(i)
            Me.PictureBox1.Invalidate()
            wait(TickTime)
        Next
        wait(100)
        CurrentImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        _fadeOpacity = CSng(0)
        Me.PictureBox1.Invalidate()
    End Sub

    Private Sub wait(ByVal interval As Integer)
        ' custom timer
        Dim stopW As New Stopwatch
        stopW.Start()
        Do While stopW.ElapsedMilliseconds < interval
            ' Allows UI to remain responsive
            Application.DoEvents()
        Loop
        stopW.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Load default image at startup
        CurrentImage = New Bitmap(Image.FromFile(My.Settings.DefaultBackgroundImage), Me.PictureBox1.Size)
        Me.PictureBox1.Refresh()

        ' For easy communication between form1 and ApplicationEvents, the command line argument has been passed to Label6
        If Label6.Text.StartsWith("/init") Then
            ' Initialize the application and load default background image from xml settings
            StartTransition(My.Settings.DefaultBackgroundImage)
        ElseIf Label6.Text.StartsWith("/load") Then
            ' Check existance and load the specified image
            Dim inputArgument As String = "/load="
            Dim inputName As String = ""
            If Label6.Text.StartsWith(inputArgument) Then
                inputName = Label6.Text.Remove(0, inputArgument.Length)
            End If
            If inputName = "" Then
                ' No image name specified, load the image not specified image
                StartTransition(My.Settings.ImageNotSpecified)
            Else
                If My.Computer.FileSystem.FileExists(My.Settings.ImagesFolder & inputName & ".jpg") Then
                    ' Image has been found and is displayed
                    StartTransition(My.Settings.ImagesFolder & inputName & ".jpg")
                Else
                    ' Image not found, displaying the not found message
                    StartTransition(My.Settings.ImageNotFound)
                End If
            End If
        ElseIf Label6.Text.StartsWith("/quit") Then
            ' Quit application
            Me.Close()
        Else
            ' No parameter sent, showing instructions
        End If
    End If

    End Sub

    Private Function argument() As String
        Throw New NotImplementedException
    End Function

End Class
公共类表单1
Dim TickTime作为整数=My.Settings.TransitionTime/100
Private _FadePocity作为单个=0
私有图像作为位图
私有下一页作为位图
专用函数FadeBitmap(ByVal bmp作为位图,ByVal不透明度作为单个)作为位图
将bmp2调整为新位图(bmp.Width、bmp.Height、Imaging.PixelFormat.Format32bppArgb)
不透明度=数学最大值(0,数学最小值(不透明度,1.0F))
将ia用作新的Imaging.ImageAttributes
Dim cm作为新的Imaging.ColorMatrix
cm.x33=不透明度
ia.SetColorMatrix(厘米)
Dim destpoints()作为点F={新点(0,0),新点(bmp.Width,0),新点(0,bmp.Height)}
使用g作为Graphics=Graphics.FromImage(bmp2)
g、 DrawImage(bmp、点、,
新矩形F(Point.Empty,bmp.Size),GraphicsUnit.Pixel,ia)
终端使用
终端使用
返回bmp2
端函数
私有子PictureBox1_Paint(ByVal sender作为对象,ByVal e作为System.Windows.Forms.PaintEventArgs)处理PictureBox1.Paint
如果_fadeCapacity<100并且CurrentImage不是空的,则
e、 Graphics.DrawImageUnscaled(当前图像,点为空)
如果结束
如果FADEPOLITY>0并且下一个时间段也不是空的,那么
使用FadeImage作为位图=FadeBitmap(下一个页面,_FadePocity)
e、 Graphics.DrawImageUnscaled(FadeImage,Point.Empty)
终端使用
如果结束
端接头
私有子开始转换(ImageName作为字符串)
“在“设置”中,我有一个过渡类型的参数“剪切”或“溶解”。这会将新图像发送到相应的函数中加载
选择案例My.Settings.TransitionType
案例0
转换切(图像名称)
案例1
转换(图像名称)
结束选择
端接头
私有子转换\u剪切(ImageName作为字符串)
'替换了PictureBox1中的图像
CurrentImage=新位图(Image.FromFile(ImageName),Me.PictureBox1.Size)
Me.PictureBox1.Refresh()
端接头
私有子转换(ImageName作为字符串)
'在My.Settings(我的设置)中定义的时间内,在当前图像和新图像之间淡入淡出(毫秒)
NextImage=新位图(Image.FromFile(ImageName),Me.PictureBox1.Size)
“淡入新形象
Dim TickTime作为整数=My.Settings.TransitionTime/100
对于i=0到1,步骤0.01
_FadePocity=CSng(一)
Me.PictureBox1.Invalidate()
等待(滴答声时间)
下一个
等待(100)
CurrentImage=新位图(Image.FromFile(ImageName),Me.PictureBox1.Size)
_FadePocity=CSng(0)
Me.PictureBox1.Invalidate()
端接头
专用子等待(ByVal间隔为整数)
'自定义计时器
调暗stopW作为新秒表
stopW.Start()
停止时执行W.ElapsedMilliseconds<间隔
'允许用户界面保持响应
Application.DoEvents()
环
stopW.Stop()
端接头
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
'在启动时加载默认映像
CurrentImage=新位图(Image.FromFile(My.Settings.DefaultBackgroundImage),Me.PictureBox1.Size)
Me.PictureBox1.Refresh()
'为了便于form1和ApplicationEvents之间的通信,命令行参数已传递到Label6
如果Label6.Text.StartsWith(“/init”),则
'初始化应用程序并从xml设置加载默认背景图像
StartTransition(My.Settings.DefaultBackgroundImage)
ElseIf Label6.Text.StartsWith(“/load”)然后
'检查存在并加载指定的图像
Dim inputArgument As String=“/load=”
Dim inputName As String=“”
如果Label6.Text.StartsWith(inputArgument),则
inputName=Label6.Text.Remove(0,inputArgument.Length)
如果结束
如果inputName=“”,则
'未指定图像名称,请加载未指定图像的图像
开始转换(My.Settings.ImageNotSpecified)
其他的
如果存在My.Computer.FileSystem.files(My.Settings.ImagesFolder&inputName&“.jpg”),则
Public Class Form1

    Dim TickTime As Integer = My.Settings.TransitionTime / 100
    Private _fadeOpacity As Single = 0
    Private CurrentImage As Bitmap
    Private NextImage As Bitmap

    Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
        Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
        opacity = Math.Max(0, Math.Min(opacity, 1.0F))
        Using ia As New Imaging.ImageAttributes
            Dim cm As New Imaging.ColorMatrix
            cm.Matrix33 = opacity
            ia.SetColorMatrix(cm)
            Dim destpoints() As PointF = {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
            Using g As Graphics = Graphics.FromImage(bmp2)
                g.DrawImage(bmp, destpoints,
                New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
            End Using
        End Using
        Return bmp2
    End Function

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If _fadeOpacity < 100 AndAlso CurrentImage IsNot Nothing Then
            e.Graphics.DrawImageUnscaled(CurrentImage, Point.Empty)
        End If
        If _fadeOpacity > 0 AndAlso NextImage IsNot Nothing Then
            Using fadedImage As Bitmap = FadeBitmap(NextImage, _fadeOpacity)
                e.Graphics.DrawImageUnscaled(fadedImage, Point.Empty)
            End Using
        End If
    End Sub

    Private Sub StartTransition(ImageName As String)
        ' In settings I have a parameter for the transition type, CUT or DISSOLVE. This sends the new image to load in the approriate function
        Select Case My.Settings.TransitionType
            Case 0
                Transition_CUT(ImageName)
            Case 1
                Transition_Dissolve(ImageName)
        End Select
    End Sub

    Private Sub Transition_CUT(ImageName As String)
        ' Replaced the image in PictureBox1
        CurrentImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        Me.PictureBox1.Refresh()
    End Sub

    Private Sub Transition_Dissolve(ImageName As String)
        ' Fade between current image and new image over the defined time in My.Settings (in ms)
        NextImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        'fade to newimage
        Dim TickTime As Integer = My.Settings.TransitionTime / 100
        For i = 0 To 1 Step 0.01
            _fadeOpacity = CSng(i)
            Me.PictureBox1.Invalidate()
            wait(TickTime)
        Next
        wait(100)
        CurrentImage = New Bitmap(Image.FromFile(ImageName), Me.PictureBox1.Size)
        _fadeOpacity = CSng(0)
        Me.PictureBox1.Invalidate()
    End Sub

    Private Sub wait(ByVal interval As Integer)
        ' custom timer
        Dim stopW As New Stopwatch
        stopW.Start()
        Do While stopW.ElapsedMilliseconds < interval
            ' Allows UI to remain responsive
            Application.DoEvents()
        Loop
        stopW.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Load default image at startup
        CurrentImage = New Bitmap(Image.FromFile(My.Settings.DefaultBackgroundImage), Me.PictureBox1.Size)
        Me.PictureBox1.Refresh()

        ' For easy communication between form1 and ApplicationEvents, the command line argument has been passed to Label6
        If Label6.Text.StartsWith("/init") Then
            ' Initialize the application and load default background image from xml settings
            StartTransition(My.Settings.DefaultBackgroundImage)
        ElseIf Label6.Text.StartsWith("/load") Then
            ' Check existance and load the specified image
            Dim inputArgument As String = "/load="
            Dim inputName As String = ""
            If Label6.Text.StartsWith(inputArgument) Then
                inputName = Label6.Text.Remove(0, inputArgument.Length)
            End If
            If inputName = "" Then
                ' No image name specified, load the image not specified image
                StartTransition(My.Settings.ImageNotSpecified)
            Else
                If My.Computer.FileSystem.FileExists(My.Settings.ImagesFolder & inputName & ".jpg") Then
                    ' Image has been found and is displayed
                    StartTransition(My.Settings.ImagesFolder & inputName & ".jpg")
                Else
                    ' Image not found, displaying the not found message
                    StartTransition(My.Settings.ImageNotFound)
                End If
            End If
        ElseIf Label6.Text.StartsWith("/quit") Then
            ' Quit application
            Me.Close()
        Else
            ' No parameter sent, showing instructions
        End If
    End If

    End Sub

    Private Function argument() As String
        Throw New NotImplementedException
    End Function

End Class
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Public Shared argument As String
        Private _fadeOpacity As Single = 0
        Private CurrentImage As Bitmap
        Private NextImage As Bitmap

        Private Sub StartTransition(ImageName As String)
            Select Case My.Settings.TransitionType
                Case 0
                    Transition_CUT(ImageName)
                Case 1
                    Transition_Dissolve(ImageName)
            End Select
        End Sub

        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            If _fadeOpacity < 100 AndAlso CurrentImage IsNot Nothing Then
                e.Graphics.DrawImageUnscaled(CurrentImage, Point.Empty)
            End If
            If _fadeOpacity > 0 AndAlso NextImage IsNot Nothing Then
                Using fadedImage As Bitmap = FadeBitmap(NextImage, _fadeOpacity)
                    e.Graphics.DrawImageUnscaled(fadedImage, Point.Empty)
                End Using
            End If
        End Sub

        Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
            Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
            opacity = Math.Max(0, Math.Min(opacity, 1.0F))
            Using ia As New Imaging.ImageAttributes
                Dim cm As New Imaging.ColorMatrix
                cm.Matrix33 = opacity
                ia.SetColorMatrix(cm)
                Dim destpoints() As PointF = {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
                Using g As Graphics = Graphics.FromImage(bmp2)
                    g.DrawImage(bmp, destpoints,
                New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
                End Using
            End Using
            Return bmp2
        End Function

        Private Sub Transition_CUT(ImageName As String)
            CurrentImage = New Bitmap(Image.FromFile(ImageName), Form1.PictureBox1.Size)
            _fadeOpacity = CSng(0)
            Form1.PictureBox1.Invalidate()
        End Sub

        Private Sub Transition_Dissolve(ImageName As String)
            NextImage = New Bitmap(Image.FromFile(ImageName), Form1.PictureBox1.Size)
            'fade to newimage
            Dim TickTime As Integer = CInt(Form1.TextBox_Time.Text) / 100
            For i = 0 To 1 Step 0.01
                _fadeOpacity = CSng(i)
                Form1.PictureBox1.Invalidate()
                wait(TickTime)
            Next
            wait(100)
            CurrentImage = New Bitmap(Image.FromFile(ImageName), Form1.PictureBox1.Size)
            _fadeOpacity = CSng(0)
            Form1.PictureBox1.Invalidate()
        End Sub

        Private Sub wait(ByVal interval As Integer)
            Dim stopW As New Stopwatch
            stopW.Start()
            Do While stopW.ElapsedMilliseconds < interval
                ' Allows your UI to remain responsive
                Application.DoEvents()
            Loop
            stopW.Stop()
        End Sub

        Private Sub MyApplication_Startup(
            ByVal sender As Object,
            ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
        ) Handles Me.Startup

            ' Clean the command line parameter container (label6)
            Form1.Label6.Text = "none"

            For Each s As String In e.CommandLine
                Form1.Label6.Text = s.ToLower
            Next
        End Sub

        Private Sub MyApplication_StartupNextInstance(
            ByVal sender As Object,
            ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs
        ) Handles Me.StartupNextInstance

            For Each s As String In e.CommandLine
                If s.ToLower.StartsWith("/quit") Then
                    'Quit the application
                    Form1.Close()
                ElseIf s.ToLower.StartsWith("/init") Then
                    'Initialize the application and load default background image from xml settings
                    StartTransition(My.Settings.DefaultBackground)
                ElseIf s.ToLower.StartsWith("/load") Then
                    'Check existance and load the specified image
                    Dim inputArgument As String = "/load="
                    Dim inputName As String = ""
                    For Each i As String In e.CommandLine
                        If i.ToLower.StartsWith(inputArgument) Then
                            inputName = i.Remove(0, inputArgument.Length)
                        End If
                    Next
                    If inputName = "" Then
                        ' No image name specified, load the image not specified image
                        StartTransition(My.Settings.ImageNotSpecified)
                    Else
                        If My.Computer.FileSystem.FileExists(My.Settings.ImagesFolder & inputName & ".jpg") Or My.Computer.FileSystem.FileExists(My.Settings.ImagesFolder & inputName & ".swf") Then
                            ' Image has been found and is displayed
                            StartTransition(My.Settings.ImagesFolder & inputName & ".jpg")
                        Else
                            ' Image not found, displaying the not found message
                            StartTransition(My.Settings.ImageNotFound)
                        End If
                    End If
                ElseIf s.ToLower.StartsWith("") Then
                    'No parameter sent, showing instructions
                End If
            Next
        End Sub
    End Class
End Namespace
Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    MainForm.Refresh()
End Sub
Public Sub RefreshPictureBox()
    PictureBox1.Refresh()
End Sub
Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    DirectCast(MainForm, Form1).RefreshPictureBox()
End Sub