2010-10-05 42 views
2

我目前的代碼允許我從用戶定義的地點繪製矩形,但並不符合我的願望。我需要它,就像您在油漆做到這一點,這是我當前的代碼:繪製一個用戶定義的矩形

命名空間SimpleDraw2 {/// /// MainForm的說明。 /// public partial class MainForm:Form { bool IsMouseDown = false; Point MousePosition; int DrawShape = 0; 位圖StoredImage;

public MainForm() 
    { 
     // 
     // The InitializeComponent() call is required for Windows Forms designer support. 
     // 
     InitializeComponent(); 

     // 
     // TODO: Add constructor code after the InitializeComponent() call. 
     // 
     pictureBox1.Image = new Bitmap (pictureBox1.Width,pictureBox1.Height);      
     StoredImage = new Bitmap(pictureBox1.Width,pictureBox1.Height); 
    } 

    void PictureBox1MouseDown(object sender, MouseEventArgs e) 
    { 
     IsMouseDown = true; 
     MousePosition = e.Location; 
     Graphics gStored = Graphics.FromImage(StoredImage); 
     gStored.Clear(Color.Transparent); 
     gStored.DrawImage(pictureBox1.Image, 0, 0); 
    } 

    void PictureBox1MouseUp(object sender, MouseEventArgs e) 
    { 
     IsMouseDown = false; 
    } 

    void PictureBox1MouseMove(object sender, MouseEventArgs e) 
    { 
     Graphics g = Graphics.FromImage(pictureBox1.Image); 
     if (DrawShape == 0) 
     { 
      Pen p = new Pen(Color.Red, 10); 
      if (IsMouseDown) 
      { 
       g.DrawLine(p,MousePosition,e.Location); 
       MousePosition = e.Location; 
      } 
     } 
     if (DrawShape == 1) 
     { 
      g.Clear(Color.Transparent); 
      g.DrawImage(StoredImage,0,0); 
      g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y); 

     } 
     if (DrawShape == 2) 
     { 
      g.Clear(Color.Transparent); 
      g.DrawImage(StoredImage, 0, 0); 
      g.DrawEllipse(Pens.HotPink, MousePosition.X, MousePosition.Y, e.X, e.Y); 
     } 
     if (DrawShape == 3) 
     { 
      g.Clear(Color.Transparent); 
      g.DrawImage(StoredImage, 0, 0); 
      g.DrawArc(Pens.Indigo,pictureBox1.Bounds, e.Y, e.X); 
     } 
     //if (DrawShape == 4) 
     //{ 
     // g.Clear(Color.Transparent); 
     // g.DrawImage(StoredImage, 0, 0); 
     // g.DrawPolygon(Pens.Indigo, Point[] e.X); 
     //} 

     this.Refresh(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      axWindowsMediaPlayer1.URL = ofd.FileName; 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     axWindowsMediaPlayer1.Ctlcontrols.pause(); 
     Bitmap bmp = new Bitmap(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height); 
     Graphics gfx = Graphics.FromImage(bmp); 
     gfx.CopyFromScreen(PointToScreen(axWindowsMediaPlayer1.Location), new Point(0, 0), axWindowsMediaPlayer1.Bounds.Size, CopyPixelOperation.SourceCopy); 
     pictureBox1.BackgroundImage = bmp; 
     //axWindowsMediaPlayer1.Visible = false; 
     //pictureBox1.Visible = true; 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     Graphics gg = Graphics.FromImage(pictureBox1.BackgroundImage); 
     gg.Clear(Color.Transparent); 
     Graphics gStored = Graphics.FromImage(StoredImage); 
     gStored.Clear(Color.Transparent); 
     Graphics g = Graphics.FromImage(pictureBox1.Image); 
     g.Clear(Color.Transparent); 


    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     DrawShape = 1; 

    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     DrawShape = 2; 
    } 

    private void button8_Click(object sender, EventArgs e) 
    { 
     DrawShape = 3; 
    } 

    private void button7_Click(object sender, EventArgs e) 
    { 
     DrawShape = 0; 
    } 
} 

}

如果有人可以幫我修改我的代碼,以消除這一問題,可以很容易拖動並繪製系統會我非常感激。

由於提前

克里斯

回答

2

msdn

繪製由一 座標對,寬度和高度 指定的矩形。

所以,你的代碼將無法正常工作:

g.DrawRectangle(Pens.Green,MousePosition.X,MousePosition.Y,e.X,e.Y); 

應該像

g.DrawRectangle(Pens.Green, MousePosition.X, MousePosition.Y, Math.Abs(e.X - MousePosition.X), Math.Abs(e.Y - MousePosition.Y)); 
+0

同樣的問題仍然存在,它保存最後一個位置,並從控制器最後離開圖片框的地方畫一個正方形。 – 2010-10-05 10:58:53

1

我看到的最大的問題是你想在鼠標事件繪製。這意味着您的繪圖將在您刷新事件的瞬間消失。

只繪製Paint事件,從不在鼠標事件中繪製。如果您希望應用程序因鼠標事件而繪製,請在鼠標事件中設置一個點,矩形或其他任何內容(例如,您開始使用IsMouseDown),使您想要在MouseMoved事件中更改的區域無效,然後繪製你的矩形或任何在你的Paint事件中。