"Boki" <bo******@ms21.hinet.net> wrote
I think you all knew my problem.
I don't know how to draw line @@ in VB.NET (2005 beta) , very sad.
Coudl you please help me?
Here is a simple scribble demo. Paste the code below into a new
form, under the Designer code (section). I've used white to draw
the lines on the form. But, if you want that picture to stay after
resizing, or after minimizing (ect.) then you need to keep a bitmap
in memory to redraw the image. I've used black to draw to that
so you can see when that takes effect....
Have fun!
LFS
Private Page As Bitmap
Private Mouse As Point
Private Drawing As Boolean
Private PageGrx As Graphics
Private FormGrx As Graphics
Private BlackInk As Pen = New Pen(Color.Black, 2)
Private WhiteInk As Pen = New Pen(Color.White, 2)
Private MyPen As Pen
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Page = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
ResetGraphics()
End Sub
Private Sub ResetGraphics()
If Not PageGrx Is Nothing Then PageGrx.Dispose()
PageGrx = Graphics.FromImage(Page)
PageGrx.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
If Not FormGrx Is Nothing Then FormGrx.Dispose()
FormGrx = Me.CreateGraphics
FormGrx.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
End Sub
Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
' Bail if minimized / downsizing
If Me.WindowState = FormWindowState.Minimized Then Exit Sub
If (Me.Width * Me.Height) < (Page.Width * Page.Height) Then Exit Sub
' Copy old image to new
Dim tmp As Bitmap = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim grx As Graphics = Graphics.FromImage(tmp)
grx.DrawImageUnscaled(Page, 0, 0)
grx.Dispose()
Page.Dispose()
' Start using new image
Page = tmp
ResetGraphics()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Not FormGrx Is Nothing Then
FormGrx.DrawImageUnscaled(Page, 0, 0)
End If
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
Mouse.X = e.X
Mouse.Y = e.Y
Drawing = True
If e.Button = MouseButtons.Left Then
MyPen = BlackInk
Else
MyPen = WhiteInk
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If Drawing Then
' Use MyPen as both ink pens for normal use
FormGrx.DrawLine(WhiteInk, Mouse.X, Mouse.Y, e.X, e.Y)
PageGrx.DrawLine(BlackInk, Mouse.X, Mouse.Y, e.X, e.Y)
Mouse.X = e.X
Mouse.Y = e.Y
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Drawing = False
End Sub