Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27212

VS 2010 FindWindow - Using wild cards

$
0
0
Hi All,

I am using a program that when an alert is received it sends a continous beep to the PC system board speaker and also opens up an alert window. The problem is, the server is located far away and inside the chassis of a rear projected screen, so it is very hard to hear. Unfortunatley there is no way setting up the sound to go through external speakers.

I thought I had found my solution using the FindWindow function, so I could play a sound when the alert window opened, but I have discovered that each alert window has a different Caption (class also). Now I could go down the route of finding each alert window Caption, as I have below and setting it up as I have below. BUT there could be 10-12 of these different captions and I don't want to repeat so much code as I'm sure there is an easier way.

I have found that each alert window will always start with an open bracket, for example "(1) 963 - Device specific". I basically want to be able to perform the same task as in the code below (show message and start sound) but instead of looking for an exact caption just look at the first character of the window and if it is ( then do the tasks.


Code:

Imports System.Runtime.InteropServices
Public Class Form1

    ' API
    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
    Private Shared Function FindWindowLike(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    ' form load
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' set timer speed to watch for a specific window
        Timer1.Interval = 5000
        Timer2.Interval = 5000
    End Sub

    ' set timer on/off
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Timer1.Enabled = CheckBox1.Checked
        Timer2.Enabled = CheckBox1.Checked
    End Sub

    ' timer 1
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim hWnd = FindWindowLike(vbNullString, "(1) 963 - Device specific") ' classname, titletext
        ' if returned handle is not zero then window was found!
        If hWnd <> IntPtr.Zero Then
            ' stop timer / reset checkbox
            Timer1.Enabled = False
            CheckBox1.Checked = True

            ' play alert and show message
            My.Computer.Audio.Play("C:\WINDOWS\Media\Notify.Wav", AudioPlayMode.BackgroundLoop)
                Threading.Thread.Sleep(1000)

            ' if messagebox receives ok, stop audio
            Dim result = MessageBox.Show("ALERT RECEIVED, PRESS OK TO STOP SOUND")
            If result = DialogResult.OK Then
                My.Computer.Audio.Stop()
                Timer1.Enabled = True
            End If
        End If
    End Sub

    ' timer 2
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim hWnd = FindWindowLike(vbNullString, "(5) E5 data centre Chiller plant faults") ' classname, titletext
        ' if returned handle is not zero then window was found!
        If hWnd <> IntPtr.Zero Then
            ' stop timer / reset checkbox
            Timer2.Enabled = False
            CheckBox1.Checked = True

            ' play alert and show message
            My.Computer.Audio.Play("C:\WINDOWS\Media\Notify.Wav", AudioPlayMode.BackgroundLoop)
            Threading.Thread.Sleep(1000)

            ' if messagebox receives ok, stop audio
            Dim result = MessageBox.Show("ALERT RECEIVED, PRESS OK TO STOP SOUND")
            If result = DialogResult.OK Then
                My.Computer.Audio.Stop()
                Timer2.Enabled = True
            End If
        End If
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27212


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>