如何得到指针(鼠标)的当前位置?? private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){ statusBar.Panels[0].Text = "当前鼠标的位置为:( " + e.X + " , " + e.Y + ")" ;} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 XXX_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){ int x=e.X; int y=e.Y;} XXX_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){ int x=e.X; int y=e.Y; Point mouseDownLocation = new Point(x, y);} 调用GetCaretPos来实现。using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace csTest { public class Form1 : System.Windows.Forms.Form { [DllImport("user32")] public static extern bool GetCaretPos(ref System.Drawing.Point lpPoint); private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button buttonGetResult; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.buttonGetResult = new System.Windows.Forms.Button(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(19, 26); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(634, 250); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = ""; // // buttonGetResult // this.buttonGetResult.Location = new System.Drawing.Point(547, 293); this.buttonGetResult.Name = "buttonGetResult"; this.buttonGetResult.Size = new System.Drawing.Size(90, 25); this.buttonGetResult.TabIndex = 1; this.buttonGetResult.Text = "行数列数"; this.buttonGetResult.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(672, 324); this.Controls.Add(this.buttonGetResult); this.Controls.Add(this.richTextBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private int X=0; private int Y=0; private void button1_Click(object sender, System.EventArgs e) { Point P=new Point(0); GetCaretPos(ref P); int Pos=this.richTextBox1.GetCharIndexFromPosition(P); this.Y=this.richTextBox1.GetLineFromCharIndex(Pos); if (this.Y>0) { int offset=1; while(this.richTextBox1.Text[Pos-offset]!='\n') offset++; this.X=offset; this.Y++; } else { this.X=Pos+1; this.Y=1; } MessageBox.Show("当前位置: 第"+this.Y.ToString()+"行 "+"第"+this.X.ToString()+"列"); } } }TheAres(班门斧) 的算法,我不过是在班门弄斧罢了。 如果你想得到当前鼠标的位置(不一定是当前窗口)可以用Contrl.MousePoint得到,返回的point表示当前鼠标位置(屏幕坐标) 打错了,是:System.Drawing.Point pp= Control.MousePosition; 若仅仅是要获得自己程序中的鼠标位置,建议使用MouseMove事件.否则使用GetCursorPos这个API,声明如下:[DllImport("user32.dll", EntryPoint="GetCursorPos")]public static extern int GetCursorPos ( ref Point lpPoint); C# 自定义控件 C# 调用dll的结构的指针的指针 第二次发贴:为什么网站发布后,有些图片看不到了? 初学者提问,很简单的问题 问大家一个关于oracle的问题 今天初学C#,一调试竟发现:C#是解释型的!!! 如何实现按钮的顺序的自动触发? C#保存网页cookie的问题 动态绑定List<Dictionary<string, string>>和datagrid 关于C#的两个问题: c#中类似VC中hidword和lowdword的函数是什么? c#中有和vb.net中的IsNumeric功用相同的函数吗?
{
int x=e.X;
int y=e.Y;}
{
int x=e.X;
int y=e.Y;
Point mouseDownLocation = new Point(x, y);
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices; namespace csTest
{
public class Form1 : System.Windows.Forms.Form
{ [DllImport("user32")]
public static extern bool GetCaretPos(ref System.Drawing.Point lpPoint); private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button buttonGetResult;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.buttonGetResult = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(19, 26);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(634, 250);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(547, 293);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.Size = new System.Drawing.Size(90, 25);
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "行数列数";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 324);
this.Controls.Add(this.buttonGetResult);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private int X=0;
private int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
Point P=new Point(0);
GetCaretPos(ref P);
int Pos=this.richTextBox1.GetCharIndexFromPosition(P);
this.Y=this.richTextBox1.GetLineFromCharIndex(Pos); if (this.Y>0)
{
int offset=1;
while(this.richTextBox1.Text[Pos-offset]!='\n')
offset++;
this.X=offset;
this.Y++;
}
else
{
this.X=Pos+1;
this.Y=1;
} MessageBox.Show("当前位置: 第"+this.Y.ToString()+"行 "+"第"+this.X.ToString()+"列");
}
}
}TheAres(班门斧) 的算法,我不过是在班门弄斧罢了。
否则使用GetCursorPos这个API,声明如下:
[DllImport("user32.dll", EntryPoint="GetCursorPos")]
public static extern int GetCursorPos (
ref Point lpPoint
);