[visual-studio]Eventhandler"bug"与 windows 窗体一起使用 VB.NET

发布时间: 2014/2/27 1:52:11
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有以下代码:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    Const WM_SYSCOMMAND As Integer = &H112
    Const SC_SCREENSAVE As Integer = &HF140

    MyBase.WndProc(m)
    If bloqueado = 0 Then
        If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then
            Timer2.Start()
            inicio = Now
            pausa = pausa + 1
            AddHandler Application.Idle, AddressOf Application_Idle
        End If
    End If
End Sub

Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
    Dim newitem As ListViewItem
    Dim diferença As TimeSpan

    'MsgBox(Now.ToString)'
    Debug.Print(Now.ToString)
    fim = Now
    diferença = fim - inicio
    Timer2.Stop()
    newitem = New ListViewItem
    newitem.Text = pausa
    newitem.SubItems.Add(inicio.ToLongTimeString)
    newitem.SubItems.Add(fim.ToLongTimeString)
    newitem.SubItems.Add(diferença.ToString.Substring(0, 8))
    ListView1.Items.Add(newitem)
    parcial = parcial & pausa & vbTab & vbTab & inicio.ToLongTimeString & vbTab & vbTab & fim.ToLongTimeString _
         & vbTab & vbTab & diferença.ToString.Substring(0, 8) & vbTab & vbTab & "   screensaver" & System.Environment.NewLine
    RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub

基本上第一部分检测时屏幕保护程序激活,并创建一个 application.idle 事件处理程序和第二部分,当一堆代码运行时检测到活动和移除的处理程序。

它是所有工程罚款只有一点:

正如你可以看到我有属于 = 现在当屏幕保护程序成为活跃和 fim = 现在当检测到活动 (当屏幕保护程序变为非活动状态),所以我应该有不同的 2 倍,但如果我有它喜欢我贴 2 日期时间将会是相同的。如果你注意到了我有显示现在的 msgbox (当屏幕保护程序停止) 在评论中,如果我拿出注释 2 datetimes 将不同和更正 (我使用 cronometer,以确保结果的)

现在我的问题: 为什么它需要的 messagebox now 进行更新和它为什么不工作它 debug.print?

有没有办法解决这个的问题更新 now var,而不必使用 messagebox (我也不会像有弹出消息的应用程序)

如果真的有必要为此目的使用 msgbox 有没有方法为它不发送弹出窗口或到 autoclick ok 右所以它消失后立即吗?

编辑:

我一直在寻求和发现了此代码:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Function IsSNRunning() As Boolean
        IsSNRunning = (FindWindow("WindowsScreenSaverClass", vbNullString) <> 0)
    End Function

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        If IsSNRunning() Then
            'Screen Saver Is Running
        Else
            Timer3.Stop()
            code
        End If
End Sub

我用 Timer3.Start() 的时候为什么捕获的屏幕保护程序,我的想法的如果我启动计时器,当我知道屏幕保护程序如果上,然后当我到 IsSNRunning 为 false 时屏幕保护程序停止运行,但它不会工作,任何的想法开始的部分呢?

解决方法 1:

首先我会感谢你们的帮助,就像你说的 application.idle 不工作,与你的帮助我有此解决方案我 VB:

Imports System
Imports Microsoft.Win32
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SystemParametersInfo(uAction As UInteger, _
    uParam As UInteger, ByRef lpvParam As Boolean, fWinIni As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' Check if the screensaver is busy running.'
Public Shared Function IsScreensaverRunning() As Boolean
    Const SPI_GETSCREENSAVERRUNNING As Integer = 114
    Dim isRunning As Boolean = False

    If Not SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, isRunning, 0) Then
        ' Could not detect screen saver status...'
        Return False
    End If

    If isRunning Then
        ' Screen saver is ON.'
        Return True
    End If

    ' Screen saver is OFF.'
    Return False
End Function


Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    Const WM_SYSCOMMAND As Integer = &H112
    Const SC_SCREENSAVE As Integer = &HF140

    MyBase.WndProc(m)
    If bloqueado = 0 Then
        If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then
            Timer2.Start()
            Timer3.Enabled = True
            Timer3.Start()
            'here we that that the screensaver started running so we start a timer'
        End If
    End If
End Sub

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick

    If IsScreensaverRunning() Then
        'Screen Saver Is Running'
    Else
        Timer3.Stop()
        Timer3.Enabled = False
        'Screen Saver Is not Running'
    End If

End Sub

因为计时器只能启动时屏幕保护程序正在运行,我们知道当你 timer3.stop 是屏幕保护程序停止运行时运行

重要的是,不要把 msgbox 之前计时器停止,因为它不会工作,弹出窗口会显示,因此无数弹出窗口将会显示不会停止 (yes......我犯下这个错误: S)

再次,谢谢你对我的帮助和希望它在未来将会帮助别人

赞助商