C#中如何获取鼠标坐标?

RedErick 2004-11-26 04:29:59
无论在窗口内还是窗口外,请高人指教。
...全文
506 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucbesson 2004-11-27
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace spy
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label m_lblMousePos;
private System.Windows.Forms.TextBox m_txtLog;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public MainForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.m_lblMousePos = new System.Windows.Forms.Label();
this.m_txtLog = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// m_lblMousePos
//
this.m_lblMousePos.Location = new System.Drawing.Point(16, 24);
this.m_lblMousePos.Name = "m_lblMousePos";
this.m_lblMousePos.Size = new System.Drawing.Size(120, 16);
this.m_lblMousePos.TabIndex = 0;
this.m_lblMousePos.Text = "鼠标位置";
//
// m_txtLog
//
this.m_txtLog.Location = new System.Drawing.Point(16, 64);
this.m_txtLog.Multiline = true;
this.m_txtLog.Name = "m_txtLog";
this.m_txtLog.ReadOnly = true;
this.m_txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.m_txtLog.Size = new System.Drawing.Size(408, 152);
this.m_txtLog.TabIndex = 1;
this.m_txtLog.Text = "";
this.m_txtLog.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.m_txtLog_KeyPress);
this.m_txtLog.MouseMove += new System.Windows.Forms.MouseEventHandler(this.m_txtLog_MouseMove);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 246);
this.Controls.Add(this.m_txtLog);
this.Controls.Add(this.m_lblMousePos);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "截获";
this.Load += new System.EventHandler(this.MainForm_Load);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.m_lblMousePos.Text = Control.MousePosition.ToString();
}

private void MainForm_Load(object sender, System.EventArgs e)
{
this.m_lblMousePos.Text = Control.MousePosition.ToString();
}

private void m_txtLog_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.m_lblMousePos.Text = Control.MousePosition.ToString();
}

private void m_txtLog_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
this.m_txtLog.AppendText(DateTime.Now.ToString() + " " + e.KeyChar.ToString() + "\r\n");
}
}
}

还有键盘记录看看吧。
给分 heihei










pxk 2004-11-27
  • 打赏
  • 举报
回复
try some win32 api
lucbesson 2004-11-27
  • 打赏
  • 举报
回复
使用hook吧,全局钩子能做到。

你的分。。。。。。少了点哦 哈哈
ballnan 2004-11-27
  • 打赏
  • 举报
回复
event.x
event.y

onmousemove 事件发生时

<script>
function onmove()
{
document.all("TextBox").value = "x = "+event.x + " y = "+event.y;
}
</script>

<body onmousemove=onmove()>
ZC747 2004-11-26
  • 打赏
  • 举报
回复
窗口内的 Control.MousePosition.X
Control.MousePosition.Y
iamknight 2004-11-26
  • 打赏
  • 举报
回复
?????????????????????
Cursor.Position 得到当前鼠标的屏幕坐标

myForm.DesktopLocation 得到窗口的屏幕坐标
hatita 2004-11-26
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public struct POINTAPI
{
public int x;
public int y;
}
[DllImport("user32.dll", EntryPoint="GetCursorPos")]
public static extern int GetCursorPos (ref POINTAPI lpPoint);
POINTAPI point = new POINTAPI();
int i = GetCursorPos( ref point);

this.textBox1.Text = point.x.ToString();
this.textBox2.Text = point.y.ToString();

用API可以取得窗口内外的坐标(像素),但问题是当鼠标移动的时候你取得坐标,你用什么来判断窗口外的鼠标移动?应该有个API吧。。不记得了-_______________________-b
hanbingture 2004-11-26
  • 打赏
  • 举报
回复
侠客好强。 lxrxyz(无忧十年) 你怎么混的4个裤衩
mazekui 2004-11-26
  • 打赏
  • 举报
回复
具体实现方法:
步骤一:
在使用的窗体里声明一个类:
public class GetCurPos
{
[DllImport("user32.dll")]
public static extern short GetCursorPos(ref System.Drawing.Point point);
}

步骤二:
调用方法:
private void button1_Click(object sender, System.EventArgs e)
{
System.Drawing.Point point=new System.Drawing.Point();
GetCurPos.GetCursorPos(ref point);
label1.Text="X:"+point.X+"Y:"+point.Y;
}
一半乐事 2004-11-26
  • 打赏
  • 举报
回复
看一个这个类对你有没有用
MouseEventArgs
iamknight 2004-11-26
  • 打赏
  • 举报
回复
Cursor.Position
还不够格啊 2004-11-26
  • 打赏
  • 举报
回复
然后可用 PointToClient 变换
还不够格啊 2004-11-26
  • 打赏
  • 举报
回复
Control.MousePosition
mazekui 2004-11-26
  • 打赏
  • 举报
回复
用API函数getcursorpos()可以取到

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧