1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| ' WM_APP is 0x8000
Const WM_APP = (&H8000)
Const WM_ALLREADY = (WM_APP + &H1)
Const WM_KEY = (WM_APP + &H5)
Const WM_AC_STATUS = (WM_APP + &H8)
Const WM_SD_STATUS = (WM_APP + &H9)
Const WM_BRIGHTNESS = (WM_APP + &H103)
' Declare functions for PostMessage
Declare Function FindWindow Lib "Coredll" Alias "FindWindowW" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function PostMessage Lib "Coredll" Alias "PostMessageW" _
(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
' Variables
Dim hMain As Long
Dim hWnd As Long
Dim flag As Boolean
Dim str As String
' VB.NET request delegate to use "AddressOf" operator
' refer http://msdn.microsoft.com/en-us/library/kkasf56d(vs.71).aspx
Public Delegate Function WinProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
' Declare funcstions for receive messages
Public Declare Function SetWindowLong Lib "Coredll" Alias "SetWindowLongW" _
(ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As WinProcDelegate) As Integer
Public Declare Function CallWindowProc Lib "Coredll" _
(ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
' Variables
Const GWL_WNDPROC = (-4)
Dim PrevProc As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim addr As String
Dim addr2 As String
flag = True
' Get App.exe and Main.exe handle
hWnd = FindWindow(vbNullString, "App")
hMain = FindWindow("Main", vbNullString)
'hMain = FindWindow(vbNullString, "MsgTest") ' Test purpose
' Show handle to check
addr = Hex(hWnd)
addr2 = Hex(hMain)
txtBox.Text = "App:" & addr & " Main:" & addr2
' Subclassing
PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WinProc)
End Sub
Private Sub bSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSend.Click
Try
'PostMessage sample function
If flag = True Then
PostMessage(hMain, WM_BRIGHTNESS, 9, 0)
txtBox.Text = "WM_BRIGHTNESS : 9"
flag = False
Else
PostMessage(hMain, WM_BRIGHTNESS, 0, 0)
txtBox.Text = "WM_BRIGHTNESS : 0"
flag = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Function WinProc(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
' Message receive sample
Select Case msg
Case WM_ALLREADY
txtBox.Text = "WM_ALLREADY"
Case WM_KEY
txtBox.Text = "WM_KEY"
Case WM_AC_STATUS
txtBox.Text = "WM_AC_STATUS " & wParam & ", " & lParam
Case WM_SD_STATUS
txtBox.Text = "WM_SD_STATUS " & wParam
End Select
WinProc = CallWindowProc(PrevProc, hwnd, msg, wParam, lParam)
End Function |
Partager