先判断richTextBox的坐标位置,在判断鼠标在屏幕中的位置,换算一下就可以了,具体的看看.net framework sdk
解决方案 »
- 如何换行?
- vs2010使用sql server compact edition 3.5疑问
- c#一个很严重问题
- 请教关于创建控件的BorderStyle属性问题
- 关于C# 的datagridview里的表格数据进行分栏的问题
- 在WEB SERVICE中访问一个本地文件怎么做?路径老有问题,取相对路径“\x.txt”不行。
- WPF,请问路由事件的source和sender
- 求问诸位大神,如何用VS2012将C#代码中的十六进制代码替换为10进制
- 有没有函数可以把一般的路径变成UNC路径?比如“\\机器名(IP)\C$\winnt\...”
- 请问哪边有英汉对照的文章下载,
- c#datagrid 增加,删除,修改
- 读取xml的问题!
您好!
我说的光标不是鼠标的光标,而是指编辑文本时闪动在字与字之间的光标。麻烦在思考一下,我一定给分,谢谢!
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(16, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(528, 232);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(456, 272);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "Clike Me!";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonGetResult,
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()+"列");
}
}
}