cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010
-
24 avril 2008 à 09:03
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010
-
28 avril 2008 à 10:03
Bonjour je suis débutant en vb.net et je n'arrive pas à avoir le handle de Word pour effectuer un hook.
Je fais Word_hwnd = FindWindow(vbNullString, "Microsoft Word")
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 09:51
Dans mon cas j'ai ca :
Public Function InstallHook() As Boolean
Dim Word_hwnd As Long
Try
If _mouseHook = IntPtr.Zero Then
_mouseProc = New CallBack(AddressOf MouseHookProc)
Word_hwnd = FindWindow(vbNullString, "SEB.doc [mode de compatiblité] - Microsoft Word")
'Word_hwnd = FindWindow("Microsoft Word", vbNullString)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, Word_hwnd, 0)
Dim name As String = GetWndText(Word_hwnd)
MsgBox(name)
End If
Return _mouseHook <> IntPtr.Zero
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
End Try
End Function
J'ai essayé plusieurs possibilités mais je n'ai pas trouvé.
Je travaille avec Word 2007
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 10:05
J'ai changé le Word_hwnd en Integer mais le probléme c'est que Word_hwnd me renvoit 0.
On trouve pas mal de forum ou y mettent IntPtr et ou ca marche mais c'est pas mon cas.
Private Declare Function FindWindowByClass Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 24 avril 2008 à 10:13
testé, ca fonctionne...
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Word_Wnd As Integer
Word_Wnd = FindWindow(vbNullString, "Document1 - Microsoft Word")
If Word_Wnd <> 0 Then
MessageBox.Show("Handle retrouvé : " & Word_Wnd.ToString())
End If
End Sub
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 10:35
En fait je vais expliquer mon probléme :
Je veux récupérer les évènements dans une page word avec du hook donc je fais
Public Structure MSLLHOOKSTRUCT
Public pt As Point
Public mouseData As Integer
Public flags As Integer
Public time As Integer
Public extra As IntPtr
End Structure
Private _mouseHook As IntPtr
Private Const WH_MOUSE_LL As Integer = 14
Private Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack
Public Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Debug.Print("Message = {0}", wParam.ToInt32)
Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
End Function
Public Function InstallHook() As Boolean
If _mouseHook = IntPtr.Zero Then
_mouseProc = New CallBack(AddressOf MouseHookProc)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero)
, 0)
End If
Return _mouseHook <> IntPtr.Zero
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
End Try
End Function
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal
idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef
lParam As MSLLHOOKSTRUCT) As Int32
Ca marche trés bien pour l'écran complet mais impossible de le faire fonctionner pour ma page Word
C'est pour ca que je veux récupérer le handle.
Je récupère bien une valeur non nulle mais ca n'a pas l'air de correspondre avec la fenêtre word ..
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 10:51
Mais comment je peux apprendre des choses sur la structure hierrarchique des fenetres de Word avec Spy++
C'est peut être idiot comme question mais bon .. :S
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 24 avril 2008 à 10:53
ben lance Spy++, cible la fenetre word principale (celle dont tu recherche le handle)
tu regarde ses fenetres filles, tu espionne les messages
et tu regarde si tu cherches a "écouter" la bonne
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 10:54
En fait je veux chopper le clic gauche de la souris via le hook
Et ensuite par exemple je veux cliquer sur un composant que j aurais rajouté via un module externe et voir ses propriétés
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 12:03
Mais apparement ca => Word_hwnd = FindWindow("OpusApp", vbNullString) je crois que ca marche puisque ca me renvoit pas quelque chose de nul donc le problème peut venir du hook peut être ?
Je suis à court d'idées la :S
cs_sylvain230
Messages postés67Date d'inscriptionmardi 5 décembre 2000StatutMembreDernière intervention 5 juillet 2010 24 avril 2008 à 12:18
Ben voila le code mais je n'est pas compris cette histoire de dll externe
Mettre quoi dedans ou la créer, comment la créer ... a quoi elle peut me servir
Comment savoir si mon code permet de faire du hook cliblé sur une fenêtre ?
Je pense pas avoir tout compris sur le hook ...
Faut dire ce morceau de code je l'ai trouvé sur le net et ca marchait impec sans word
J'ai voulu le modifier pour mon code mais ca a pas donné grand chose lol :S
Private Declare Function FindWindowByClass Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Structure MSLLHOOKSTRUCT
Public pt As Point
Public mouseData As Integer
Public flags As Integer
Public time As Integer
Public extra As Integer
End Structure
Private Sub Btn_Formulaire_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Btn_Formulaire.MouseMove
Public Function InstallHook() As Boolean
If _mouseHook = IntPtr.Zero Then
_mouseProc = New CallBack(AddressOf MouseHookProc)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero, 0)
End If
Return _mouseHook <> IntPtr.Zero
End Function
Public Sub RemoveHook()
If _mouseHook = IntPtr.Zero Then Return
UnhookWindowsHookEx(_mouseHook)
_mouseHook = IntPtr.Zero
End Sub
Public Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Integer
Debug.Print("Message = {0}", wParam.ToInt32)
Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
End Function