2016-04-20 13 views
2

Wir verwenden GMA MouseHook für ein ausgeklügeltes Outlook VSTO AddIn. Wegen der Kompliziertheit unseres AddIns haben wir uns bei bestimmten Problemen irgendwie in eine Ecke entwickelt.MouseUp/MouseClick nach MouseDown verhindern

Unser Problem: Wir haben Outlook maximiert und das Mail.Compose-Fenster ist darüber hinaus maximiert. Wenn wir auf die Schließen-Schaltfläche im Mail.Compose-Fenster klicken, rufen wir im MouseDown-Event den eigentlichen Fensterabschluss auf. Jetzt wird das Mail.Compose-Fenster geschlossen und die MouseUp + MouseClick-Ereignisse werden auf die Schließen-Schaltfläche des zugrunde liegenden Outlook-Hauptfensters ausgelöst.

Ich habe versucht, die MouseEventArgs 'e.Handled = True, aber es führt immer noch die MouseUp + MouseClick Ereignisse nach.

Irgendwelche Ideen, wie man das löst?

EDIT Danke mehrdad safa für mich in die richtige Richtung! Hier ist, wie ich mein Problem gelöst:

Zuerst habe ich (zB kopiert und aus dem Internet eingefügt) diese Klasse:

Imports System.Runtime.InteropServices 

Public Class MouseInterceptor 

    Public Delegate Function CallBack(_ 
    ByVal nCode As Integer, _ 
    ByVal wParam As IntPtr, _ 
    ByVal lParam As IntPtr) As Integer 

    'Declare the mouse hook constant. 
    'For other hook types, obtain these values from Winuser.h in Microsoft SDK. 
    Dim WH_MOUSE As Integer = 7 
    Shared hHook As Integer = 0 

    'Keep the reference so that the delegate is not garbage collected. 
    Private hookproc As CallBack 

    'Import for the SetWindowsHookEx function. 
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _ 
    Public Overloads Shared Function SetWindowsHookEx _ 
      (ByVal idHook As Integer, ByVal HookProc As CallBack, _ 
      ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer 
    End Function 

    'Import for the CallNextHookEx function. 
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _ 
    Public Overloads Shared Function CallNextHookEx _ 
      (ByVal idHook As Integer, ByVal nCode As Integer, _ 
      ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer 
    End Function 
    'Import for the UnhookWindowsHookEx function. 
    <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _ 
    Public Overloads Shared Function UnhookWindowsHookEx _ 
       (ByVal idHook As Integer) As Boolean 
    End Function 

    'Point structure declaration. 
    <StructLayout(LayoutKind.Sequential)> Public Structure Point 
     Public x As Integer 
     Public y As Integer 
    End Structure 

    'MouseHookStruct structure declaration. 
    <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct 
     Public pt As Point 
     Public hwnd As Integer 
     Public wHitTestCode As Integer 
     Public dwExtraInfo As Integer 
    End Structure 

    Public Shared Function MouseHookProc(_ 
    ByVal nCode As Integer, _ 
    ByVal wParam As IntPtr, _ 
    ByVal lParam As IntPtr) As Integer 
     Dim MyMouseHookStruct As New MouseHookStruct() 

     If (nCode < 0) Then 
      Return CallNextHookEx(hHook, nCode, wParam, lParam) 
     End If 

     MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct) 

     Return CallNextHookEx(hHook, nCode, wParam, lParam) 

    End Function 

Dann habe ich es nur rufen in meinem MouseDown- Ereignis das Fenster vor der Schließung:

MouseInterceptor.MouseHookProc(0, "0x201", 0) 

der erste param teilt den Haken, wenn das System mit anderen Ereignissen fortgesetzt werden soll, wenn der Wert < 0. Also ist I 0 nach MouseDown- zu stoppen verwenden. Der zweite Parameter bedeutet "Linke Maustaste gedrückt", der Thirs-Parameter zeigt an, ob es sich um einen Einzel- oder Doppelklick handelt. Ich habe es jetzt bei 0 gelassen.

Antwort

1

Sie sollten einen Rückruf für LowLevelMouseProc mit setWindowsHook Funktion, um globale Mausereignis behandeln. Dann können Sie verhindern, dass Mouse-Up-Ereignisse zurück zu Windows und anderen Anwendungen gesendet werden. rufen Sie einfach nicht callNextHook Funktion, um Windows zu verhindern, die Nachricht weiterzuleiten!

Verwendung dieses Beispiel:

class InterceptMouse 

{ 

private static LowLevelMouseProc _proc = HookCallback; 

private static IntPtr _hookID = IntPtr.Zero; 

public static void Main() 

{ 

    _hookID = SetHook(_proc); 

    Application.Run(); 

    UnhookWindowsHookEx(_hookID); 

} 

private static IntPtr SetHook(LowLevelMouseProc proc) 

{ 

    using (Process curProcess = Process.GetCurrentProcess()) 

    using (ProcessModule curModule = curProcess.MainModule) 

    { 

     return SetWindowsHookEx(WH_MOUSE_LL, proc, 

      GetModuleHandle(curModule.ModuleName), 0); 

    } 

} 

private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); 

private static IntPtr HookCallback( 

    int nCode, IntPtr wParam, IntPtr lParam) 

{ 

    if (nCode >= 0 && 

     MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam) 

    { 

     MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); 

     Console.WriteLine(hookStruct.pt.x + “, ” + hookStruct.pt.y); 

    } 

    return CallNextHookEx(_hookID, nCode, wParam, lParam); 

} 

private const int WH_MOUSE_LL = 14; 

private enum MouseMessages 

{ 

    WM_LBUTTONDOWN = 0x0201, 

    WM_LBUTTONUP = 0x0202, 

    WM_MOUSEMOVE = 0x0200, 

    WM_MOUSEWHEEL = 0x020A, 

    WM_RBUTTONDOWN = 0x0204, 

    WM_RBUTTONUP = 0x0205 

} 

[StructLayout(LayoutKind.Sequential)] 

private struct POINT 

{ 

    public int x; 

    public int y; 

} 

[StructLayout(LayoutKind.Sequential)] 

private struct MSLLHOOKSTRUCT 

{ 

    public POINT pt; 

    public uint mouseData; 

    public uint flags; 

    public uint time; 

    public IntPtr dwExtraInfo; 

} 

[DllImport(“user32.dll”, CharSet = CharSet.Auto, SetLastError = true)] 

private static extern IntPtr SetWindowsHookEx(int idHook, 

    LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); 

[DllImport(“user32.dll”, CharSet = CharSet.Auto, SetLastError = true)] 

[return: MarshalAs(UnmanagedType.Bool)] 

private static extern bool UnhookWindowsHookEx(IntPtr hhk); 

[DllImport(“user32.dll”, CharSet = CharSet.Auto, SetLastError = true)] 

private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, 

    IntPtr wParam, IntPtr lParam); 

[DllImport(“kernel32.dll”, CharSet = CharSet.Auto, SetLastError = true)] 

private static extern IntPtr GetModuleHandle(string lpModuleName); 

}

Achtung: Verhindern Maus nach oben Ereignis passiert Mai tritt unerwartet weitere Probleme.

+0

Gern geschehen! Gern geschehen. –