2

我正在构建一个简单的WPF应用程序。我有一个透明的最大化 Window和一个Canvas(canvas1)。

我想将鼠标位置移入canvas1或移入MainWindow(在这种情况下是同一件事)。

为此,我使用以下代码:

Point p = Mouse.GetPosition(canvas1); //and then I have p.X and p.Y

此代码适用于不透明的 Canvas. 问题是我有一个透明 Canvas的,这段代码不起作用......(它没有给我错误,但坐标是p.X = 0p.Y = 0)。

我该如何解决这个问题?

4

2 回答 2

2

一种可能的解决方法是使用GetCursorPosWin32 函数:

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out System.Drawing.Point lpPoint); 

如果要在 WPF 中使用坐标,则必须将坐标从像素转换为点。

使用示例:

System.Drawing.Point point;
if(!GetCursorPos(out point))
    throw new InvalidOperationException("GetCursorPos failed");
// point contains cursor's position in screen coordinates.
于 2012-07-31T10:53:09.090 回答
1

C#

    using System.Windows;
    using System.Windows.Input;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System;
    using System.Windows.Threading;
    namespace Test
    {
        public partial class MainWindow : Window
        {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool GetCursorPos(ref Win32Point pt);

            [StructLayout(LayoutKind.Sequential)]
            internal struct Win32Point
            {
                public Int32 X;
                public Int32 Y;
            };
            public static Point GetMousePosition()
            {
                Win32Point w32Mouse = new Win32Point();
                GetCursorPos(ref w32Mouse);
                return new Point(w32Mouse.X, w32Mouse.Y);
            }

            private double screenWidth;
            private double screenHeight;

            public MainWindow()
            {
                InitializeComponent();
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
                screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;

                this.Width = screenWidth;
                this.Height = screenHeight;
                this.Top = 0;
                this.Left = 0;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }

            void timer_Tick(object sender, EventArgs e)
            {
                var mouseLocation = GetMousePosition();
                elipse1.Margin = new Thickness(mouseLocation.X, mouseLocation.Y, screenWidth - mouseLocation.X - elipse1.Width, screenHeight - mouseLocation.Y- elipse1.Height);
            }
        }
    }

XAML

    <Window x:Class="Test.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" 
            WindowStyle="None" 
            Topmost="True" 
            AllowsTransparency="True" 
            Background="Transparent"
            ShowInTaskbar="False"
            Loaded="Window_Loaded">
        <Grid>
                <Ellipse Width="20" Height="20" Fill="Red" Name="elipse1"/>
        </Grid>
    </Window>

解决了!但迟到了:(

于 2012-07-31T10:44:56.503 回答