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
| //Région de déclaration des variables, inutile de vous l'afficher
//Fin de la région
public Form1()
{
InitializeComponent();
USBObject = new USBClass();
SystemEvents.SessionEnded += new SessionEndedEventHandler(Form1_Close);
SystemEvents.SessionEnding += new SessionEndingEventHandler(Form1_Close);
Application.ApplicationExit += new EventHandler(Form1_Close);
// Create an instance of HookProc.
MouseHookProcedure = new HookProc(Form1.MouseHookProc);
SetWindowsHookEx(WH_MOUSE_LL,MouseHookProcedure,(IntPtr)0,0);
SetWindowsHookEx(WH_KEYBOARD_LL, MouseHookProcedure, (IntPtr)0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
attemptUSBConnectionFrontEnd();
}
void attemptUSBConnectionFrontEnd()
{
USBObject.connectionState = USBObject.attemptUSBConnection();
if (USBObject.connectionState == USBClass.CONNECTION_SUCCESSFUL){
tmrUSB.Enabled = true;
hidConnect(Handle);hid_connect = true;
}
else if (USBObject.connectionState == USBClass.CONNECTION_NOT_SUCCESSFUL){
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
tmrUSB.Enabled = false;
}
}
private void tmrUSB_Tick(object sender, EventArgs e)
{
USBObject.fromHostToDeviceBuffer[2] = PTX;
if (avant_veille == true)
{USBObject.fromHostToDeviceBuffer[1] = BEFORE_SUSPEND;}
else if (apres_veille == true)
{USBObject.fromHostToDeviceBuffer[1] = AFTER_SUSPEND;}
USBObject.sendViaUSB();
}
private void Form1_Close(object sender, EventArgs e)
{
USBObject.fromHostToDeviceBuffer[1] = SHUTDOWN;
if (USBObject.connectionState == USBClass.CONNECTION_SUCCESSFUL)
{USBObject.sendViaUSB();}
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
UnhookWindowsHookEx(WH_KEYBOARD_LL);
UnhookWindowsHookEx(WH_MOUSE_LL);
SystemEvents.SessionEnded -= new SessionEndedEventHandler(Form1_Close);
SystemEvents.SessionEnding -= new SessionEndingEventHandler(Form1_Close);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_POWERBROADCAST:
switch (m.WParam.ToInt32())
{
//value passed when system is going on standby / suspended
case PBT_APMQUERYSUSPEND:
apres_veille = false;
avant_veille = true;
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
break;
//value passed when system Suspend Failed
case PBT_APMQUERYSUSPENDFAILED:
apres_veille = false;
avant_veille = true;
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
break;
//value passed when system is suspended
case PBT_APMSUSPEND:
apres_veille = false;
avant_veille = true;
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
break;
case PBT_APMRESUMESUSPEND:
avant_veille = false;
apres_veille = true;
if (hid_connect == false) { hidConnect(Handle); hid_connect = true; }
break;
case PBT_APMRESUMEAUTOMATIC:
avant_veille = false;
apres_veille = true;
if (hid_connect == false) { hidConnect(Handle); hid_connect = true; }
break;
default: break;
}
break;
case WM_HID_EVENT:
switch (m.WParam.ToInt32())
{
case NOTIFY_PLUGGED :
DevHandle = m.LParam; // handle of HID device in this message
if (hidGetVendorID(DevHandle) == pVendorID && hidGetProductID(DevHandle) == pProductID)
{
attemptUSBConnectionFrontEnd();
}
break;
case NOTIFY_UNPLUGGED :
DevHandle = m.LParam; // handle of HID device in this message
if (hidGetVendorID(DevHandle) == pVendorID && hidGetProductID(DevHandle) == pProductID)
{
tmrUSB.Enabled = false;
if (hid_connect == true) { hidDisconnect(); hid_connect = false; }
}
break;
case NOTIFY_CHANGED:
DevHandle = hidGetHandle(pVendorID, pProductID);
hidSetReadNotify(DevHandle, true);
break;
case NOTIFY_READ:
DevHandle = m.LParam; // handle of HID device in this message
if (hidGetVendorID(DevHandle) == pVendorID && hidGetProductID(DevHandle) == pProductID)
{
USBObject.receiveViaUSB();
if (USBObject.fromDeviceToHostBuffer[1] == 0x50)
{
Eco_Motion_Main.StartInfo = Eco_Motion_Main_StartInfo;
Eco_Motion_Main.Start();
}
}
break;
default: break;
}
break;
case WM_DEVICECHANGE:
switch (m.WParam.ToInt32())
{
//value passed when system detect arrival device
case DBT_DEVNODES_CHANGED:
attemptUSBConnectionFrontEnd();
break;
}
break;
default: break;
}
base.WndProc(ref m);
}
#region HOOK;
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//Marshall the data from the callback.
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
//Create a string variable that shows the current mouse coordinates.
PTX = (byte)MyMouseHookStruct.pt.x;
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
#endregion;
} //end class
} //en namespace |
Partager