Open a small floating window at cursor position
c#, mouse-cursor, window, winforms, wpf
Solution
Try this example for WPF. By pressing the Enter key displays a `Popup` window in advance receiving the coordinates of the mouse cursor.
`XAML`
<Window x:Class="OpenWindowForCursor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen"
PreviewKeyDown="Window_PreviewKeyDown">
<Grid>
<Popup Name="PopupWindow"
Placement="Relative"
IsOpen="False"
StaysOpen="False">
<Border Width="100"
Height="100"
Background="AntiqueWhite">
<Label Content="Test" />
</Border>
</Popup>
</Grid>
</Window>
`Code-behind`
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
PopupWindow.IsOpen = true;
var point = Mouse.GetPosition(Application.Current.MainWindow);
PopupWindow.HorizontalOffset = point.X;
PopupWindow.VerticalOffset = point.Y;
}
}
}
`Edit: An easier solution`
You can just set `Placement="Mouse"` for `Popup` instead receive coordinates of the mouse:
`XAML`
<Grid>
<Popup Name="PopupWindow"
Placement="Mouse"
IsOpen="False"
StaysOpen="False">
<Border Width="100"
Height="100"
Background="AntiqueWhite">
<Label Content="Test" />
</Border>
</Popup>
</Grid>
`Code-behind`
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
PopupWindow.IsOpen = true;
}
}
}
Problem
I'm writing a small proof of concept that requires me to listen to some key combination that when pressed opens a small `WPF/WinForms` window underneath the current cursor position. I'm more of a web guy so I'm having trouble starting with this. Can anyone point me in the right direction? Or provide some resources/examples? Thanks.