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
|
#region Théorème du bazooka tueur de fourmi
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback,
IntPtr hMod, uint dwThreadId);
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
public const int SWP_NOSIZE = 0x1;
public const int SWP_NOZORDER = 0x4;
public const int SWP_NOACTIVATE = 0x10;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left_, int top_, int right_, int bottom_)
{
Left = left_;
Top = top_;
Right = right_;
Bottom = bottom_;
}
public int Height { get { return Bottom - Top; } }
public int Width { get { return Right - Left; } }
public Size Size { get { return new Size(Width, Height); } }
public Point Location { get { return new Point(Left, Top); } }
// Handy method for converting to a System.Drawing.Rectangle
public Rectangle ToRectangle()
{ return Rectangle.FromLTRB(Left, Top, Right, Bottom); }
public static RECT FromRectangle(Rectangle rectangle)
{
return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
}
public override int GetHashCode()
{
return Left ^ ((Top << 13) | (Top >> 0x13))
^ ((Width << 0x1a) | (Width >> 6))
^ ((Height << 7) | (Height >> 0x19));
}
#region Operator overloads
public static implicit operator Rectangle(RECT rect)
{
return rect.ToRectangle();
}
public static implicit operator RECT(Rectangle rect)
{
return FromRectangle(rect);
}
#endregion
}
IntPtr hHook = IntPtr.Zero;
Point newPosition = new Point(5, 5);
int HookFunction(int code, IntPtr wParam, IntPtr lParam)
{
const int HCBT_ACTIVATE = 5;
if (code == HCBT_ACTIVATE)
{
RECT rect;
GetWindowRect(wParam, out rect);
SetWindowPos(wParam, IntPtr.Zero, newPosition.X, newPosition.Y, rect.Width, rect.Height,
SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
UnhookWindowsHookEx(hHook);
hHook = IntPtr.Zero;
}
return 0;
}
#endregion
#region Fonction affichant la boite de message
private void ShowPositionnedMessageBox(string text, string titre, MessageBoxButtons messageBoxButtons, MessageBoxIcon messageBoxIcon, int x, int y)
{
newPosition = new Point(x, y);
hHook = IntPtr.Zero;
hHook = SetWindowsHookEx(HookType.WH_CBT, new HookProc(HookFunction), GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), GetCurrentThreadId());
MessageBox.Show(text, titre, messageBoxButtons, messageBoxIcon);
}
#endregion
private void button4_Click(object sender, EventArgs e)
{
ShowPositionnedMessageBox("Test de message", "Titre", MessageBoxButtons.OK, MessageBoxIcon.Information, 15, 15);
} |
Partager