如何实现得到屏幕的绝对坐标?

2025-06-22 16:08:21
推荐回答(4个)
回答1:

通过调api函数来获得系统的鼠标位置,也可以直接调用.net类库中的Cursor类来得到坐标,然后在程序用通过一个线程去实时显示当前鼠标的位置显示到文本框中
下面给出用C#实现的源码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace ShowCursor
{
public class frmShowPoint1:Form
{
///


/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;

///
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.txtShowPoint = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 50;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// txtShowPoint
//
this.txtShowPoint.Location = new System.Drawing.Point(0, 1);
this.txtShowPoint.Name = "txtShowPoint";
this.txtShowPoint.Size = new System.Drawing.Size(234, 21);
this.txtShowPoint.TabIndex = 1;
//
// frmShowPoint1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(234, 22);
this.Controls.Add(this.txtShowPoint);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "frmShowPoint1";
this.Text = "Form2";
this.Load += new System.EventHandler(this.frmShowPoint_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private TextBox txtShowPoint;
private System.Windows.Forms.Timer timer1;

public frmShowPoint1()
{
InitializeComponent();
}

public class GetCurPos
{
[DllImport("user32.dll")]
public static extern short GetCursorPos(ref System.Drawing.Point point);
}

private void timer1_Tick(object sender, EventArgs e)
{
System.Drawing.Point point = new System.Drawing.Point();
GetCurPos.GetCursorPos(ref point);
txtShowPoint.Text = "X坐标:" + point.X + " Y坐标:" + point.Y;
//这是另一种不需要调用Api通过直接调用.net类库来得到鼠标坐标
//Point point = Cursor.Position;
//txtShowPoint.Text = "X坐标:" + point.X + " Y坐标:" + point.Y;
}

private void frmShowPoint_Load(object sender, EventArgs e)
{
this.Top = 0;
}

[STAThread]
static void Main(string[] args)
{
Application.Run(new frmShowPoint1());
}
}
}

回答2:

试试这个(没毒),点底下的“预览效果”:http://free.2259.com/jscode.asp?js_id=428

回答3:

X坐标:event.X
Y坐标:event.Y
还有不理解的可以给我留言。

回答4:

左上角是0 0