IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Hook sur le bouton droit de la souris


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut Hook sur le bouton droit de la souris
    Bonjour;
    Environnement : winforms C# .NET 3.5
    Un client me demande d'interdire le copier coller et l'impression écran lorsque l'application que je dois lui faire est en runtime pour protéger les données.
    J'ai réussi à bloquer les touches clavier impr écran et ctrl par un global hook.
    Mais pas encore réussi à désactiver le bouton droit de la souris.
    Le problème est au niveau de : LowLevelMouseProc et de la struct juste en bas de cette méthode.
    Merci de votre aide.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
     
     
     
    #region
     
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
     
    #endregion
     
    namespace GlobalHook
    {
        public class UserActivityHook
        {
            private const int VmRButton = 0x02;
            private const int VkLControl = 0xA2;
            private const int VkRControl = 0xA3;
            private const int VkControl = 0x11;
            private const int VkSnapshot = 0x2C;
            private const int WhKeyboardLl = 13;
            private const int WhMouseLl = 14;
            private const int WmKeydown = 0x0100;
            private const int WmMousedown =0x0201;
            private static IntPtr kbh_Handle;
            private static IntPtr kbh_Handle1;
     
            private static HookProc KeyboardHookProcedure;
            private static HookProc MouseHookProcedure;     
            private int HMouseHook;        
            private int HKeyboardHook;
     
            #region Windows Function Imports
     
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
                SetLastError = true)]
            private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
     
     
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
                SetLastError = true)]
            public static extern int CallNextHookEx(IntPtr hhk, int nCode, int wParam, IntPtr lParam);
     
     
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
                SetLastError = true)]
            private static extern int UnhookWindowsHookEx(int idHook);
     
     
            private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
     
            #endregion
     
            #region Windows constants
     
            #endregion
     
            ~UserActivityHook()
            {
                //uninstall hooks and do not throw exceptions
                Stop(true, true, false);
     
            }
     
            public void Start()
            {
     
                if (HMouseHook == 0)
                {
                    // Create an instance of HookProc.
                    MouseHookProcedure = new HookProc(LowLevelMouseProc);
                    //install hook
                    HMouseHook = SetWindowsHookEx(WhMouseLl, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                    //If SetWindowsHookEx fails.
                    if (HMouseHook== 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(true, false, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
     
     
     
                // install Keyboard hook only if it is not installed and must be installed
                if (HKeyboardHook == 0)
                {
                    // Create an instance of HookProc.
                    KeyboardHookProcedure = new HookProc(LowLevelKeyboardProc);
                    //install hook
                    HKeyboardHook = SetWindowsHookEx(WhKeyboardLl, KeyboardHookProcedure,
                                                     Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                                                     0);
                    //If SetWindowsHookEx fails.
                    if (HKeyboardHook == 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(false, true, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
            }
     
            public void Stop()
            {
                Stop(true, true, true);
            }
     
     
            public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook, bool throwExceptions)
            {
                if (HMouseHook != 0 && uninstallMouseHook)
                {
                    //uninstall hook
                    int retMouse = UnhookWindowsHookEx(HMouseHook);
                    //reset invalid handle
                    HMouseHook = 0;
                    //if failed and exception must be thrown
                    if (retMouse == 0 && throwExceptions)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
     
                //if keyboard hook set and must be uninstalled
                if (HKeyboardHook != 0 && uninstallKeyboardHook)
                {
                    //uninstall hook
                    int retKeyboard = UnhookWindowsHookEx(HKeyboardHook);
                    //reset invalid handle
                    HKeyboardHook = 0;
                    //if failed and exception must be thrown
                    if (retKeyboard == 0 && throwExceptions)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
            }
     
     
     
     
            private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
            {
                if (nCode < 0)
                {
                    CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
                    return 0;
                }
     
                if (wParam == WmKeydown)
                {
                    IntPtr kbdll = lParam;
                    Kbdllhookstruct kbdllstruct = (Kbdllhookstruct) Marshal.PtrToStructure(kbdll, typeof (Kbdllhookstruct));
     
                    if ((kbdllstruct.VkCode == VkSnapshot) || (kbdllstruct.VkCode == VkControl) || (kbdllstruct.VkCode == VkLControl) || (kbdllstruct.VkCode == VkRControl))
                        return -1;
                }
     
                return CallNextHookEx(kbh_Handle, nCode, wParam, lParam);
            }
     
     
     
     
            private static int LowLevelMouseProc(int nCode, int wParam, IntPtr lParam)
            {
                if (nCode < 0)
                {
                    CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
                    return 0;
                }
     
                if (wParam == WmMousedown)
                {
                    IntPtr msdll = lParam;
                    MouseLlHookStruct msdllstruct = (MouseLlHookStruct)Marshal.PtrToStructure(msdll, typeof(MouseLlHookStruct));
     
                    if (msdllstruct.MouseData == VmRButton)
                        return -1;
                }
     
                return CallNextHookEx(kbh_Handle1, nCode, wParam, lParam);
            }
     
     
            [StructLayout(LayoutKind.Sequential)]
            private struct Point
            {
                public int X;
                public int Y;
            }
     
     
     
            [StructLayout(LayoutKind.Sequential)]
            private struct MouseLlHookStruct
            {
                public Point Point;
                public int MouseData;
                public int Flags;
                public int Time;
                public int ExtraInfo;
            }
     
     
            #region Nested type: Kbdllhookstruct
     
            [StructLayout(LayoutKind.Sequential)]
            public struct Kbdllhookstruct
            {
                public int VkCode;
                public int ScanCode;
                public int Flags;
                public int Time;
                public int ExtraInfo;
            }
     
            #endregion
        }
    }

  2. #2
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Par défaut
    salut

    arrives tu au moins à trapper les messages souris ?

    Car si tu y arrives, il suffira d'arreter la chaine de propagation de l'evenement souris pour que ca arrete le bouton droit

  3. #3
    Membre très actif
    Inscrit en
    Décembre 2006
    Messages
    205
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 205
    Par défaut
    J'ai trouvé la solution: il fallait supprimer entièrement la structure du code lié au mouse proc. La clé également doit être changée de 513 à 516.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Désactivation menu quand clique sur bouton droit de la souris
    Par cath2123 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 05/02/2014, 10h35
  2. Réponses: 4
    Dernier message: 08/09/2006, 04h36
  3. Réponses: 7
    Dernier message: 19/12/2005, 12h27
  4. Réponses: 3
    Dernier message: 05/11/2005, 14h35
  5. Réponses: 3
    Dernier message: 10/02/2005, 17h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo