![](/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.