public void Desactiver()
 {
     if (_resolutionChanged)
     {
         DisplayManager.SetDisplaySettings(_originalDisplaySettings);
     }
     Task.Factory.StartNew(() =>
     {
         if (_isFullscreenGregware && !_isGentool)
         {
             _globalHook.MouseMoveExt -= GlobalHookMouseMoveExt;
         }
         if (_isScrollGregware)
         {
             _globalHook.KeyDown -= _globalHook_KeyDown;
             _globalHook.KeyUp   -= _globalHook_KeyUp;
         }
         _globalHook.Dispose();
     }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);
     _enabled = false;
 }
        public void Activer()
        {
            // Initialisation
            _enabled = true;

            // Trouver le process
            Process process = Process.GetProcessesByName("game.dat").FirstOrDefault();

            if (process == null)
            {
                throw new Exception("Problème d'exécution du jeu, si cette erreur persiste essayer de désactiver les options Gregware (fullscreen et scroll)");
            }

            if (_isFullscreenGregware)
            {
                // Changer resolution
                if (_originalDisplaySettings.Width != _width || _originalDisplaySettings.Height != _height)
                {
                    DisplaySettings newDs = new DisplaySettings
                    {
                        BitCount    = _originalDisplaySettings.BitCount,
                        Frequency   = _originalDisplaySettings.Frequency,
                        Index       = _originalDisplaySettings.Index,
                        Orientation = _originalDisplaySettings.Orientation,
                        Width       = _width,
                        Height      = _height
                    };
                    DisplayManager.SetDisplaySettings(newDs);
                    _resolutionChanged = true;
                }

                // Attendre
                if (_width == 800)
                {
                    // si width de 800 on ne peut pas différencier splash screen du jeu, on attend 10 secondes bêtement...
                    Thread.Sleep(10000);
                }
                else
                {
                    // sinon on peut différencier, on attend juste ce qu'il faut - 10 sec max
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    Rect rect = new Rect();
                    GetWindowRect(process.MainWindowHandle, ref rect);
                    while (rect.Right - rect.Left != _width && sw.Elapsed.Milliseconds < 10000)
                    {
                        Thread.Sleep(100);
                        GetWindowRect(process.MainWindowHandle, ref rect);
                    }
                }

                // No top most
                SetWindowPos(process.MainWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);

                // Replacer la fenêtre
                MoveWindow(process.MainWindowHandle, 0, 0, _width, _height, true);
            }

            // Focus
            Task.Factory.StartNew(() =>
            {
                while (_enabled)
                {
                    Thread.Sleep(200);
                    _activateHook = GetForegroundWindow() == process.MainWindowHandle;
                }
            });

            // Hook
            Task.Factory.StartNew(() =>
            {
                _globalHook = Hook.GlobalEvents();
                if (_isFullscreenGregware && !_isGentool)
                {
                    _globalHook.MouseMoveExt += GlobalHookMouseMoveExt;
                }
                if (_isScrollGregware)
                {
                    _globalHook.KeyDown += _globalHook_KeyDown;
                    _globalHook.KeyUp   += _globalHook_KeyUp;
                }
            }, CancellationToken.None, TaskCreationOptions.None, _uiScheduler);

            // Scroll
            Task.Factory.StartNew(() =>
            {
                while (_enabled)
                {
                    // Simuler en envoyant des pressions de touche du clavier
                    Thread.Sleep(50);

                    // Cumuler les actions de souris et clavier.
                    EdgeZone edgeZoneCumul;
                    if (!_activateHook)
                    {
                        edgeZoneCumul = EdgeZone.NONE;
                    }
                    else
                    {
                        if (_edgeZoneKeyboard == _edgeZoneMouse)
                        {
                            edgeZoneCumul = _edgeZoneKeyboard;                                      // Tout va bien, on est d'accord
                        }
                        else if (_edgeZoneKeyboard == EdgeZone.NONE)
                        {
                            edgeZoneCumul = _edgeZoneMouse;                                          // Que la souris en scroll
                        }
                        else if (_edgeZoneMouse == EdgeZone.NONE)
                        {
                            edgeZoneCumul = _edgeZoneKeyboard;                                       // Que le clavier en scroll
                        }
                        else
                        {
                            edgeZoneCumul = EdgeZone.NONE;  // Il y a conflit, on ne fait rien
                        }
                    }

                    // Envoyer les keys
                    switch (edgeZoneCumul)
                    {
                    case EdgeZone.NONE:
                        // Toutes les touches UP
                        if (_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, false);
                        }
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        break;

                    case EdgeZone.L:
                        // Left et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, true);
                        }

                        // les autre UP
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        break;

                    case EdgeZone.R:
                        // Right et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, true);
                        }

                        // les autre UP
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        break;

                    case EdgeZone.T:
                        // Top et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, true);
                        }

                        // les autre UP
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        break;

                    case EdgeZone.B:
                        // Bottom et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, true);
                        }

                        // les autre UP
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        break;

                    case EdgeZone.LT:
                        // Left, Top et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, true);
                        }
                        if (!_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, true);
                        }

                        // les autre UP
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        break;

                    case EdgeZone.LB:
                        // Left, Bottom et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, true);
                        }
                        if (!_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, true);
                        }

                        // les autre UP
                        if (_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        break;

                    case EdgeZone.RT:
                        // Right, Top et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, true);
                        }
                        if (!_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, true);
                        }

                        // les autre UP
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, false);
                        }
                        break;

                    case EdgeZone.RB:
                        // Right, Bottom et Alt DOWN
                        if (!_isDownAlt)
                        {
                            SendKey(KEYS_DX.ALT, true);
                        }
                        if (!_isDownBottom)
                        {
                            SendKey(KEYS_DX.BOTTOM, true);
                        }
                        if (!_isDownRight)
                        {
                            SendKey(KEYS_DX.RIGHT, true);
                        }

                        // les autre UP
                        if (_isDownLeft)
                        {
                            SendKey(KEYS_DX.LEFT, false);
                        }
                        if (_isDownTop)
                        {
                            SendKey(KEYS_DX.UP, false);
                        }
                        break;
                    }
                }
            });
        }