在光标位置打开一个小的浮动窗口 [英] Open a small floating window at cursor position

查看:96
本文介绍了在光标位置打开一个小的浮动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个小的概念证明,要求我听一些按键组合,按下该组合键会在当前光标位置下方打开一个小的 WPF / WinForms 窗口。我更像一个网络专家,因此我很难从此开始。

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?

谢谢。

推荐答案

尝试使用此示例进行WPF。通过按 Enter 键,可以提前显示 Popup 窗口。

Try this example for WPF. By pressing the Enter key displays a Popup window in advance receiving the coordinates of the mouse cursor.

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

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






编辑:更简单的解决方案


An easier solution

您只需设置 Placement = Mouse for Popup 而是接收鼠标的坐标:

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

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

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            PopupWindow.IsOpen = true;
        }
    }
}

这篇关于在光标位置打开一个小的浮动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆