Intercepting another applications system messages

elitesama

Member
Joined
Dec 14, 2005
Messages
13
Programming Experience
3-5
I am currently developing an application for personal use and I have run into a bit of a problem. The application sits in the system tray and provides a few helpful tools for applications running in the taskbar. The applications gathers the current running applications and any other app that is opened up and creates a few extra items on the system context menu (the menu that pops up when you right click on the taskbar representation of the app) through the use of a few simple API calls. Now I am able to create the menu items just fine but the problem I am having is that I am unable to catch the messages that that specific app throws. Typically, I could just use the WndProc function of the form and catch the m parameter and check those messages, but this is difficult (or i just dont know how to do it) on another app.

I thought perhaps using the GetMessage API, I could retrieve the messages thrown by that app but i dont think i am doing it correctly. If anyone has a good VB.NET example of how to use the GetMessage API or if they have a nice example of how to catch messages from another app, it would be greatly appreciated. I have posted the code I have so far to make the menu and such...

VB.NET:
Public Class frmMain
    Inherits System.Windows.Forms.Form

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function AppendMenu(ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpNewItem As String) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function GetMessage(ByVal lpMsg As oGetMessage, ByVal hWnd As IntPtr, ByVal wMsgFilterMin As Integer, ByVal wMsgFilterMax As Integer) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function CreatePopupMenu() As Integer
    End Function

    Private Const WM_SYSCOMMAND As Integer = &H112

    Private Enum oMenuFlags As Integer
        MF_SEPARATOR = &H800
        MF_STRING = &H0
        MF_POPUP = &H10
        MF_BITMAP = &H4
        MF_GRAYED = &H1
        MF_CHECKED = &H8
        MF_DISABLED = &H2
        MF_MENUBREAK = &H40
        MF_OWNERDRAW = &H100
        MF_BYPOSITION = &H400
        MF_MENUBARBREAK = &H20
    End Enum

    Private Enum oItemHandles As Integer
        ITEM_NULL = 0
        ITEM_TRANSOPAQ = 101
        ITEM_TRANS90 = 102
        ITEM_TRANS80 = 103
        ITEM_TRANS70 = 104
        ITEM_TRANS60 = 105
        ITEM_TRANS50 = 106
        ITEM_TRANS40 = 107
        ITEM_TRANS30 = 108
        ITEM_TRANS20 = 109
        ITEM_TRANS10 = 110
        ITEM_TRANSFULL = 111
    End Enum

    Private Structure oGetMessage
        Dim hWnd As Integer
        Dim message As Integer
        Dim wParam As Integer
        Dim lParam As Integer
        Dim time As Integer
        Dim pt As Point
    End Structure

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

        'Grab API Guide handle and test using it
        Dim iHandle As Integer = FindWindow("ThunderRT5Form", "API-Guide 3.7 - 925 functions found!")
        CreateSystemMenu(New System.IntPtr(iHandle))

    End Sub

'This is easy using WndProc but how do i access another application's "WndProc"
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If (m.Msg = WM_SYSCOMMAND) Then
            Select Case m.WParam.ToInt32()
                Case oItemHandles.ITEM_TRANS10
                    MessageBox.Show("This sets transparency to 10%")
                Case oItemHandles.ITEM_TRANS20
                    MessageBox.Show("This sets transparency to 20%")
                Case Else
                    Exit Select
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub CreateSystemMenu(ByVal hWnd As IntPtr)

        Dim oMenuHandle As Integer = GetSystemMenu(hWnd, False)
        Dim oTransSubMenu As Integer = CreatePopupMenu()

        AppendMenu(oMenuHandle, oMenuFlags.MF_SEPARATOR, 0, String.Empty)
        AppendMenu(oMenuHandle, oMenuFlags.MF_POPUP, oTransSubMenu, "Transparency")

        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANSOPAQ, "Opaque")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS90, "90%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS80, "80%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS70, "70%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS60, "60%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS50, "50%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS40, "40%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS30, "30%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS20, "20%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANS10, "10%")
        AppendMenu(oTransSubMenu, oMenuFlags.MF_STRING, oItemHandles.ITEM_TRANSFULL, "Transparent")

    End Sub
End Class

Note: If you try to use this code, you must have the API guide open or nothing will happen. Perhaps if you wanted to play around and you don't have the API guide, open up spy++ and change the line:
VB.NET:
Dim iHandle As Integer = FindWindow("ThunderRT5Form", "API-Guide 3.7 - 925 functions found!")
...so that the first parameter of the FindWindow function is the classname and the second is the text in the window title bar
 
Last edited:
Back
Top