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();
}
}
} |
Partager