C# .NET中如何使用GetCursorPos函数 例程
2012-04-29 19:52
519 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CursorPosition
{
public partial class frmMain : Form
{
// We need to use unmanaged code
[DllImport("user32.dll")]
// GetCursorPos() makes everything possible
static extern bool GetCursorPos(ref Point lpPoint);
// Variable we will need to count the traveled pixels
static protected long totalPixels = 0;
static protected int currX;
static protected int currY;
static protected int diffX;
static protected int diffY;
public frmMain()
{
InitializeComponent();
}
private void tmrDef_Tick(object sender, EventArgs e)
{
// New point that will be updated by the function with the current coordinates
Point defPnt = new Point();
// Call the function and pass the Point, defPnt
GetCursorPos(ref defPnt);
// Now after calling the function, defPnt contains the coordinates which we can read
lblCoordX.Text = "X = " + defPnt.X.ToString();
lblCoordY.Text = "Y = " + defPnt.Y.ToString();
// If the cursor has moved at all
if (diffX != defPnt.X | diffY != defPnt.Y)
{
// Calculate the distance of movement (in both vertical and horizontal movement)
diffX = (defPnt.X - currX);
diffY = (defPnt.Y - currY);
// The difference will be negative if the cursor was moved left or up
// and if it is so, make the number positive
if (diffX < 0)
{
diffX *= -1;
}
if (diffY < 0)
{
diffY *= -1;
}
// Add to the "pixels traveled" counter
totalPixels += diffX + diffY;
// And display inside a label
lblTravel.Text = "You have traveled " + totalPixels + " pixels";
}
// We need this to see the difference of pixels between two mouse movements
currX = defPnt.X;
currY = defPnt.Y;
}
private void btnReset_Click(object sender, EventArgs e)
{
totalPixels = 0;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CursorPosition
{
public partial class frmMain : Form
{
// We need to use unmanaged code
[DllImport("user32.dll")]
// GetCursorPos() makes everything possible
static extern bool GetCursorPos(ref Point lpPoint);
// Variable we will need to count the traveled pixels
static protected long totalPixels = 0;
static protected int currX;
static protected int currY;
static protected int diffX;
static protected int diffY;
public frmMain()
{
InitializeComponent();
}
private void tmrDef_Tick(object sender, EventArgs e)
{
// New point that will be updated by the function with the current coordinates
Point defPnt = new Point();
// Call the function and pass the Point, defPnt
GetCursorPos(ref defPnt);
// Now after calling the function, defPnt contains the coordinates which we can read
lblCoordX.Text = "X = " + defPnt.X.ToString();
lblCoordY.Text = "Y = " + defPnt.Y.ToString();
// If the cursor has moved at all
if (diffX != defPnt.X | diffY != defPnt.Y)
{
// Calculate the distance of movement (in both vertical and horizontal movement)
diffX = (defPnt.X - currX);
diffY = (defPnt.Y - currY);
// The difference will be negative if the cursor was moved left or up
// and if it is so, make the number positive
if (diffX < 0)
{
diffX *= -1;
}
if (diffY < 0)
{
diffY *= -1;
}
// Add to the "pixels traveled" counter
totalPixels += diffX + diffY;
// And display inside a label
lblTravel.Text = "You have traveled " + totalPixels + " pixels";
}
// We need this to see the difference of pixels between two mouse movements
currX = defPnt.X;
currY = defPnt.Y;
}
private void btnReset_Click(object sender, EventArgs e)
{
totalPixels = 0;
}
}
}
相关文章推荐
- C# .NET中如何使用GetCursorPos函数 例程(转载)
- C# .NET中如何使用GetCursorPos函数
- C# .NET中如何使用GetCursorPos函数
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 如何使用<Beginning ASP.NET 3.5 in C# and VB>书中的示例代码
- 0120如何合并两个使用 System.Xml 使用 Visual C#.NET 的 XML 文档中的数据
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- [引]如何藉由使用 Visual C# . NET 處理 Word 中的事件
- 如何在VS.Net中使用C#语言调用CHM帮助文件
- 如何高效使用SQLITE .NET (C#)
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 微信公众号入门学习2_使用C#,ASP.NET APIController如何被动回复用户消息
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 如何在ASP.NET 网站项目中使用C# 6?
- 如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- .net(C#)中String.Format如何使用
- (原創) 如何使function傳回多值? (使用iterator) (.NET) (C#)
- 如何使用 ASP.NET 应用程序以使用 Visual C#.NET 来查询索引服务目录
- 如何使用ADO.Net助手生成C#数据库代码