Vb 2010 graphics.drawline [英] Vb 2010 graphics.drawline
问题描述
我正在编写一个小屏幕保护程序,使用计时器在屏幕上绘制线条。
为此,两个数据点A&从文件中读取B,并且以A和A为单位的像素的(x,y)坐标。 B计算。
从A点到B点的线是使用graphics.drawline方法绘制的。到目前为止它工作正常。
从B点到C点的下一行也是类似的。
问题是当绘制BC行时,AB行不会被保留在屏幕上。
如何使用计时器绘制线段BC,CD,DE等,同时保留以前的计时器?
我正在使用VB 2010.
基本上,我正在画一条由许多段组成的线,其中每个段都是使用计时器刻度事件绘制的。
我尝试了什么:
我试过没有在timer.tick sub中使用'refresh'。
这里是代码
I am writing a small screen saver program to draw lines on screen using a timer.
For this, two data points A & B are read from a file and the (x,y) coordinates in pixels for A & B computed.
The line from point A to B is drawn using graphics.drawline method. So far it works fine.
The next line from point B to C is drawn similarly.
The problem is that when line BC is drawn, the line AB is not retained onscreen.
How can I draw line segments BC, CD, DE and so on using a timer while retaining the previous ones ?
I am using VB 2010.
Essentially, I'm drawing a line made up of numerous segments, where each segment is to to be drawn using timer tick event.
What I have tried:
I've tried without using 'refresh' in the timer.tick sub.
here's the code
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System.Math
Imports System.Drawing.Drawing2D
Public Class frmscr
Dim greenPen2 As New Pen(Color.Green, 5)
Dim grayPen1 As New Pen(Color.Gray, 1)
Dim WithEvents tmrClock As New Timer
Public point1 As New Point
Public point2 As New Point
Private Sub frmscr_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Read input data file,reads array of GR
Call ReadTxtInputFile()
'Get screen size in pixels
scrW = Screen.PrimaryScreen.Bounds.Width.ToString
scrH = Screen.PrimaryScreen.Bounds.Height.ToString
Windows.Forms.Cursor.Hide()
'initialize variable i
i = 1
'Compute screen x,y of start point in pixels
'where xo1p is x offset in pixels
'gr(0) is 1st point of array
't1Wp is width of 1st vertical track
'grmax & grmin are max & min values of parameter array value gr(i)
xGR0 = xo1p + t1Wp * (GR(0) - GRmin) / (GRmax - GRmin)
yGR0 = 0
'set initial values of second point & its backup value
xGR1 = xGR0
xGR2 = xGR0
yGR1 = yGR0
yGR2 = yGR0
' Set Pen Styles
greenPen2.DashStyle = Drawing2D.DashStyle.Solid
grayPen1.DashStyle = Drawing2D.DashStyle.DashDotDot
'timer
tmrClock.Enabled = True
tmrClock.Interval = 50
tmrClock.Start()
End Sub
Private Sub frmscr_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Windows.Forms.Cursor.Show()
End Sub
Private Sub frmscr_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Me.Close()
End Sub
Private Sub frmscr_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Me.Close()
End Sub
Private Sub frmscr_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Me.Close()
End Sub
Private Sub frmscr_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
Me.Close()
End Sub
Private Sub frmscr_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Static OldX As Integer
Static OldY As Integer
'Determines whether the mouse was moved and whetherthe movement was large.
''If so, the screen saver is ended.
If (OldX > 0 And OldY > 0) And (Math.Abs(e.X - OldX) > 3 Or Math.Abs(e.Y - OldY) > 3) Then
Me.Close()
End If
''Assigns the current X and Y locations to OldX and OldY.
OldX = e.X
OldY = e.Y
End Sub
Private Sub DrawGrids(ByVal e As PaintEventArgs)
' Define Vertical grid points : track width in % : default values
t1W = 15
'track width in pixels
t1Wp = t1W * scrW / 100
'track x offset in pixels
xo1p = 0
' Create points that define line.
'vertical grid
Dim ii As Integer
For ii = 1 To 4 Step 1
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
For ii = 6 To 9 Step 1
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
For ii = 0 To 10 Step 5
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(Pens.Blue, point1, point2)
Next ii
'horizontal grid
nhThk = scrH / 75
For ii = 1 To nhThk Step 1
Dim point1 As New Point(0, 75 * ii)
Dim point2 As New Point(t1Wp, 75 * ii)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
'routine to draw the grid
DrawGrids(e)
'routine to draw the curve
DrawCurve(e)
End Sub
Private Sub DrawCurve(ByVal e As PaintEventArgs)
e.Graphics.DrawLine(greenPen2, xGR1, yGR1, xGR2, yGR2)
'stores
xGR1 = xGR2
yGR1 = yGR2
'clear screen after line reaches screen bottom
If i >= (scrH / 3.75) - 1 Then
i = 1
ClearForm(e)
yGR1 = 0
End If
End Sub
Private Sub ClearForm(ByVal e As PaintEventArgs)
'To clear the screen before continuing drawing
e.Graphics.Clear(Color.Black)
End Sub
Private Sub tmrClock_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles tmrClock.Tick
'increment variable i to compute screen location of next point
i = i + 1
'computes screen location (x,y) of next point
xGR2 = xo1p + t1Wp * (GR(i) - GRmin) / (GRmax - GRmin)
yGR2 = (DEPT(i) - DepthFirst) * 25
Refresh()
End Sub
End Class
推荐答案
这是一个有效的模拟:
Imports System.Drawing.Text
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Refresh()
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
DrawLines(e.Graphics)
End Sub
Dim r As Random = New Random()
Dim pens As Pen() = New Pen(4) _
{
New Pen(Brushes.Blue),
New Pen(Brushes.Red),
New Pen(Brushes.Green),
New Pen(Brushes.Cyan),
New Pen(Brushes.Purple)
}
Private Sub DrawLines(ByVal gr As Graphics)
gr.TextRenderingHint = TextRenderingHint.AntiAlias
For i As Integer = 1 To 5
gr.DrawLine(pens(r.Next(0, 4)), r.Next(0, 200), r.Next(0, 200), r.Next(0, 200), r.Next(0, 200))
Next
End Sub
End Class
我建议您的代码存在问题。
I suggest that there is an issue with your code.
其中每个段将使用计时器刻度事件绘制 - 问题是你没有保留图形对象的快照并添加到它但绘制在ne上w每次都是图形对象。这就是您没有看到任何绘图历史记录的原因。 (解决方案1评论的回答副本)。
以下是解决方案1的修改版本:
"where each segment is to to be drawn using timer tick event" - The issue is that you're not retaining a snapshot of the graphics object and adding to it but drawing on a new graphics object each time. This is why you don't see any drawing history. (copy of answer from Solution 1 comments).
Here is a modified version of Solution 1:
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Public Class Form1
Private myBitmap As Bitmap
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Refresh()
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
DrawLines(e.Graphics)
End Sub
Dim r As Random = New Random()
Dim pens As Pen() = New Pen(4) _
{
New Pen(Brushes.Blue),
New Pen(Brushes.Red),
New Pen(Brushes.Green),
New Pen(Brushes.Cyan),
New Pen(Brushes.Purple)
}
Private Sub DrawLines(ByVal gr As Graphics)
Dim myG As Graphics
If myBitmap Is Nothing Then
myBitmap = New Bitmap(ClientRectangle.Width, ClientRectangle.Height, PixelFormat.Format24bppRgb)
myG = Graphics.FromImage(myBitmap)
myG.Clear(Color.Black)
Else
myG = Graphics.FromImage(myBitmap)
End If
myG.TextRenderingHint = TextRenderingHint.AntiAlias
myG.DrawLine(pens(r.Next(0, 4)), r.Next(0, 200), r.Next(0, 200), r.Next(0, 200), r.Next(0, 200))
gr.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height)
myG.Dispose()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackColor = Color.Black
End Sub
End Class
这篇关于Vb 2010 graphics.drawline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!