Solved

Can I wake up a computer from screen saver with a VB executable?

Posted on 2004-10-22
705 Views
Last Modified: 2008-02-01
I have a VB executable that notifies me of new email with a large flashing image.  Is there anyway to have the VB wake up the computer once it goes into the screen saver (of course I mean when the executable is run)?  I want the screen saver to kick in as normal so that teh screen does not burn in, but I would like for the executable to return the screen to the desktop and the flashing image so that I will see it.

If not with VB then maybe something else?
0
Question by:murryc
    20 Comments
     
    LVL 4

    Expert Comment

    by:AjithJose
    You have to find the screensaver window and post a wm_close message to it.

    Check out this URL:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q140723

    Or Try the API

    PostMessage (GetForegroundWindow(), WM_CLOSE, 0, 0)

    from VB if you are not running NT

    0
     
    LVL 8

    Expert Comment

    by:List244
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type

    Private Sub MousePos(ByRef X As Long, ByRef Y As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    X = Mouse.X
    Y = Mouse.Y
    End Sub

    Private Sub SetX(X As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    Mouse.X = X
    RealMousePos Mouse
    mouse_event &H1 Or &H8000, Mouse.X, Mouse.Y, 0, 0
    End Sub

    Private Sub RealMousePos(ByRef Point As POINTAPI)
    Point.X = Point.X * (65536 / GetSystemMetrics(0))
    Point.Y = Point.Y * (65536 / GetSystemMetrics(1))
    End Sub

    I would use this, then when you get new mail do:

    Dim X as long,Y as long
    MousePos X,Y
    SetX x+5

    This will send mouse movement. The reason I
    suggest this is, if the screensaver is not active,
    it will not close a different program (the other
    method will). Also, such little mouse movement
    will not cause troubles if screensavers not active.
    Otherwise you need to MAKE SURE that the
    screensaver is active, so you are not closing
    the wrong program.
    0
     
    LVL 28

    Expert Comment

    by:vinnyd79
    How about simulating a keypress:

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const VK_S = &H53&

    Private Sub Command1_Click()
        keybd_event VK_S, 0, 0, 0 ' Press S
        keybd_event VK_S, 0, KEYEVENTF_KEYUP, 0 ' Release it
    End Sub


    0
     
    LVL 8

    Expert Comment

    by:List244
    If you use Vinny's method, I would suggest taking out
    the Sleep, seeing how it is unused.
    0
     

    Author Comment

    by:murryc
    I have increased the points to 500 from 250 because I need a little more help with implementing these solutions.  I am okay with either mouse movement or keypress so I will award points to whoever can add code to the already working VB code below and test it working to kill the screensaver.  I have very little experience with VB so I am unable to get either to work.  The code below is someones else's work and it works perfectly but I need to add the screen saver solution to it. Thanks.

    VERSION 5.00
    Begin VB.Form frmMain
       BorderStyle     =   3  'Fixed Dialog
       Caption         =   "Message Notification"
       ClientHeight    =   3120
       ClientLeft      =   45
       ClientTop       =   435
       ClientWidth     =   7065
       LinkTopic       =   "Form1"
       MaxButton       =   0   'False
       MinButton       =   0   'False
       ScaleHeight     =   208
       ScaleMode       =   3  'Pixel
       ScaleWidth      =   471
       ShowInTaskbar   =   0   'False
       StartUpPosition =   2  'CenterScreen
       Begin VB.Timer Timer1
          Enabled         =   0   'False
          Interval        =   1000
          Left            =   120
          Top             =   2520
       End
       Begin VB.CommandButton btnClose
          Appearance      =   0  'Flat
          Caption         =   "Close"
          Default         =   -1  'True
          Height          =   375
          Left            =   5760
          TabIndex        =   1
          Top             =   2640
          Width           =   1095
       End
       Begin VB.Label lblMessage
          Alignment       =   2  'Center
          Appearance      =   0  'Flat
          BackColor       =   &H80000005&
          Caption         =   "URGENT New message for Backup Guard"
          BeginProperty Font
             Name            =   "MS Sans Serif"
             Size            =   13.5
             Charset         =   0
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H80000008&
          Height          =   375
          Left            =   240
          TabIndex        =   0
          Top             =   1200
          Width           =   6495
       End
    End
    Attribute VB_Name = "frmMain"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    Dim arrParams As Variant, _
        lngRootColor As Long, _
        lngFlashColor As Long

    Private Sub Form_Load()
        Dim strArguments As String, _
            intParamCount As Integer, _
            varTemp As Variant
        lngRootColor = frmMain.BackColor
        lngFlashColor = vbRed
        strArguments = Command()
        arrParams = Split(strArguments, "/", , vbTextCompare)
        For intParamCount = 0 To UBound(arrParams)
            varTemp = Trim(Mid(arrParams(intParamCount), 3))
            Select Case LCase(Left(arrParams(intParamCount), 1))
                Case "a"    'Account parameter
                    lblMessage.Caption = lblMessage.Caption & " " & varTemp
                Case "c"    'Root color parameter
                    lngFlashColor = Val(varTemp)
                Case "h"    'Height of the dialog-box parameter
                    If IsNumeric(varTemp) Then
                        frmMain.Height = (Int(varTemp) * Screen.TwipsPerPixelX)
                    End If
                Case "w"    'Width of the dialog-box parameter
                    If IsNumeric(varTemp) Then
                        frmMain.Width = (Int(varTemp) * Screen.TwipsPerPixelY)
                    End If
                Case "s"    'Speed parameter
                    If IsNumeric(varTemp) Then
                        Timer1.Interval = CLng(varTemp)
                    End If
            End Select
        Next
        btnClose.Top = frmMain.ScaleHeight - (btnClose.Height + 25)
        btnClose.Left = frmMain.ScaleWidth - (btnClose.Width + 25)
        lblMessage.Top = (frmMain.ScaleHeight - lblMessage.Height) / 2
        lblMessage.Left = (frmMain.ScaleWidth - lblMessage.Width) / 2
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Timer()
        Static bolRoot As Boolean
        frmMain.BackColor = IIf(bolRoot, lngRootColor, lngFlashColor)
        bolRoot = Not bolRoot
        DoEvents
    End Sub

    0
     
    LVL 8

    Expert Comment

    by:List244
    Are you using Visual Basic or Wordpad???
    Is the vb EXE that tells you you have mail
    something you have code for , or a seperate
    program?
    0
     

    Author Comment

    by:murryc
    I am using Visual Basic to compile the code I provided.  I use another program to fire the exe that the visual basic creates.  All of the process is working okay, I just need to be able to see the flashing message when the screen saver is running.  I just paste the completed code into notepad and name *.frm then open in VB.
    0
     
    LVL 8

    Expert Comment

    by:List244
    Okay for the code I provided:

    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type

    Private Sub MousePos(ByRef X As Long, ByRef Y As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    X = Mouse.X
    Y = Mouse.Y
    End Sub

    Private Sub SetX(X As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    Mouse.X = X
    RealMousePos Mouse
    mouse_event &H1 Or &H8000, Mouse.X, Mouse.Y, 0, 0
    End Sub

    Private Sub RealMousePos(ByRef Point As POINTAPI)
    Point.X = Point.X * (65536 / GetSystemMetrics(0))
    Point.Y = Point.Y * (65536 / GetSystemMetrics(1))
    End Sub


    Paste the above in the declarations under General.
    Then paste the following code where you need the
    screensaver disabled:

    Dim X as long,Y as long
    MousePos X,Y
    SetX x+5

    (In an exisiting sub of course)
    0
     

    Author Comment

    by:murryc
    please excuse my ignorance in Visual Basic.  I am unable to understand where to place your code.  I want to execute your mouse movement at the end of the process.  Can you please insert all of your code in the correct place within the code I have provided.  I will then create a frm file with that code and compile it.  Thank you again.
    0
     
    LVL 8

    Assisted Solution

    by:List244
    VERSION 5.00
    Begin VB.Form frmMain
       BorderStyle     =   3  'Fixed Dialog
       Caption         =   "Message Notification"
       ClientHeight    =   3120
       ClientLeft      =   45
       ClientTop       =   435
       ClientWidth     =   7065
       LinkTopic       =   "Form1"
       MaxButton       =   0   'False
       MinButton       =   0   'False
       ScaleHeight     =   208
       ScaleMode       =   3  'Pixel
       ScaleWidth      =   471
       ShowInTaskbar   =   0   'False
       StartUpPosition =   2  'CenterScreen
       Begin VB.Timer Timer1
          Enabled         =   0   'False
          Interval        =   1000
          Left            =   120
          Top             =   2520
       End
       Begin VB.CommandButton btnClose
          Appearance      =   0  'Flat
          Caption         =   "Close"
          Default         =   -1  'True
          Height          =   375
          Left            =   5760
          TabIndex        =   1
          Top             =   2640
          Width           =   1095
       End
       Begin VB.Label lblMessage
          Alignment       =   2  'Center
          Appearance      =   0  'Flat
          BackColor       =   &H80000005&
          Caption         =   "URGENT New message for Backup Guard"
          BeginProperty Font
             Name            =   "MS Sans Serif"
             Size            =   13.5
             Charset         =   0
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          ForeColor       =   &H80000008&
          Height          =   375
          Left            =   240
          TabIndex        =   0
          Top             =   1200
          Width           =   6495
       End
    End
    Attribute VB_Name = "frmMain"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    Dim arrParams As Variant, _
        lngRootColor As Long, _
        lngFlashColor As Long

    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type

    Private Sub MousePos(ByRef X As Long, ByRef Y As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    X = Mouse.X
    Y = Mouse.Y
    End Sub

    Private Sub SetX(X As Long)
    Dim Mouse As POINTAPI
    GetCursorPos Mouse
    Mouse.X = X
    RealMousePos Mouse
    mouse_event &H1 Or &H8000, Mouse.X, Mouse.Y, 0, 0
    End Sub

    Private Sub RealMousePos(ByRef Point As POINTAPI)
    Point.X = Point.X * (65536 / GetSystemMetrics(0))
    Point.Y = Point.Y * (65536 / GetSystemMetrics(1))
    End Sub
    Private Sub Form_Load()
        Dim strArguments As String, _
            intParamCount As Integer, _
            varTemp As Variant
        lngRootColor = frmMain.BackColor
        lngFlashColor = vbRed
        strArguments = Command()
        arrParams = Split(strArguments, "/", , vbTextCompare)
        For intParamCount = 0 To UBound(arrParams)
            varTemp = Trim(Mid(arrParams(intParamCount), 3))
            Select Case LCase(Left(arrParams(intParamCount), 1))
                Case "a"    'Account parameter
                    lblMessage.Caption = lblMessage.Caption & " " & varTemp
                Case "c"    'Root color parameter
                    lngFlashColor = Val(varTemp)
                Case "h"    'Height of the dialog-box parameter
                    If IsNumeric(varTemp) Then
                        frmMain.Height = (Int(varTemp) * Screen.TwipsPerPixelX)
                    End If
                Case "w"    'Width of the dialog-box parameter
                    If IsNumeric(varTemp) Then
                        frmMain.Width = (Int(varTemp) * Screen.TwipsPerPixelY)
                    End If
                Case "s"    'Speed parameter
                    If IsNumeric(varTemp) Then
                        Timer1.Interval = CLng(varTemp)
                    End If
            End Select
        Next
        btnClose.Top = frmMain.ScaleHeight - (btnClose.Height + 25)
        btnClose.Left = frmMain.ScaleWidth - (btnClose.Width + 25)
        lblMessage.Top = (frmMain.ScaleHeight - lblMessage.Height) / 2
        lblMessage.Left = (frmMain.ScaleWidth - lblMessage.Width) / 2
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Timer()
        Static bolRoot As Boolean
        frmMain.BackColor = IIf(bolRoot, lngRootColor, lngFlashColor)
        bolRoot = Not bolRoot
        DoEvents
    End Sub


    Then wherever it should be turning off the screensaver
    (Im confused on where you want that to happen) you
    would put:

    Dim X as long,Y as long
    MousePos X,Y
    SetX x+5

    (Where you want it to disable the screensaver though,
    I am confused, so it's up to you to place the LAST
    bit of code) The rest was placed for you.
    0
     

    Author Comment

    by:murryc
    it works...I would also like to create a seperate exe with just your code that I can run by itself.  I thought I could do that simply and it works but it creates a small prompt that I have to close manually.  Can you assemble code for that too and include a way to make sure it closes any windows it opens.

    Again, I just want seperate code of your solution that I can compile by itself without any of my stuff but I want it to move the mouse then exit completely.
    0
     
    LVL 17

    Expert Comment

    by:zzzzzooc
    Only need to use the below to move the mouse 10 "mickeys" to the right:

    Call mouse_event(&H1, 10, 0, 0, 0)

    :P
    0
     

    Author Comment

    by:murryc
    zzzzzzooc...

    I have no experience with Visual Basic, can you make me a complete VB code that I can compile with VB into an exe that will move the mouse as you describe?  I create an FRM file in notepad with the source code then open with VB and create the exe.
    0
     
    LVL 17

    Expert Comment

    by:zzzzzooc
    Comment was directed towards List244. It was his suggestion so I'm not going to "muscle in" after his time spent helping. Just trying to make things simpler.
    0
     
    LVL 8

    Expert Comment

    by:List244
    MurryC, I told you put the line wherever it should be disabling the screensaver.
    From looking at the code, it appears to me there is no spot where it is to
    disable the screensaver. On form load would make no sense, and your
    timer.... Has no Ifs.. So, really I don't quite see what you are doing
    here. I think you have a seperate program that tells you if you have
    mail, and you are trying to write an add-on that makes it disable the
    screensaver as well, and well... With your experience in VB... That
    is going to be rough, even if one of us GAVE you a solution... Sorry.

    However, maybe that program launches yours when you get new mail?
    If so, put the mouse-move code on form load. Otherwise... Put it in
    the timer, and watch your mouse go crazy as long as your program
    is running. Sorry I couldn't help more.
    0
     
    LVL 8

    Expert Comment

    by:List244
    Zzzzzooc If you happen to understand his problem
    any better than I apparently do, go right ahead and
    jump in, I've helped all I can.
    0
     

    Author Comment

    by:murryc
    List244.. the overall solution with my existing coding works great, inlcuding what you added for me.  It would be hard for me to explain what it all does for me and how I run it, but I do use a batch file that runs the executable with additional switch information.  All I want now is just to have a VB executable that does nothing but moves the mouse when executed.  I implemented your coding into my exiting code and placed the line where I wanted it and it wakes up the computer when the exe executes.  I would now like to just have your code by itself in an executable that if run does nothing but wakes the computer from the screen saver.  I tried creating the FRM file with just your code and it works but it also opens a small window prompt that has to be closed.  I am sure it can just run and close all on its own, I just do not know how to do it.

    Is that a better explaination?
    0
     
    LVL 17

    Accepted Solution

    by:
    >>I am sure it can just run and close all on its own, I just do not know how to do it.

    1.) Create a new project.

    2.) Remove Form1 (right-click on it in the list and choose "Remove Form1").

    3.) Add a new Module (Project -> Add Module)

    4.) Copy and paste the below into the new Module (Module1):

    Option Explicit

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Sub Main()
        Call mouse_event(&H1, 10, 0, 0, 0)
    End Sub

    5.) Compile it. When it's run, it'll move the mouse cursor 10 mickeys to remove the screensaver and then exit immediately. However, you're in trouble if the screensaver has a password. ;)
    0
     

    Author Comment

    by:murryc
    thanks to both list244 and zzzzzooc for the solutions..they both work great...majority of points to List244 for providing main solution...others to zzzzzooc for following up with additional request
    0
     

    Author Comment

    by:murryc
    list224 was supposed to get the accepted answer and zzzzzooc was supposed to get assisted but I reversed them by accident..points where split correctly though..thanks again
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now