Skip to content

Instantly share code, notes, and snippets.

@SchreinerK
Created December 2, 2022 10:09
Show Gist options
  • Save SchreinerK/54436a7d5cbc596bca8afabad26fc53c to your computer and use it in GitHub Desktop.
Save SchreinerK/54436a7d5cbc596bca8afabad26fc53c to your computer and use it in GitHub Desktop.
Show Window on Mouse Position
public partial class PopupWindow : Window {
public PopupWindow() {
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.Manual;
Loaded += (s, e) => { // HACK1: use Loaded
Mouse.Capture(this); // HACK2: use Capture
var p = PointToScreen(Mouse.GetPosition(this));
Mouse.Capture(null);
var source = PresentationSource.FromVisual(this);
p = new Point( // HACK2: use Transform
p.X / source.CompositionTarget.TransformToDevice.M11,
p.Y / source.CompositionTarget.TransformToDevice.M22
);
Left = p.X - (ActualWidth / 2);
Top = p.Y - 16;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment