![](/img/trans.png)
[英]C# How to programatically set/clear selections and cursor position in PowerPoint?
[英]C#, How to set the cursor position manually
我做了“获取坐标”部分,我需要做“设置”部分,在这里我可以手动输入坐标,然后按“设置”按钮使“蓝色Cirlce”与我输入的坐标一起出现。 pictureBox1
textBox2
。 此代码为“获取”:
int mouseX, mouseY;
Pen bluePen = new Pen(Color.Blue, 1);
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "X = " + e.X + " ; Y = " + e.Y;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle circle = new Rectangle(mouseX - 8, mouseY - 8, 16, 16);
e.Graphics.DrawEllipse(bluePen, circle);
}
将按钮单击事件处理方法“ ButtonSet_Click”添加到“设置”按钮。
private void ButtonSet_Click(object sender, EventArgs e)
{
Point p = getXYfromTextBox();
Rectangle circle = new Rectangle(p.X - 8, p.Y - 8, 16, 16);
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(redPen, circle);
}
//this method can be optimized
private Point getXYfromTextBox()
{
string xy = textBox2.Text.Trim();
string[] xys = xy.Split(';');
mouseX = Convert.ToInt32(xys[0].Split('=')[1].Trim());
mouseY = Convert.ToInt32(xys[1].Split('=')[1].Trim());
Point p = new Point(mouseX, mouseY);
return p;
}
如果我理解正确,那么您正在尝试寻找一种将输入解析为坐标的方法。 尝试此操作(您需要仔细验证输入字符串并处理将字符串强制转换为int时引发的潜在异常)。
private void btnSet_Click(object sender, EventArgs e)
{
string input = tbInput.Text.Trim();
string[] parts = input.Split(",".ToCharArray()); //assume your coordinates are commas-separated, like "80,100"
if (parts.Length == 2)
{
mouseX = int.Parse(parts[0]);
mouseY = int.Parse(parts[1]);
pictureBox1.Refresh(); //Now force picturebox to repaint
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.