本文介绍了哪个事件我应该使用,以显示在画什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要把一些图形在一个TableLayoutPanel的一个部分。

I need to put some graphics in one section of a TableLayoutPanel.

我创建的TLP的一个单元一个PictureBox这样做。

I'm doing this by creating a PictureBox in one cell of the TLP.

不能得到两件事情一起工作:

Can't get two things to work:

1)初始显示是空白的!图纸只有当调整形式出现

1) The initial display is blank! Drawing appears only when you resize the form

2)不断扩大规模相比承包时,resize事件不会触发一样。

2) The resize event doesn't fire equally when expanding the size as compared contracting.

任何建议,以改善上述两个问题将是巨大的!

Any suggestions to improve the above two problems would be great!

下面是我的code。窗体有一个2x2的TableLayoutPanel在里面,而TLP的一个单元中有一个图片。无论是TLP和图片框设置为填充父:

Here is my code. The form has a 2x2 TableLayoutPanel in it, and one cell of the TLP has a PictureBox in it. Both the TLP and the PictureBox are set to Fill Parent:

Imports System.Drawing.Drawing2D

Public Class Form1
    Private g As Graphics
    Dim n As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Debug.Print(String.Format("{0}{0}Form1_Load at {1}", vbCrLf, Now()))
    Me.SetDesktopLocation(800, 200)
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    n += 1
    Debug.Print(String.Format("MyBase.Paint: {0}", n))
    DisplayMyStuff()
End Sub

Private Sub PictureBox1_Resize(sender As Object, e As EventArgs) Handles Pict    ureBox1.Resize
    n += 1
    Debug.Print(String.Format("PictureBox1.Resize: {0}  PictureBoxSize = {1} / {2}", n, PictureBox1.Width, PictureBox1.Height))
    If g IsNot Nothing Then
        g.Dispose()
    End If
    g = PictureBox1.CreateGraphics()
End Sub

Private Sub DisplayMyStuff()
    Dim rect1 As Rectangle
    Dim rect2 As Rectangle
    Dim pt1 As New Point(50, 50)
    Dim pt2 As New Point(100, 100)
    Dim pt3 As New Point(150, 150)
    Dim brR As New SolidBrush(Color.Red)
    Dim linGradBr As New LinearGradientBrush(pt2, pt3, Color.Yellow, Color.Blue)
    Dim pictBoxSize As Size
    Dim sz As Size
    Dim width, height As Integer

    pictBoxSize = New Size(CType(PictureBox1.Size, Point))
    width = CInt(pictBoxSize.Width / 2)
    height = CInt(pictBoxSize.Height / 2)
    sz = New Size(width, height)
    n += 1
    Debug.Print(String.Format("DisplayMyStuff: {0}, Half-Size = {1} / {2}", n, width, height))
    g.Clear(Color.Bisque)
    rect1 = New Rectangle(pt1, sz)
    rect2 = New Rectangle(pt2, sz)
    g.FillRectangle(brR, rect1)
    g.FillRectangle(linGradBr, rect2)
    brR.Dispose()
    linGradBr.Dispose()
End Sub

末级

推荐答案

显然,你想画一个图片框( G = PictureBox1.CreateGraphics()

Apparently, you are trying to draw to a picturebox (g = PictureBox1.CreateGraphics())

原因东西​​消失是,当尺寸变化,或东西越过窗口,控制和形式需要被重新绘制。这发生在Paint事件,所以你的code需要做的图纸存在。不像一个图片形象,吸引到窗体或控件项目不执着于自己,即通过在Paint事件绘制完成。

The reason stuff disappears is that when the size changes, or something passes over the window, the controls and form need to be repainted. This happens in the Paint event, so your code needs to do the drawing there. Unlike a PictureBox image, items drawn to a form or control are not persistent on their own, that is done by drawing in the Paint event.

这基本上是你的DrawMyStuff程序迁往Picbox的Paint事件。

This is essentially your DrawMyStuff procedure relocated to the Picbox's Paint event.

Private Sub PictureBox1_Paint(sender As Object,
          e As PaintEventArgs) Handles PictureBox1.Paint
    Dim pt1 As New Point(50, 50)
    Dim pt2 As New Point(100, 100)
    Dim pt3 As New Point(150, 150)

    Dim sz As New Size(CInt(PictureBox1.Size.Width / 2),
                       CInt(PictureBox1.Size.Height / 2))
    n += 1
    Debug.Print(String.Format("DisplayMyStuff: {0},
            Half-Size = {1} / {2}", n, sz.Width, sz.Height))

    Dim rect1 As New Rectangle(New Point(50, 50), sz)
    Dim rect2 As New Rectangle(New Point(100, 100), sz)

    Using linGradBr As New LinearGradientBrush(pt2, pt3, Color.Yellow, Color.Blue)

        e.Graphics.Clear(Color.Bisque)

        e.Graphics.DrawRectangle(Pens.Black, rect1)
        e.Graphics.DrawRectangle(Pens.Black, rect2)

        e.Graphics.FillRectangle(Brushes.Red, rect1)
        e.Graphics.FillRectangle(linGradBr, rect2)

    End Using
End Sub

如果你确实想画的形式,然后严峻的答案是解决方案。在那里,你响应形式Paint事件。在这两种情况下,使用作为EventArg Windows提供的图形对象。

If you are actually trying to paint on the Form, then Grim's answer is the solution. There you respond to the Form Paint event. In either case, use the Graphics object provided by Windows as an EventArg.

在上面,您正在使用的图形对象的图片框(通过事件参数),所以输出的图片框。

Above, you are using the Graphics object for the PictureBox (via event args) so output is to the PictureBox.

的Windows不会知道要绘制的东西在Paint事件,所以你需要告诉它的图像需要在特定时间进行更新时,该图片大小调整等。在调整大小事件,加上:

Windows wont know you are drawing something in the Paint event, so you need to tell it that the image needs to be updated at certain times such as when the PictureBox is resized. In the resize event, add:

PictureBox1.Invalidate       ' tell windows it needs to be redrawn
' or
PictureBox1.Refresh          ' redraw now

Me.Refresh 是有点矫枉过正,因为整个表单可能并不需要重新绘制的。

Me.Refresh is a bit of overkill because the entire form likely does not need to be repainted.

这篇关于哪个事件我应该使用,以显示在画什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:22