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 :

Installation hook souris


Sujet :

C#

  1. #1
    Membre à l'essai
    Inscrit en
    Novembre 2010
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Novembre 2010
    Messages : 17
    Points : 10
    Points
    10
    Par défaut Installation hook souris
    bonjour,

    j'ai un problème avec mon hook il ne veux pas s'installer et je ne comprend pas pk?

    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Security;
     
    namespace HookSouris
    {
        public partial class Form1 : Form
        {
     
            #region Win32
     
            [StructLayout(LayoutKind.Sequential)]
            private struct MSLLHOOKSTRUCT
            {
                public int X;
                public int Y;
                public int Data;
                public int Flags;
                public int Time;
                public UIntPtr Extra;
            }
     
            [DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
            private static extern IntPtr SetWindowsHookEx(int hook, HookProc callback, IntPtr module, uint threadID);
     
            [DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
            private static extern bool UnhookWindowsHookEx(IntPtr hHook);
     
            [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity]
            private static extern IntPtr CallNextHookEx(IntPtr hHook, int code, UIntPtr wParam, IntPtr lParam);
     
            private delegate IntPtr HookProc(int code, UIntPtr wParam, IntPtr lParam);
     
            private const int WH_MOUSE_LL = 14;
            private const int HC_ACTION = 0;
     
            #endregion Win32
     
            private IntPtr hHook = IntPtr.Zero;
            private HookProc hookProc = null;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
     
                hookProc = new HookProc(LowLevelMouseProc);
                hHook = SetWindowsHookEx(WH_MOUSE_LL, hookProc, Marshal.GetHINSTANCE(this.GetType().Module), 0);
     
                if (hHook == IntPtr.Zero) // Le hook ne s'installe pas en mode DEBUG.
                    MessageBox.Show("Impossible d'installer le hook.", "Erreur");
            }
     
            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                base.OnFormClosed(e);
     
                if (hHook != IntPtr.Zero)
                {
                    if (!UnhookWindowsHookEx(hHook))
                        MessageBox.Show("Impossible de désinstaller le hook.", "Erreur");
     
                    hHook = IntPtr.Zero;
                    hookProc = null;
                }
            }
     
            private IntPtr LowLevelMouseProc(int code, UIntPtr wParam, IntPtr lParam)
            {
                if (code == HC_ACTION)
                {
                    unsafe
                    {
                        MSLLHOOKSTRUCT* p = (MSLLHOOKSTRUCT*)lParam;
                        Point screenPos = new Point(p->X, p->Y);
                        Point clientPos = this.PointToClient(screenPos);
                        this.Text = screenPos + " - " + clientPos; // Pour l'exemple.
                    }
                }
     
                return CallNextHookEx(hHook, code, wParam, lParam);
            }
     
     
     
            private void Form1_Load(object sender, EventArgs e)
            {
                // L'icône de not_zero est celle de l'application.
                this.notifyIcon1.Icon = this.Icon;
            }
     
            private void btnMessage_Click(object sender, EventArgs e)
            {
                // Icône information de Windows.
                this.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                // Titre du message.
                this.notifyIcon1.BalloonTipTitle = "NotifyZero";
                // Corps du message.
                this.notifyIcon1.BalloonTipText = "Ceci est un message de NotifyZero";
                // On affiche le message indéfiniment.
                this.notifyIcon1.ShowBalloonTip(0);
            }
     
            private void btnErreur_Click(object sender, EventArgs e)
            {
                // Icône information de Windows.
     
                this.notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                // Titre du message.
                this.notifyIcon1.BalloonTipTitle = "NotifyZero";
                // Corps du message.
                this.notifyIcon1.BalloonTipText = "Une erreur est survenue dans NotifyZero";
                // On affiche le message indéfiniment.
                this.notifyIcon1.ShowBalloonTip(9000);
            }
     
            private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // Quitte le programme
                this.Close();
            }
     
            private void btnCacher_Click(object sender, EventArgs e)
            {
                // Cache la winform
                this.Hide();
            }
        }
    }
    si quelqu'un pourait m'aider sa serai simpa

    Merci

    AzevedoSt

  2. #2
    Membre expérimenté Avatar de ctxnop
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2007
    Messages
    858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2007
    Messages : 858
    Points : 1 732
    Points
    1 732
    Par défaut
    Salut,
    Tu pourrais donner plus d'infos tout de même. Un simple "ça marche pas" c'est vague.
    Une exception ? Une ligne particulière à regarder ? etc...

    En attendant, ton delegate HookProc peut être modifié pour avoir delegate IntPtr HookProc(int code, WindowsMessages wParam, [In] MSLLHOOKSTRUCT lParam) ce qui t'évitera probablement de jouer avec du unsafe.

  3. #3
    Membre émérite Avatar de meziantou
    Homme Profil pro
    Ingénieur R&D
    Inscrit en
    Avril 2010
    Messages
    1 223
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Canada

    Informations professionnelles :
    Activité : Ingénieur R&D
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 1 223
    Points : 2 439
    Points
    2 439
    Par défaut
    Tu n'as pas besoin de faire de l'unsafe pour gérer un hook souris.

    Une implémentation parmis tant d'autres:
    http://blogs.msdn.com/b/toub/archive...03/589468.aspx

Discussions similaires

  1. [Vista, Fwk 2.0] Hook souris local ne fonctionnant pas
    Par olsimare dans le forum Windows Forms
    Réponses: 2
    Dernier message: 13/11/2012, 16h28
  2. hook souris et message non envoyer
    Par mapmip dans le forum Access
    Réponses: 1
    Dernier message: 18/01/2008, 23h01
  3. Java natif & hook souris Windows
    Par ®om dans le forum Langage
    Réponses: 1
    Dernier message: 18/07/2006, 22h39
  4. Probleme de Hook souris
    Par mandagor dans le forum MFC
    Réponses: 17
    Dernier message: 07/07/2005, 17h12

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