日期:2014-05-19  浏览次数:20777 次

如何监控鼠标位置
例如当用户鼠标移动到windows的任务栏,当前程序就去打开一个窗体。  
或鼠标移动到离屏幕顶端多少个像素,当前程序就去打开一个窗体。

------解决方案--------------------
win32 API:GetCursorPos
------解决方案--------------------
private void timer1_Tick(object sender, EventArgs e)
{
Text = string.Format( "{0} ", Control.MousePosition);
}

------解决方案--------------------
private Point MouseMove(object sender, MouseEventArgs e)
{
Point p2 = new Point(e.X, e.Y);
}

//可以考虑抓windows的任务栏的句柄,因为电脑启动一般来说没有什么变化,猜想应该不会改(猜想而已应以事实说话);
------解决方案--------------------
using System.Runtime.InteropServices;

[DllImport( "user32.dll ")]
public static extern IntPtr WindowFromPoint(Point Point);
[DllImport( "user32.dll ")]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport( "user32.dll ")]
public static extern bool GetClassName(IntPtr hWnd,
StringBuilder lpClassName, int nMaxCount);

private void timer1_Tick(object sender, EventArgs e)
{
IntPtr vHandle = WindowFromPoint(Control.MousePosition); //得到鼠标所在的窗体句柄
while (GetParent(vHandle) != IntPtr.Zero) //得到最上级窗体的句柄
vHandle = GetParent(vHandle);
StringBuilder vStringBuilder = new StringBuilder(256);
GetClassName(vHandle, vStringBuilder, vStringBuilder.Capacity); //取得该窗体的类名
if (vStringBuilder.ToString() == "Shell_TrayWnd ") //如果类名为Shell_TrayWnd则为任务栏
Text = "鼠标在任务栏上 ";
else Text = "NULL ";
}