2

我刚开始在 vb.net 中创建图形。我创建了一个 Windows 窗体并双击它。然后我得到了一个方法,它是表单加载方法。在这种方法中,我编写了以下代码。

    Dim g As Graphics
    g = Me.CreateGraphics

    Dim pencolor As New Pen(Color.Red)
    g.DrawLine(pencolor, 10, 20, 100, 200)

我知道必须在Paint事件中创建 Graphics。但我试图在表单加载事件中显示它们。出于某种原因,我看不到输出可能是什么问题..??

4

3 回答 3

1

不要使用CreateGraphics-永远。我认为它实际上没有合法的用例。

您的问题是您在表单上绘制了一些东西,但是一旦触发了下一次重新绘制表单,它就会被覆盖,因为您以这种方式创建的图形不会被保留。

您基本上必须使用Paint事件(或OnPaint方法)进行绘图,没有办法解决这个问题。如果你想触发重绘,Form_Load你可以简单地调用Me.Invalidate(但我认为这应该是多余的)。

OnPaint方法(或Paint事件)内部,使用Graphics参数中提供的对象:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e) ' First, let the base class do its thing

    Dim g = e.Graphics

    Using p As New Pen(Color.Red)
        g.DrawLine(…)
    End Using
End Sub 

(请注意,这Pen是一次性资源,因此您应该将其包装在一个Using块中。)

于 2013-02-14T11:28:25.640 回答
1

试试这个:

Public Class Form1

    Dim painted As Boolean = False

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        If Not painted Then
            painted = True
            e.Graphics.Clear(Color.Blue)
        End If
    End Sub

End Class
于 2013-10-08T11:13:33.553 回答
-1

我在 VB.net (2010) 中扩展了这个不错的答案,以处理 DataGridView 控件中文本的自动调整大小。

首先,将函数修改为:

Private Function MeasureTextWidth(ByVal c As Control, ByVal text As String) As Integer
    If (c Is DBNull.Value) Then
        Return -1
    Else
        Dim g As Graphics = c.CreateGraphics
        Return CInt(Math.Ceiling(g.MeasureString(text, c.Font).Width))
    End If
End Function

下一部分,在调用这个函数的软件中是这样的:

    With frm_Parameters_FITs
        .Show()
        With .DataGridView1
            .SuspendLayout()
            .DataSource = Nothing '  CRITICAL
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToResizeRows = False
            .AllowUserToOrderColumns = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ReadOnly = True
            .MultiSelect = False
            .RowHeadersVisible = False
            .Columns.Clear()
            .Rows.Clear()
            ' setup columns
            .Columns.Add("Item", "Item#")
            .Columns(0).Width = 11
            .Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
            .Columns.Add("Parameter", "gHeaderTitle") ' (ColName, HeaderText)
            .Columns(1).Width = 25
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
            .Columns.Add("ParamValue", "gHeaderInfo")
            .Columns(2).Width = 40
            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
            .Columns.Add("Comments", "gHeaderComment")
            .Columns(3).Width = 50
            .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
            .Columns.Add("Comments", "Comments")
            .Columns(4).Width = 60
            .Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable
            .ResumeLayout(True)
        End With
        'Then you can add the data in rows to the cells of the DataGridView:
        Dim piIndex As Integer ' this is a row pointer
        Dim ColTextWidth(5) As Integer
        Dim TextToMeasure As String
        Dim IntTextWidthPixels As Integer

        With .DataGridView1
            For i As Integer = 1 To Globals.giHeaderLines
                .Rows.Add() ' increases the rows.count...the last index is (rows.count-1)
                piIndex = .Rows.Count - 1  ' pointer to the last row just added
                .Rows(piIndex).Cells(0).Value = i.ToString ' puts this text into the col 0 cell

                .Rows(piIndex).Cells(0).Style.WrapMode = DataGridViewTriState.True
                .Rows(piIndex).Cells(1).Value = gHeaderTitle(i)
                .Rows(piIndex).Cells(2).Value = gHeaderInfo(i)
                .Rows(piIndex).Cells(3).Value = gHeaderComment(i)
                .Rows(piIndex).Cells(4).Value = gHeaderComment(i)
                ' now determine the correct col width
                For j As Integer = 0 To 4
                    Try
                        TextToMeasure = .Rows(piIndex).Cells(j).Value
                        IntTextWidthPixels = MeasureTextWidth(frm_Parameters_FITs.DataGridView1, TextToMeasure)

                        If IntTextWidthPixels > ColTextWidth(j) Then
                            ColTextWidth(j) = IntTextWidthPixels
                        End If
                    Catch ex As Exception
                        Debug.Print("Error here in Class_FileIO_FITs. PutDataIntoHeaderDataGrid")
                    End Try
                Next j
            Next i
            ' now reset the cols to the correct width
            For j As Integer = 0 To 4
                .Columns(j).Width = ColTextWidth(j)
            Next j
        End With
        'make sure the row we added is visible
        .DataGridView1.FirstDisplayedScrollingRowIndex = piIndex
        .DataGridView1.ClearSelection()
        .DataGridView1.Rows(piIndex).Selected = True
        .Refresh()
    End With

这可以很好地为 DataGridView 控件提供正确宽的列。

于 2017-08-05T01:29:14.280 回答