我正在写一个小的概念证明,要求我听一些按键组合,按下该组合键会WPF/WinForms
在当前光标位置下方打开一个小窗口。我更像一个网络专家,因此我在开始时遇到了麻烦。
谁能指出我正确的方向?或提供一些资源/示例?
谢谢。
对于WPF,请尝试以下示例。通过按键,Enter可以Popup
预先显示一个窗口,以接收鼠标光标的坐标。
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
您可以设置Placement="Mouse"
为Popup
接收鼠标坐标,而不是:
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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句