1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
Imports System.Drawing.Drawing2D
Public Class Form2
Private EchelleCourante1 As Single = 0.5
Private EchelleCourante2 As Single = 1.5
Private UniteMillimetre As GraphicsUnit = GraphicsUnit.Millimeter
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.Panel1.Location = New Point(50, 50)
Me.Panel1.Size = New Size(400, 400)
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim g As Graphics = e.Graphics
g.Clear(Color.White)
' Set page units and scale
g.PageUnit = UniteMillimetre
g.PageScale = EchelleCourante1 ' 1 unit => 0.5 millimetre
Dim blackPen As Pen = New Pen(Color.Black, 0)
' Specify units in Millimtres
Dim rect1 As RectangleF = New RectangleF(10, 10, 50.0, 30.0)
' Draw in Millimtre
g.DrawRectangle(blackPen, rect1.X, _
rect1.Y, rect1.Width, rect1.Height)
g.PageScale = EchelleCourante2 ' 1 unit => 1.5 Millimetre
' Draw in Millimetre
g.DrawRectangle(blackPen, rect1.X, _
rect1.Y, rect1.Width, rect1.Height)
g.PageScale = EchelleCourante1
Dim points() As PointF = New PointF() {
New PointF(Me.ClientSize.Width, Me.ClientSize.Height)}
' Convert client size to Millimetres from pixels
g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, points)
Dim clientRect As RectangleF = New RectangleF(0, 0, points(0).X, points(0).Y)
g.DrawRectangle(Pens.Red, Rectangle.Ceiling(clientRect))
If IsDrawing Then
g.DrawEllipse(Pens.Blue, Rectangle.Ceiling(ellip))
End If
End Sub
Private Sub Panel1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.Resize
Me.Panel1.Refresh()
End Sub
Private IsDrawing As Boolean = False
Private ellip As RectangleF
Private StartPoint As PointF
Private EndPoint As PointF
Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
IsDrawing = True
StartPoint = PixelToMillimetre(e.Location)
EndPoint = StartPoint
ellip = New RectangleF(StartPoint.X, StartPoint.Y,
Math.Abs(StartPoint.X - EndPoint.X),
Math.Abs(StartPoint.Y - EndPoint.Y))
Me.Panel1.Refresh()
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If Not IsDrawing Then Return
EndPoint = PixelToMillimetre(e.Location)
ellip = New RectangleF(StartPoint.X, StartPoint.Y,
Math.Abs(StartPoint.X - EndPoint.X),
Math.Abs(StartPoint.Y - EndPoint.Y))
Me.Panel1.Refresh()
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
If Not IsDrawing Then Return
EndPoint = PixelToMillimetre(e.Location)
ellip = New RectangleF(StartPoint.X, StartPoint.Y,
Math.Abs(StartPoint.X - EndPoint.X),
Math.Abs(StartPoint.Y - EndPoint.Y))
Me.Panel1.Refresh()
IsDrawing = False
End Sub
Private Function PixelToMillimetre(ByVal pt As Point) As PointF
Dim pts() As PointF = {pt}
Dim g As Graphics = Me.Panel1.CreateGraphics
g.PageUnit = UniteMillimetre
g.PageScale = EchelleCourante1
g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, pts)
g.Dispose()
Return pts(0)
End Function
End Class |
Partager