wpf 在光标位置打开一个小的浮动窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22742549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:23:32  来源:igfitidea点击:

Open a small floating window at cursor position

c#wpfwinformswindowcursor

提问by JanivZ

I'm writing a small proof of concept that requires me to listen to some key combination that when pressed opens a small WPF/WinFormswindow underneath the current cursor position. I'm more of a web guy so I'm having trouble starting with this.

我正在编写一个小的概念证明,需要我听一些组合键,当按下该组合键时WPF/WinForms,会在当前光标位置下方打开一个小窗口。我更像是一个网络人,所以我在开始时遇到了麻烦。

Can anyone point me in the right direction? Or provide some resources/examples?

任何人都可以指出我正确的方向吗?或者提供一些资源/例子?

Thanks.

谢谢。

回答by Anatoliy Nikolaev

Try this example for WPF. By pressing the Enterkey displays a Popupwindow in advance receiving the coordinates of the mouse cursor.

试试这个 WPF 的例子。通过Enter按键显示一个Popup窗口,预先接收鼠标光标的坐标。

XAML

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

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

Edit: An easier solution

You can just set Placement="Mouse"for Popupinstead receive coordinates of the mouse:

您可以只设置Placement="Mouse"forPopup而不是接收鼠标的坐标:

XAML

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

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;
        }
    }
}