3

FillEllipse と FillRectangle を使用して画面に描画できる簡単なプログラムがあります。私の問題は、画面のごく一部でも別のウィンドウをドラッグすると、その部分が消去されることです。これは、他のウィンドウをドラッグして離し、ドラッグして元に戻すと発生します。これを修正する方法はありますか?

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
4

4 に答える 4

5

フォームに PictureBox コントロールを配置して代わりに描画すると、他のウィンドウがその上にペイントしても消去されません。

form_load または何かで、これを 1 回実行します。

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

描く:

Graphics.FromImage(pictureBox1.Image).FillRectangle(Brushes.Black, 0, 0, 100, 100);
pictureBox1.Refresh();
于 2012-11-11T18:50:52.927 に答える
4

Paintコントロールが再描画されるたびに発生するイベントですべての描画を行う必要があります。

于 2012-11-11T18:43:55.943 に答える
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
于 2012-11-12T02:39:43.277 に答える
0

@SLaksは、OnPaintメソッドですべてのペイントを行うようにすでに指示しています。ここにもう少し情報があります。フォームに描画しようとしている場合は、OnPaintメソッドをオーバーライドし、メソッドに渡されたGraphicsインスタンスを使用してすべてのペイントを実行します。このトピックの詳細は次のとおりです。

Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    e.Graphics.FillEllipse(Brushes.Red, Me.ClientRectangle)

End Sub
于 2012-11-12T15:33:47.647 に答える