日期:2009-12-25 浏览次数:20430 次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 如何获取鼠标相对当前窗口的坐标
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 可以用Control.MousePosition获得当前鼠标的坐标,使用PointToClient计算鼠标相对于某个控件的坐标,如下
Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
Point formPoint = this.PointToClient(Control.MousePosition);//鼠标相对于窗体左上角的坐标
Point button1Point = button1.PointToClient(Control.MousePosition);//鼠标相对于contextMenuStrip1左上角的坐标
label4.Text = screenPoint.ToString();
label5.Text=formPoint.ToString();
label6.Text=button1Point.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}