2010-11-05 29 views
2

我想製作一個工具,允許我選擇圖片框上的某個位置來放置文本框中的文本。它需要能夠在圖片框上放置多個不同的文本,然後才能被刪除。這是我當前的代碼:用戶在c中的圖片框上放置文本#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace TextboxTool 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
     } 

     private void textBox1_MouseClick(object sender, MouseEventArgs e) 
     { 
      textBox1.Text = ""; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      textBox1.Visible = true; 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 
      Graphics G = Graphics.FromImage(pictureBox1.Image); 
      G.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.Black, new Point(MousePosition.X, MousePosition.Y)); 
     } 
    } 
} 

此刻,我可以在文本框中鍵入文本,但不能借鑑PictureBox的字符串,並選擇它的位置。我有一個按鈕,用於確認寫入的文本是否正確,然後允許用戶選擇其位置。請有人能幫我把這個代碼分類出來嗎?

Thanks-

回答

3

MousePosition屬性是相對於屏幕,而不是PictureBox

您應該處理MouseClick事件並在e.Xe.Y處繪製字符串。
或者,您可以致電pictureBox1.PointToClient將屏幕座標轉換爲控制相對座標。

另外,您應該將Graphics對象置於using聲明中。

最後,我敢肯定你需要在修改圖像強制重繪後調用pictureBox1.Invalidate()