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
|
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.Diagnostics;
namespace WinPleinEcran
{
public partial class FormRWIN : Form
{
// Structure contain information about low-level keyboard input event
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public Keys key;
public int scanCode;
public int flags;
public int time;
public IntPtr extra;
}
//System level functions to be used for hook and unhook keyboard input
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
//Declaring Global objects
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;
public FormRWIN()
{
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule; //Get Current Module
objKeyboardProcess = new LowLevelKeyboardProc(captureKey); //Assign callback function each time keyboard process
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0); //Setting Hook of Keyboard Process for current module
InitializeComponent();
// ajuste les props
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = true;
TopMost = true;
Location = new Point(0, 0);
this.WindowState = FormWindowState.Maximized;
this.KeyPreview = true;
this.BackColor = Color.Violet;
// set tooltip
this.toolTip1.IsBalloon = true;
this.toolTip1.ShowAlways = true;
this.toolTip1.SetToolTip(this, "esc ou space bar pour Quitter");
// ajuste les handlers pour quitter l'application
this.KeyPress += new KeyPressEventHandler(FormRWIN_KeyPress);
// HANDLER A DES FINS DE DEMONSTRATION
this.KeyDown += new KeyEventHandler(FormRWIN_KeyDown);
// cet event est necessaire pour positionner correctement le bouton Reduire (minimizer)
this.SizeChanged += new EventHandler(FormRWIN_SizeChanged);
}
private void btnReduire_Click(object sender, EventArgs e)
{
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
void FormRWIN_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Application.Exit();
else if (e.KeyChar == (char)Keys.Space)
Application.Exit();
}
void FormRWIN_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("touche WinLogo est desactivee");
if (e.KeyCode == Keys.RWin)
{
MessageBox.Show("Ce message est envoye aux oubliettes");
}
}
void FormRWIN_SizeChanged(object sender, EventArgs e)
{
btnReduire.Location = new Point(this.Right - 100, this.Bottom - 100);
}
private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys
{
return (IntPtr)1;
}
}
return CallNextHookEx(ptrHook, nCode, wp, lp);
}
}
} |
Partager