我有以下代码:
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事件处理程序和第二部分,当检测到活动时,运行一堆代码并删除处理程序。
除了一点之外,一切正常:
正如你可以看到我有inicio =现在当屏幕保护程序变为活动状态时fim =现在当检测到活动时(当屏幕保护程序变为非活动状态时),所以我应该有2个不同的时间,但如果我拥有它就像我发布了2个日期时间会是一样的。如果你注意到我有一个msgbox显示现在(当屏幕保护程序停止时)评论,如果我把它从评论中删除,2个日期时间将是不同的和正确的(我使用计时器来确保结果)
现在我的问题:
为什么需要更新now
的消息框以及为什么它不能用于debug.print?
有没有办法解决这个问题/更新now
var,而不必使用消息框(我不希望应用程序有弹出消息)
如果我真的必须使用msgbox用于此目的,有没有办法让它不发送弹出窗口或立即自动点击确定它会立即消失?
编辑:
我一直在搜索,我发现了这段代码:
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时屏幕保护程序停止跑步,但它不起作用,任何想法为什么?
答案 0 :(得分:1)
对Application.Idle执行任何操作都是错误的原因。屏幕保护程序激活后,您的应用程序不仅会立即空闲 ,而且在运行时也永远不会停止闲置。屏幕保护程序将活动桌面切换到专用安全桌面,没有任何正在运行的程序将获得任何输入,直到它取消激活。
您可以观察桌面切换,触发SystemEvents.SessionSwitch事件。
请注意这样的代码相当缺乏实用性。好奇心还可以,但总有很多东西值得学习。屏幕保护程序应位于列表的底部。
答案 1 :(得分:0)
首先,我要感谢你们的帮助,就像你说的那样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,因为它不会工作,弹出窗口会显示,它不会停止,因此会出现无数的弹出窗口(是的...我犯了错误: S)
再一次,感谢帮助我,希望它能帮助将来的某个人