private void MouseMoveHandler(object sender, EventArgs e)
        {
            if (!smoothingOn)
            {
                overlay.cursorPos = MouseHook.GetCursorPosition();
                overlay.Invalidate();
            }

            if (config.smoothOnDraw && !isDrawing && MouseHook.moveEnabled)
            {
                overlay.cursorPos = MouseHook.GetCursorPosition();
                overlay.Invalidate();
            }
        }
        // Line smoothing
        private void LineSmoothingUpdate(object sender, ElapsedEventArgs e)
        {
            Point guidePos = position;

            if (lastPosition == guidePos)
            {
                mouseMoving = false;
            }
            else
            {
                mouseMoving = true;
            }
            lastPosition = guidePos;

            try {
                // Begin smoothing only if we have points to work with and if drawing
                if (smoothPoints.Count > 0 && isDrawing)
                {
                    if (config.disableCatchUp)
                    {
                        if (mouseMoving)
                        {
                            MouseHook.SetCursorPos(smoothPoints[0].X, smoothPoints[0].Y);
                            smoothPoints.RemoveAt(0);
                        }
                    }
                    else
                    {
                        MouseHook.SetCursorPos(smoothPoints[0].X, smoothPoints[0].Y);
                        smoothPoints.RemoveAt(0);
                    }
                }
            }
            catch {
                // Fail smoothing gracefully
            }

            if (!isDrawing)
            {
                smoothPoints.Clear();
                lineSmoothingTimer.Stop();
                if (!config.snapToCursor)
                {
                    MouseHook.SetCursorPos(guidePos.X, guidePos.Y);
                }
                MouseHook.moveEnabled = true;
                MouseHook.downEnabled = true;
            }
        }
 // Mouse event handling
 private void MouseDownHandler(object sender, EventArgs e)
 {
     if (smoothingOn)
     {
         if (config.smoothOnDraw && !isDrawing)
         {
             linePoints.Clear();
             smoothPoints.Clear();
             MouseHook.moveEnabled = false;
             Point p = MouseHook.GetCursorPosition();
             smoothPoints.Add(p);
             linePoints.Add(p);
             linePoints.Add(p);
             linePoints.Add(p);
             position  = p;
             isDrawing = true;
             lineProcessingTimer.Start();
             lineSmoothingTimer.Start();
         }
     }
 }
        public Main()
        {
            InitializeComponent();

            // Initialize the config file
            config = new Config(this, overlay);

            // Overlay setup
            overlay.Show();
            overlay.TopMost = true;
            overlay.Bounds  = Screen.AllScreens[0].Bounds;
            button_colorDialog.BackColor = overlay.cursorColor;

            // Attempt to load the config file, if any
            config.LoadConfig();

            // Low level mouse hook (MouseHook.cs)
            MouseHook.Start();
            MouseHook.MouseDownHooked += new EventHandler(MouseDownHandler);
            MouseHook.MouseUpHooked   += new EventHandler(MouseUpHandler);
            MouseHook.MouseMoveHooked += new EventHandler(MouseMoveHandler);

            // Mouse smoothing updater
            lineSmoothingTimer.Elapsed += new ElapsedEventHandler(LineSmoothingUpdate);
            lineSmoothingTimer.Interval = 5;

            // Line processing updater
            lineProcessingTimer.Elapsed += new ElapsedEventHandler(LineProcessingUpdate);
            lineProcessingTimer.Interval = config.smoothingStrength;

            // Register a raw input listener
            int size = Marshal.SizeOf(typeof(RawInputDevice));

            RawInputDevice[] devices = new RawInputDevice[1];
            devices[0].UsagePage = 1;
            devices[0].Usage     = 2;
            devices[0].Flags     = 0x00000100;
            devices[0].Target    = Handle;
            RegisterRawInputDevices(devices, 1, size);
        }
 private void MouseUpHandler(object sender, EventArgs e)
 {
     if (smoothingOn)
     {
         if (config.smoothOnDraw && isDrawing)
         {
             MouseHook.downEnabled = false;
             isDrawing             = false;
             lineProcessingTimer.Stop();
             linePoints.Clear();
             if (!config.snapToCursor)
             {
                 Point guidePos = overlay.cursorPos;
                 MouseHook.SetCursorPos(guidePos.X, guidePos.Y);
             }
             else
             {
                 overlay.cursorPos = MouseHook.GetCursorPosition();
             }
         }
     }
 }