在鼠标位置(鼠标左上方)显示WPF窗口的最佳方法是什么? [英] What is the best way to show a WPF window at the mouse location (to the top left of the mouse)?

查看:518
本文介绍了在鼠标位置(鼠标左上方)显示WPF窗口的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现通过继承Windows Forms鼠标指针并减去窗口的高度和宽度以设置左侧和顶部(因为我的窗口的大小是固定的),这在部分时间内是可行的:

I have found that this works PART of the time by inheriting the Windows Forms mouse point and subtracting out the height and width of my window to set the left and top (since my window's size is fixed):

MyWindowObjectThatInheritsWindow window = new MyWindowObjectThatInheritsWindow();
System.Windows.Point mouseLocation = GetMousePositionWindowsForms();
window.Left = mouseLocation.X - 300;
window.Top = mouseLocation.Y - 240;
window.Show();

这是获取鼠标位置的代码...

Here is the code for getting the mouse position...

public System.Windows.Point GetMousePositionWindowsForms()
{
    System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
    return new System.Windows.Point(point.X, point.Y);
}

请注意,这可以通过使窗口的右下边缘触摸鼠标光标的左上角来实现.但是,这对于不同的屏幕分辨率还是对具有不同分辨率的多台显示器来说都无法解决?我还没有完全缩小范围,但是我只是在另一台PC上尝试了相同的代码,它似乎不是在鼠标光标的左上角而是在它的左下角以及很远的距离处生成了该窗口.过去...

Note that this works by making the bottom right edge of the window touch the top left of your mouse cursor. But this breaks for different screen resolutions, or maybe multiple monitors with different resolutiosn? I haven't fully narrowed it down yet, but I just tried this same code on another PC, and it seems to spawn the window not to the top left of the mouse cursor, but to the bottom left of it, and a good distance past it...

我可能应该将窗口大小添加到内容,宽度和高度中,所以我不能仅使用ActualWidth和ActualHeight属性,因为它们不可用.也许问题在于正确调整大小?有什么办法吗?我已经确定我的主PC上的300和240是正确的,并且有两台运行1920x1080分辨率的显示器,因为我已经计算出窗口中所有对象的宽度和高度,这些对象的宽度和高度已经明确确定了尺寸.只是尝试明确地将高度和宽度设置为240/300,以确保窗口的大小不再适合内容,并且减去实际的高度和宽度时,我仍然遇到这个问题!

I should probably add that my window sizes to content, width and height, so I can't just use the ActualWidth and ActualHeight properties since they're not available. Perhaps the issue is in getting that sizing right? Is there any way to do that? I know for sure the 300 and 240 is correct according to my main PC with two monitors running 1920x1080 resolutions, as I have calculated the widths and heights of all the objects in my window which I have explicitly sized. Just tried explicitly setting the height and width to 240/300, to ensure that the window is no longer sized to content, and I still have this issue when subtracting out the actual height and width!

有什么想法吗?

推荐答案

最后,这成功了:

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            MoveBottomRightEdgeOfWindowToMousePosition();
        }

        private void MoveBottomRightEdgeOfWindowToMousePosition()
        {
            var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
            var mouse = transform.Transform(GetMousePosition());
            Left = mouse.X - ActualWidth;
            Top = mouse.Y - ActualHeight;
        }

        public System.Windows.Point GetMousePosition()
        {
            System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
            return new System.Windows.Point(point.X, point.Y);
        }

这篇关于在鼠标位置(鼠标左上方)显示WPF窗口的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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