[英]Graphics Object in vb.net
我刚刚开始在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。 但是我试图在表单加载事件中显示它们。 由于某种原因,我看不到输出信息。可能是问题所在。
永远不要使用CreateGraphics
。 我认为它实际上没有合理的用例。
您的问题是您在表单上绘制了某些内容,但是一旦触发下一次重新绘制该表单,该内容就会被覆盖,因为以这种方式创建的图形不会保留下来。
本质上 ,您必须使用Paint
事件(或OnPaint
方法)进行绘制, 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
块中。)
尝试这个:
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
我在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控件提供正确宽度的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.