2012-11-11 96 views
3

我有一個簡單的程序,您可以在屏幕上用FillEllipse和FillRectangle繪製。我的問題是,當您將另一個窗口拖過屏幕的一小部分時,該部分將被擦除。當您拖動另一個窗口時,發生這種情況,鬆手並將其拖回。有沒有什麼辦法解決這一問題?繪圖圖形在VB.net中消失

Dim MyFormObject As Graphics = Me.CreateGraphics 
     Select Case shape 
      Case "Ellipse" 
       MyFormObject.FillEllipse(brush, e.X - CInt(brushWidth/2), e.Y - CInt(brushHeight/2), brushWidth, brushHeight) 
      Case "Rectangle" 
       MyFormObject.FillRectangle(brush, e.X - CInt(brushWidth/2), e.Y - CInt(brushHeight/2), brushWidth, brushHeight) 
     End Select 
+0

您是直接繪製到屏幕(hdc 0)還是您自己的形式? – gordy

+0

編輯顯示我的代碼 – WillumMaguire

+0

編輯下面的答案,而不是創建一個新的圖形每次你需要畫一個像pictureBox一樣的持久性圖形。或者,您可以重新繪製每個繪畫事件,但可能會變得昂貴。 – gordy

回答

4

你需要做的所有的圖紙在Paint事件,每個控制被重新粉刷一次閃光。

+0

Paint事件到底是什麼,我將如何在其中進行繪製?我是新的圖形 – WillumMaguire

5

你可以把一個PictureBox控件窗體上,繪製到不是,它不會被刪除時,其他窗口油漆過它:

這樣做一次,上的Form_Load什麼:

pictureBox1.Image = new Bitmap(Width, Height); 

得出:

Graphics.FromImage(pictureBox1.Image).FillRectangle(Brushes.Black, 0, 0, 100, 100); 
pictureBox1.Refresh(); 
+0

這修復了我的朋友。他在他的表單加載中使用Graphics.FromHwnd(pictureBox1.Handle)。改變後使用FromImage(pictureBox1.Image)它工作!它現在在窗體上顯示圖形,而不會消失! –

1

下面的代碼可以讓你用鼠標畫(單擊並拖動)矩形。將一個PictureBox添加到表單。

Public Class Form1 
    Private mpntMouseDown As Point 

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    Dim w As Integer = PictureBox1.Width 
    Dim h As Integer = PictureBox1.Height 
    Dim bmp As New Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb) 
    Using g As Graphics = Graphics.FromImage(bmp) 
     Dim rct As New RectangleF(0, 0, w, h) 
     Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush(rct, Color.White, Color.Blue, 0) 
     g.FillRectangle(b, rct) 
     g.DrawEllipse(Pens.Blue, New RectangleF(CInt(0.1 * w), CInt(0.2 * h), CInt(0.8 * w), CInt(0.6 * h))) 
     g.FillEllipse(Brushes.Yellow, New RectangleF(CInt(0.1 * w) + 1, CInt(0.2 * h) + 1, CInt(0.8 * w) - 2, CInt(0.6 * h) - 2)) 
     Dim sft As New StringFormat 
     sft.Alignment = StringAlignment.Center 
     sft.LineAlignment = StringAlignment.Center 
     g.DrawString("Sample Image", New Font(System.Drawing.FontFamily.GenericSerif, 14, FontStyle.Italic, GraphicsUnit.Point), Brushes.Red, rct, sft) 
    End Using 
    PictureBox1.Image = bmp 
    End Sub 

    Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown 
    If e.Button = Windows.Forms.MouseButtons.Left Then 
     mpntMouseDown = e.Location 
    End If 
    End Sub 

    Private Sub PictureBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp 
    If mpntMouseDown = Nothing Then Exit Sub 
    Using g As Graphics = Graphics.FromImage(PictureBox1.Image, Bitmap) 
     Dim rct As New Rectangle 
     If mpntMouseDown.X < e.X Then 
     rct.X = mpntMouseDown.X 
     rct.Width = e.X - mpntMouseDown.X + 1 
     Else 
     rct.X = e.X 
     rct.Width = mpntMouseDown.X - e.X + 1 
     End If 
     If mpntMouseDown.Y < e.Y Then 
     rct.Y = mpntMouseDown.Y 
     rct.Height = e.Y - mpntMouseDown.Y + 1 
     Else 
     rct.Y = e.Y 
     rct.Height = mpntMouseDown.Y - e.Y + 1 
     End If 
     g.DrawRectangle(Pens.Black, rct) 
    End Using 
    mpntMouseDown = Nothing 
    PictureBox1.Invalidate() 
    End Sub 
End Class 
+1

如果PictureBox被另一個對象(例如另一個窗體或另一個應用程序)覆蓋,則此處執行的所有繪製將消失。窗體或控件的所有繪畫都應該在OnPaint方法或Paint事件中進行。 –

+0

這是奇特的。當我測試代碼時,覆蓋表單不會丟失圖形,但是最小化表單確實如此。 – SSS

+0

我編輯了我的答案來解決Chris Dunaway提出的問題 – SSS

0

@SLaks已經告訴你做的所有畫在OnPaint方法。這裏有更多的信息。如果您試圖在表單上繪圖,那麼您將覆蓋OnPaint方法,並使用傳入方法的Graphics實例完成所有繪製。這裏是關於主題的更多信息:

http://www.bobpowell.net/creategraphics.htm

http://www.bobpowell.net/picturebox.htm

Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) 
    MyBase.OnPaint(e) 

    e.Graphics.FillEllipse(Brushes.Red, Me.ClientRectangle) 
End Sub 
0

只是一個洞察,真正幫助我在vb.net平局是這個例子vbnettuthut.blogspot.com它顯示一個完整的例子來繪製平滑和快速,以工作示例和源代碼鱈魚