2009-10-08 84 views

回答

0

鼠標拖動您可以通過捕獲圖片框的鼠標移動事件,然後得到像圖片框圖形做到這一點。

Graphics g = pictureBox.CreateGraphics(); 然後你可以繪製任何你想要使用這個圖形對象繪製

+0

Eeeeep,上帝沒有!你會得到這樣的圖形圖形,我知道的唯一一個用於.CreateGraphics()的用例是測量字符串。你想重寫OnPaint。 – Quibblesome 2009-10-08 11:27:26

+0

或使用.Image屬性。如果我們要做自定義繪圖,儘管我會完全停止使用picturebox,並從Control繼承。 – Quibblesome 2009-10-08 11:28:18

20

把一個PictureBox放在你的窗體上,並將其BackColor設置爲白色。然後將此代碼添加到您的窗體(你必須低於實際掛鉤的鼠標事件,即你不能只是複製該代碼粘貼到您的形式):

private Point? _Previous = null; 
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _Previous = e.Location; 
    pictureBox1_MouseMove(sender, e); 
} 
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (_Previous != null) 
    { 
     if (pictureBox1.Image == null) 
     { 
      Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.Clear(Color.White); 
      } 
      pictureBox1.Image = bmp; 
     } 
     using (Graphics g = Graphics.FromImage(pictureBox1.Image)) 
     { 
      g.DrawLine(Pens.Black, _Previous.Value, e.Location); 
     } 
     pictureBox1.Invalidate(); 
     _Previous = e.Location; 
    } 
} 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    _Previous = null; 
} 

再畫了!

如果您願意,可以通過設置Graphics對象的SmoothingMode屬性來改善繪製圖像的質量。

更新:的.Net CF簡化版,有Pens集合,MoustEventArgs沒有Location,所以這裏是一個CF的版本:

private Point? _Previous = null; 
private Pen _Pen = new Pen(Color.Black); 
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _Previous = new Point(e.X, e.Y); 
    pictureBox1_MouseMove(sender, e); 
} 
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (_Previous != null) 
    { 
     if (pictureBox1.Image == null) 
     { 
      Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.Clear(Color.White); 
      } 
      pictureBox1.Image = bmp; 
     } 
     using (Graphics g = Graphics.FromImage(pictureBox1.Image)) 
     { 
      g.DrawLine(_Pen, _Previous.Value.X, _Previous.Value.Y, e.X, e.Y); 
     } 
     pictureBox1.Invalidate(); 
     _Previous = new Point(e.X, e.Y); 
    } 
} 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    _Previous = null; 
} 
+0

謝謝你的幫助,但我得到的錯誤不知道鋼筆和電子地址 – Gold 2010-04-26 13:32:39

+0

@金:剛剛添加了這種方法的CF友好版本,因爲CF沒有Pens集合,MouseEventArgs沒有一個位置(只是X和Y)。 – MusiGenesis 2010-04-26 14:16:26

+0

+1對一個簡短的,沒有措辭很好的問題做了詳細的回答。 :) – JYelton 2010-04-26 14:17:49

2

這裏,pictureBox1 ==簽名。我翻譯了以這種方式VB:

全球:

Dim _previous As Point = Nothing 
Dim _pen As Pen = New Pen(Color.Black) 
Dim drawing As Boolean = False 


''' <summary> 
''' This handles the signature drawing events (drawing) 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseMove 
    If drawing = True Then 
     If signature.Image Is Nothing Then 
      Dim bmp As Bitmap = New Bitmap(signature.Width, signature.Height) 

      Using g As Graphics = Graphics.FromImage(bmp) 
       g.Clear(Color.White) 
      End Using 

      signature.Image = bmp 
     End If 

     Using g As Graphics = Graphics.FromImage(signature.Image) 
      g.DrawLine(_pen, _previous.X, _previous.Y, e.X, e.Y) 
     End Using 
     signature.Invalidate() 
     _previous = New Point(e.X, e.Y) 
    End If 
End Sub 

''' <summary> 
''' this indicates somebody is going to write a signature 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseDown 
    _previous = New Point(e.X, e.Y) 
    drawing = True 
    signature_MouseMove(sender, e) 

End Sub 

''' <summary> 
''' the signature is done. 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseUp 
    _previous = Nothing 
    drawing = False 
End Sub