» »

Visual Basic - Excel (hook)

Visual Basic - Excel (hook)

vitranc ::

V excelu delam neko aplikacijo in uporabljam formo, ki je nemodalna (torej lahko ob svojem oknu uporabljam tudi druge - naprimer rišem na drawing layer v excelu). Problem pa se pojavi, ker rabim dogodke, kot so leftmouseclick in mouse move, ki pa obstajajo samo za mojo formo, ne obstajajo pa za ostala odprta okna. Ta problem sem rešil z uporabo hook-a (glej primer spodaj). Vendar pa deluje zelo čudno, saj včasih dela, včasih pa ne (kot da se včasih zacikla)?? Naprimer, takrat ko ne deluje moram v debuggerju pritinsiti "break" nato "reset" in potem še enkrat pognati program, potem pa lepo deluje. Ugotovil sem, da se to dogaja samo takrat če uporabljam formo, ki je nemodalna, kar pa obvezno mora biti. Ali mogoče kdo ve, če obstaja kakšen drug način, oz. kaj bi moral uporabiti/spremeniti, da bi pravilno delovalo?

Code:

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Declare Function GetActiveWindow Lib "user32" () As Long

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Public Const HC_ACTION = 0

Public Const WH_MOUSE = 7

Public Const WM_RBUTTONDOWN = 516

Public Const WM_LBUTTONDBLCLK = 515

Public Const WM_MOUSEWHEEL = 522

Public Const WH_GETMESSAGE = 3

Public g_hHook As Long


Public Sub Naredi_Hook()

g_hHook = SetWindowsHookEx(WH_MOUSE, AddressOf MiskaProc, Application.Hinstance, 0)

End Sub


Public Sub OdstraniUnhook()

Call UnhookWindowsHookEx(g_hHook)

End Sub


Public Function MiskaProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If GetActiveWindow = FindWindow("XLMAIN", Application.Caption) Then

If (nCode = HC_ACTION) Then

If wParam = WM_LBUTTONDBLCLK Then Range("A1").Value = "WM_LBUTTONDBLCLK"

If wParam = WM_MOUSEWHEEL Then Range("A2").Value = "WM_MOUSEWHEEL"

End If

End If

MiskaProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)

End Function




LP


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[C#] Prosim pomagajte! Potrebujem program, ki bi pobiral podatke iz ene strani

Oddelek: Programiranje
212746 (2356) David1994
»

Ena za VB 6 majstre ??

Oddelek: Programiranje
5929 (838) StratOS
»

Vb6

Oddelek: Programiranje
101329 (1192) StratOS
»

Odpiranje dat.exe v VB

Oddelek: Programiranje
122989 (2782) webblod
»

Visual basic Progress bar

Oddelek: Programiranje
51561 (1494) Lunik

Več podobnih tem