WPF中获取鼠标相对于屏幕的位置
2007-08-27 16:48
381 查看
WPF中获取鼠标相对于屏幕的位置
周银辉
WPF编程时,我们经常使用Mouse.GetPosition(IInputElement relativeTo)或MouseEventArgs.GetPosition(IInputElement relativeTo)来获取鼠标相对于某一界面元素的相对位置,这自然也联想到另外的一个问题:如何获取鼠标相对于屏幕的位置。 在WPF文档中找了半天也没找到。
但我们别忘了 System.Windows.Forms.Control类中有一个静态属性System.Windows.Forms.Control.MousePosition,我们可以在WPF中继续使用它,当然这需要你的项目添加 System.Windows.Forms和 System.Drawing名字空间,这多少让人有些别扭,因为玩WPF时总有点不喜欢调用System.Windows.Forms下的东西,喜新厌旧吧。
另外一个方法是平台调用,在VB的文档中可以找到一个API:Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long,我们可以在C#中使用它,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Samples
{
class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);
}
}
OK,问题解决了:)
周银辉
WPF编程时,我们经常使用Mouse.GetPosition(IInputElement relativeTo)或MouseEventArgs.GetPosition(IInputElement relativeTo)来获取鼠标相对于某一界面元素的相对位置,这自然也联想到另外的一个问题:如何获取鼠标相对于屏幕的位置。 在WPF文档中找了半天也没找到。
但我们别忘了 System.Windows.Forms.Control类中有一个静态属性System.Windows.Forms.Control.MousePosition,我们可以在WPF中继续使用它,当然这需要你的项目添加 System.Windows.Forms和 System.Drawing名字空间,这多少让人有些别扭,因为玩WPF时总有点不喜欢调用System.Windows.Forms下的东西,喜新厌旧吧。
另外一个方法是平台调用,在VB的文档中可以找到一个API:Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long,我们可以在C#中使用它,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Samples
{
class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);
}
}
OK,问题解决了:)
相关文章推荐
- WPF中获取鼠标相对于屏幕的位置——出自《IT168》
- WPF中获取鼠标相对于屏幕的位置
- WPF获取鼠标相对于屏幕的绝对位置
- WPF中获取鼠标相对于屏幕的位置
- 获取鼠标点相对于各屏幕、窗体和当前控件的位置
- WPF中获取鼠标相对于桌面位置
- WPF 获取鼠标屏幕位置、窗口位置、控件位置
- 获取鼠标在容器中相对位置
- WPF中获取控件之间的相对位置
- [WPF]获取控件间的相对位置
- 获取鼠标在屏幕的位置
- [JavaScript]获取鼠标点击区域的相对、绝对位置坐标
- [WPF]获取控件间的相对位置
- 获取鼠标在屏幕上的位置
- 获取鼠标在div中的相对位置的实现代码
- 用C语言在linux下获取鼠标指针的相对位置
- ios 获取控件相对屏幕的位置
- js获取窗口相对于屏幕左边和上边的位置坐标
- iOS 获取控件相对屏幕的坐标位置
- 获取控件相对于手机屏幕的位置