public static IntPoint GetCursorPos() { Win32Point position = new Win32Point(0, 0); NativeMethods.GetCursorPos(ref position); return(new IntPoint(position.x, position.y)); }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (this.CaptureMouse()) { this.mouseDownLoc = NativeMethods.GetCursorPos(); this.Bounds = new IntRect(this.mouseDownLoc.X, this.mouseDownLoc.Y, 0, 0); this.Cursor = Cursors.None; e.Handled = true; } }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (this.IsMouseCaptured) { IntPoint mousePosition = NativeMethods.GetCursorPos(); IntPoint topLeft = new IntPoint(Math.Min(mousePosition.X, this.mouseDownLoc.X), Math.Min(mousePosition.Y, this.mouseDownLoc.Y)); IntPoint bottomRight = new IntPoint(Math.Max(mousePosition.X, this.mouseDownLoc.X), Math.Max(mousePosition.Y, this.mouseDownLoc.Y)); this.Bounds = new IntRect(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); } }
private void Update(bool force) { IntPoint mousePt = NativeMethods.GetCursorPos(); if (Keyboard.IsKeyDown(Key.Space)) { } if (force || mousePt != this.previousPoint) { this.previousPoint = mousePt; IntRect rect = ScreenCoordinates.ExpandPoint(mousePt, this.screenshot); if (!rect.IsEmpty) { this.UpdateBounds(rect, mousePt); } } }
protected override void OnKeyDown(KeyEventArgs e) { switch (e.Key) { case Key.Left: { IntPoint cursorPos = NativeMethods.GetCursorPos(); cursorPos.X -= 1; NativeMethods.SetCursorPos(cursorPos); } e.Handled = true; break; case Key.Right: { IntPoint cursorPos = NativeMethods.GetCursorPos(); cursorPos.X += 1; NativeMethods.SetCursorPos(cursorPos); } e.Handled = true; break; case Key.Up: { IntPoint cursorPos = NativeMethods.GetCursorPos(); cursorPos.Y -= 1; NativeMethods.SetCursorPos(cursorPos); } e.Handled = true; break; case Key.Down: { IntPoint cursorPos = NativeMethods.GetCursorPos(); cursorPos.Y += 1; NativeMethods.SetCursorPos(cursorPos); } e.Handled = true; break; } base.OnKeyDown(e); }
private void HandleTick(object sender, EventArgs e) { IntPoint mousePoint = NativeMethods.GetCursorPos(); if (mousePoint == this.lastMousePoint && DateTime.Now - this.lastCapture < TimeSpan.FromSeconds(.2)) { return; } this.lastCapture = DateTime.Now; this.lastMousePoint = mousePoint; this.MouseX.Text = string.Format(@"X: {0}", mousePoint.X - this.basePoint.X); this.MouseY.Text = string.Format(@"Y: {0}", mousePoint.Y - this.basePoint.Y); if (this.isPaused) { return; } double width = this.Image.ActualWidth / this.Scale; double height = this.Image.ActualHeight / this.Scale; double left = (mousePoint.X - width / 2).Clamp(ScreenShot.FullScreenBounds.Left, ScreenShot.FullScreenBounds.Width - width); double top = (mousePoint.Y - height / 2).Clamp(ScreenShot.FullScreenBounds.Top, ScreenShot.FullScreenBounds.Height - height); double deltaX = left - (mousePoint.X - width / 2); double deltaY = top - (mousePoint.Y - height / 2); if (deltaX != 0) { this.CenterX.Width = new GridLength((this.Image.ActualWidth / 2 - deltaX * this.Scale) + 2 * this.Scale); } else { this.CenterX.Width = new GridLength(this.Image.ActualWidth / 2 + 8); } if (deltaY != 0) { this.CenterY.Height = new GridLength((this.Image.ActualHeight / 2 - deltaY * this.Scale) + 2 * this.Scale); } else { this.CenterY.Height = new GridLength(this.Image.ActualHeight / 2 + 8); } IntRect rect = new IntRect((int)left, (int)top, (int)width, (int)height); ScreenShot screenShot = new ScreenShot(rect); FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = screenShot.Image; newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); this.Image.Source = newFormatedBitmapSource; if (width == 0 || height == 0) { return; } uint centerPixel = (uint)screenShot.GetScreenPixel((int)mousePoint.X, (int)mousePoint.Y); centerPixel = centerPixel | 0xFF000000; byte r = (byte)((centerPixel >> 16) & 0xFF); byte g = (byte)((centerPixel >> 8) & 0xFF); byte b = (byte)((centerPixel >> 0) & 0xFF); Brush brush = new SolidColorBrush(Color.FromRgb(r, g, b)); this.ColorSwatch.Fill = brush; uint centerPixelX = (uint)screenShot.GetScreenPixel((int)mousePoint.X + 90, (int)mousePoint.Y + 90); centerPixel = centerPixelX | 0xFF000000; byte rX = (byte)((centerPixelX >> 16) & 0xFF); byte gX = (byte)((centerPixelX >> 8) & 0xFF); byte bX = (byte)((centerPixelX >> 0) & 0xFF); Brush brushX = new SolidColorBrush(Color.FromRgb(rX, gX, bX)); this.ColorSwatchX.Fill = brushX; uint centerPixelY = (uint)screenShot.GetScreenPixel((int)mousePoint.X - 90, (int)mousePoint.Y - 90); centerPixel = centerPixelY | 0xFF000000; byte rY = (byte)((centerPixelY >> 16) & 0xFF); byte gY = (byte)((centerPixelY >> 8) & 0xFF); byte bY = (byte)((centerPixelY >> 0) & 0xFF); Brush brushY = new SolidColorBrush(Color.FromRgb(rY, gY, bY)); this.ColorSwatchY.Fill = brushY; this.PixelColor.Text = "HEX: " + string.Format(@"#{0:X8}", centerPixel); PixelRGBColor.Text = "RGB: " + r + ", " + g + ", " + b; float _R = (r / 255f); float _G = (g / 255f); float _B = (b / 255f); float _Min = Math.Min(Math.Min(_R, _G), _B); float _Max = Math.Max(Math.Max(_R, _G), _B); float _Delta = _Max - _Min; float H = 0; float S = 0; float L = (float)((_Max + _Min) / 2.0f); if (_Delta != 0) { if (L < 0.5f) { S = (float)(_Delta / (_Max + _Min)); } else { S = (float)(_Delta / (2.0f - _Max - _Min)); } if (_R == _Max) { H = (_G - _B) / _Delta; } else if (_G == _Max) { H = 2f + (_B - _R) / _Delta; } else if (_B == _Max) { H = 4f + (_R - _G) / _Delta; } } PixelHSLColor.Text = "HSL: " + Math.Round(H * 100) + ", " + Math.Round(S * 100).ToString() + "%, " + Math.Round(L * 100).ToString() + "%"; }
private void SetBasePointExecuted(object sender, ExecutedRoutedEventArgs e) { this.basePoint = NativeMethods.GetCursorPos(); }
private void HandleTick(object sender, EventArgs e) { IntPoint mousePoint = NativeMethods.GetCursorPos(); if (mousePoint == lastMousePoint && DateTime.Now - lastCapture < TimeSpan.FromSeconds(.2)) { return; } lastCapture = DateTime.Now; lastMousePoint = mousePoint; MouseX.Text = string.Format(@"X: {0}", mousePoint.X - basePoint.X); MouseY.Text = string.Format(@"Y: {0}", mousePoint.Y - basePoint.Y); if (isPaused) { return; } double width = Image.ActualWidth / Scale; double height = Image.ActualHeight / Scale; double left = (mousePoint.X - width / 2).Clamp(ScreenShot.FullScreenBounds.Left, ScreenShot.FullScreenBounds.Width - width); double top = (mousePoint.Y - height / 2).Clamp(ScreenShot.FullScreenBounds.Top, ScreenShot.FullScreenBounds.Height - height); double deltaX = left - (mousePoint.X - width / 2); double deltaY = top - (mousePoint.Y - height / 2); if (deltaX != 0) { CenterX.Width = new GridLength((Image.ActualWidth / 2 - deltaX * Scale) + 2 * Scale); } else { CenterX.Width = new GridLength(Image.ActualWidth / 2 + 8); } if (deltaY != 0) { CenterY.Height = new GridLength((Image.ActualHeight / 2 - deltaY * Scale) + 2 * Scale); } else { CenterY.Height = new GridLength(Image.ActualHeight / 2 + 8); } IntRect rect = new IntRect((int)left, (int)top, (int)width, (int)height); ScreenShot screenShot = new ScreenShot(rect); FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = screenShot.Image; newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); Image.Source = newFormatedBitmapSource; if (width == 0 || height == 0) { return; } uint centerPixel = (uint)screenShot.GetScreenPixel((int)mousePoint.X, (int)mousePoint.Y); centerPixel = centerPixel | 0xFF000000; byte r = (byte)((centerPixel >> 16) & 0xFF); byte g = (byte)((centerPixel >> 8) & 0xFF); byte b = (byte)((centerPixel >> 0) & 0xFF); Brush brush = new SolidColorBrush(Color.FromRgb(r, g, b)); ColorSwatch.Fill = brush; PixelColor.Text = string.Format(@"#{0:X8}", centerPixel); }