private void StartInternalWork() { Toolkit.TraceWriteLine("특정 스크린 마우스 이동 방지를 시작합니다."); _oldMousePoint.Clear(); _isStartMouseWork = true; Screen primaryScreen = Screen.PrimaryScreen; Screen preventScreen = Screen.AllScreens[_preventMoveScreenIndex]; Point useCurrentPt = Point.Empty; try { while (_isStartMouseWork) { Thread.Sleep(Interval); MousePoint mousePoint = MouseNative.GetCursorPoint(); if (!mousePoint.IsEmpty) { useCurrentPt.X = mousePoint.X; useCurrentPt.Y = mousePoint.Y; _oldMousePoint.Push(useCurrentPt); // 오래된 좌표 삭제 if (_oldMousePoint.Count >= _oldMousePointClear) { Stack <Point> reversePoints = new Stack <Point>(); int count = 0; foreach (Point point in _oldMousePoint.Reverse()) { bool isBreak = (_autoOldMousePointClear) ? !preventScreen.Bounds.Contains(point) : count >= _lessThanOldMousePointClear; if (isBreak) { break; } reversePoints.Push(point); count++; } _oldMousePoint.Clear(); _oldMousePoint = reversePoints; string text = String.Format("오래된 마우스 좌표를 삭제하였습니다. 삭제개수:{0}", count); Toolkit.TraceWriteLine(text); } // 특정 스크린에 좌표가 오기전의 좌표를 찾는다. Point lastPreventScreenOutsidePoint = Point.Empty; foreach (Point point in _oldMousePoint) { if (!preventScreen.Bounds.Contains(point)) { lastPreventScreenOutsidePoint = point; break; } } int x = lastPreventScreenOutsidePoint.X; int y = lastPreventScreenOutsidePoint.Y; // 비어 있을 경우 주 모니터 중앙으로 if (lastPreventScreenOutsidePoint.IsEmpty) { Point primaryCenterPoint = ScreenUtility.GetPrimaryScreenBoundsCenterPoint(); x = primaryCenterPoint.X; y = primaryCenterPoint.Y; } if (preventScreen.Bounds.Contains(useCurrentPt)) { MouseNative mouseNative = new MouseNative(); mouseNative.SetCursorPoint(x, y); mouseNative.MouseEvent(MouseEventFlags.MOVE); mouseNative.SetCursorPoint(x, y); string text = String.Format("{0} 스크린으로 마우스가 넘어갔습니다. 마우스 이동 처리를 시작합니다.", preventScreen.Bounds.ToString()); Toolkit.TraceWriteLine(text); text = String.Format("마우스를 (X={0}, Y={1}) 좌표로 이동을 완료하였습니다.", x, y); Toolkit.TraceWriteLine(text); if (IsSetMouseCursorPointAfterOldPointClear) { _oldMousePoint.Clear(); _oldMousePoint.Push(new Point(x, y)); Toolkit.TraceWrite("기존에 있는 모든 좌표를 삭제하고 마지막 이동한 좌표를 추가합니다."); } } } } } catch (Exception ex) { Toolkit.DebugWriteLine(ex); } }
/// <summary> /// 현재 마우스 좌표를 반환합니다. /// </summary> /// <returns></returns> public static Point GetCursorPoint() { MousePoint mousePoint = MouseNative.GetCursorPoint(); return(new Point(mousePoint.X, mousePoint.Y)); }