0
public MainWindow()
{
        InitializeComponent();
        _hook = Hook.GlobalEvents();
        _hook.MouseMove += DrawMouseMove;
}
private void DrawMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{

        Point GetMousePos() => Mouse.GetPosition(DrawCanvas);
        Point pos = GetMousePos();
        CoordinateLabel.Content = $"{Math.Ceiling(pos.X)}, {Math.Ceiling(pos.Y)}px";

        //... code down here is draw a shape with pos, a relative position to DrawCanvas 

}

I'm using hook for global event, but i stuck at getting relative position to DrawCanvas when the mouse is outside of my application window. The thing I want to achive is just like mspaint, you can still draw shape even when the mouse moved out of the window

itsncnt
  • 15
  • 4
  • In case you need this only when a mouse button is pressed, simply capture the mouse in a MouseDown event handler. – Clemens Jan 11 '22 at 08:37
  • @itsncnt It is unclear what is your real issue but you can get the screen coordinates of Canvas by `PointToScreen` method and that of cursor by Win32 [GetCursorPos](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos) function any time and then calculate the relative position. – emoacht Jan 11 '22 at 10:32

0 Answers0