vb.net 中的图形对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14873293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Graphics Object in vb.net
提问by Pradeep
I just started creating graphics in vb.net. I created a windows form and double clicked on it. I then got a method which was Form Loadmethod. In this method i wrote the following code.
我刚开始在 vb.net 中创建图形。我创建了一个 Windows 窗体并双击它。然后我得到了一种方法,即Form Load方法。在这种方法中,我编写了以下代码。
Dim g As Graphics
g = Me.CreateGraphics
Dim pencolor As New Pen(Color.Red)
g.DrawLine(pencolor, 10, 20, 100, 200)
I know that Graphics must be created in Paintevent. But i am trying to display them in the Form LoadEvent. For some reason i don't see the output What could possibly be the problem..??
我知道必须在Paint事件中创建 Graphics 。但我试图在表单加载事件中显示它们。出于某种原因,我没有看到输出可能是什么问题..??
采纳答案by Konrad Rudolph
Don't use CreateGraphics– ever. I don't think it actually has a legitimate use-case.
不要使用CreateGraphics–永远。我认为它实际上没有合法的用例。
Your problem is that you paint something on the form but it's over-written as soon as the next redrawing of the form is triggered because graphics you create that way are not persisted.
您的问题是您在表单上绘制了一些东西,但是一旦触发了表单的下一次重绘,它就会被覆盖,因为您以这种方式创建的图形不会持久化。
You essentially haveto use the Paintevent (or the OnPaintmethod) for drawing, there's no way around this. If you want to trigger a redraw in Form_Loadyou can simply call Me.Invalidate(but I think that should be redundant).
您基本上必须使用Paint事件(或OnPaint方法)进行绘图,这是没有办法解决的。如果你想触发重绘,Form_Load你可以简单地调用Me.Invalidate(但我认为这应该是多余的)。
Inside the OnPaintmethod(or the Paintevent), use the Graphicsobject provided in the parameters:
在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
(Notice that Penis a disposable resource and as such you should wrap it in a Usingblock.)
(请注意,这Pen是一个一次性资源,因此您应该将其包装在一个Using块中。)
回答by MadsHaupt
Try this:
尝试这个:
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
回答by Michael
I have extended this nice answer in VB.net (2010) to deal with autosizing of the text in a DataGridView control.
我在 VB.net (2010) 中扩展了这个不错的答案来处理 DataGridView 控件中文本的自动调整大小。
First, modify the function to be:
首先,修改函数为:
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
The next portion, in the software that calls this function is like this:
接下来的部分,在调用这个函数的软件中是这样的:
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
This works fine to provide the DataGridView control with correctly wide columns.
这可以很好地为 DataGridView 控件提供正确宽的列。

