Rank: Advanced Member
Groups: Member, Administration, Moderator Joined: 7/28/2003(UTC) Posts: 1,659
Thanks: 5 times Was thanked: 76 time(s) in 74 post(s)
|
I have made a code sample in which we can draw shapes (lines, ellipses and rectangles) without flickering using DoubleBufferPaint event. Screenshot:  Use this code: Code:Private blnMouseDown As Boolean
Dim objGraphics As Graphics
'Start point
Private lngStartX As Long, lngStartY As Long
'End point
Private lngEndX As Long, lngEndY As Long
Private lngPenWeight As Long
Private lngPenColor As Long
Private lngBrushColor As Long
Private Sub Form_Load()
blnMouseDown = False
'Create graphics to draw on the control
Set objGraphics = New Graphics
objGraphics.Engine = DrawingEngineGdiplus
BitmapViewer1.Bitmap.Graphics.Engine = DrawingEngineGdiplus
BitmapViewer1.Bitmap.CreateNew 800, 600, Format24bppRgb
lngPenWeight = 5
lngPenColor = &H80FF0000
lngBrushColor = &H800000FF
End Sub
Private Sub Form_Resize()
'Resize BitmapViewer control on form resize
BitmapViewer1.Height = Me.ScaleHeight - 16
BitmapViewer1.Width = Me.ScaleWidth - 216
End Sub
Private Sub CommandLoad_Click()
'Load image from file
If FileDialog1.ShowOpen Then
On Error Resume Next
BitmapViewer1.Bitmap.LoadFromFile FileDialog1.FileName
If Err.Number <> 0 Then
MsgBox "Error arrised during loading image."
Exit Sub
End If
'We need to convert to Format24bppRgb to ensure that
'Graphics will be able to draw on the bitmap. Currently
'Graphics (both GDI and GDI+) can work on Format24bppRgb,
'Format32bppRgb, Format32bppArgb, Format32bppPArgb
'pixel formats
BitmapViewer1.Bitmap.Data.ConvertTo24bppRgb True
End If
End Sub
Private Sub CommandCreateNew_Click()
'Create empty bitmap
BitmapViewer1.Bitmap.CreateNew 800, 600, Format24bppRgb
End Sub
Private Sub ComboZoom_Click()
BitmapViewer1.Zoom = CLng(Left(ComboZoom.Text, Len(ComboZoom.Text) - 2)) / 100
End Sub
Private Sub OptionGdi_Click()
'Change drawing engine to GDI
objGraphics.Engine = DrawingEngineGdi
BitmapViewer1.Bitmap.Graphics.Engine = DrawingEngineGdi
'As GDI does not support alpha channels, we should disable
'the controls which specify opacity
LabelPenOpacity.Enabled = False
ComboPenOpacity.Enabled = False
LabelBrushOpacity.Enabled = False
ComboBrushOpacity.Enabled = False
End Sub
Private Sub OptionGdiplus_Click()
'Change drawing engine to GDI+
objGraphics.Engine = DrawingEngineGdiplus
BitmapViewer1.Bitmap.Graphics.Engine = DrawingEngineGdiplus
'GDI+ supports alpha channels, so we should enable
'the controls which specify opacity
LabelPenOpacity.Enabled = True
ComboPenOpacity.Enabled = True
LabelBrushOpacity.Enabled = True
ComboBrushOpacity.Enabled = True
End Sub
Private Sub CommandPenColorSelect_Click()
'Set initial color to the Color Dialog (it will be displayed
'as "Old Color" in the Color Dialog)
ColorDialog1.ColorArgb = lngPenColor
If ColorDialog1.Show Then
ShapePenColor.FillColor = ColorDialog1.ColorColorref
'Specify opacity (alpha channel) for the selected color
lngPenColor = BitmapViewer1.Bitmap.Color.ModifyAlpha(ColorDialog1.ColorArgb, CByte(ComboPenOpacity.Text))
End If
End Sub
Private Sub ComboPenOpacity_Click()
'Update opacity (alpha component) of the pen color
lngPenColor = BitmapViewer1.Bitmap.Color.ModifyAlpha(lngPenColor, CByte(ComboPenOpacity.Text))
End Sub
Private Sub ComboPenWeight_Click()
lngPenWeight = ComboPenWeight.Text
End Sub
Private Sub CommandBrushColorSelect_Click()
'Set initial color to the Color Dialog (it will be displayed
'as "Old Color" in the Color Dialog)
ColorDialog1.ColorArgb = lngBrushColor
If ColorDialog1.Show Then
ShapeBrushColor.FillColor = ColorDialog1.ColorColorref
'Specify opacity (alpha channel) for the selected color
lngBrushColor = BitmapViewer1.Bitmap.Color.ModifyAlpha(ColorDialog1.ColorArgb, CByte(ComboBrushOpacity.Text))
End If
End Sub
Private Sub ComboBrushOpacity_Click()
'Update opacity (alpha component) of the brush color
lngBrushColor = BitmapViewer1.Bitmap.Color.ModifyAlpha(lngBrushColor, CByte(ComboBrushOpacity.Text))
End Sub
Private Sub BitmapViewer1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
blnMouseDown = True
'Set start coordinate of the shape to draw
lngStartX = x
lngStartY = y
'Set end coordinate of the shape to draw
lngEndX = x
lngEndY = y
'FastRefresh method will fire DoubleBufferPaint event. Here we can
'redraw the shape
BitmapViewer1.FastRefresh
End Sub
Private Sub BitmapViewer1_MouseMove(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
If blnMouseDown Then
'Set end coordinate of drawn shape
lngEndX = x
lngEndY = y
'FastRefresh method will fire DoubleBufferPaint event. Here we can
'redraw the shape
BitmapViewer1.FastRefresh
End If
End Sub
Private Sub BitmapViewer1_MouseUp(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
If blnMouseDown Then
'Convert start and end coordinates from control coordinate system to bitmap one
BitmapViewer1.ControlToBitmapCoords lngStartX, lngStartY, lngStartX, lngStartY
BitmapViewer1.ControlToBitmapCoords lngEndX, lngEndY, lngEndX, lngEndY
blnMouseDown = False
'Adjust pen and brush
BitmapViewer1.Bitmap.Graphics.Brush.PrimaryColor = lngBrushColor
BitmapViewer1.Bitmap.Graphics.Pen.ForeColor = lngPenColor
BitmapViewer1.Bitmap.Graphics.Pen.Weight = lngPenWeight
'Draw line on bitmap
If OptionLine.Value Then
BitmapViewer1.Bitmap.Graphics.DrawLine lngStartX, lngStartY, lngEndX, lngEndY
'Draw rectangle on bitmap
Else
If lngStartX < lngEndX Then
lngLeft = lngStartX
lngWidth = lngEndX - lngStartX
Else
lngLeft = lngEndX
lngWidth = lngStartX - lngEndX
End If
If lngStartY < lngEndY Then
lngTop = lngStartY
lngHeight = lngEndY - lngStartY
Else
lngTop = lngEndY
lngHeight = lngStartY - lngEndY
End If
If OptionRectangle.Value Then
BitmapViewer1.Bitmap.Graphics.DrawRectangle lngLeft, lngTop, _
lngWidth, lngHeight, True, True
'Draw ellipse on bitmap
Else
BitmapViewer1.Bitmap.Graphics.DrawEllipse lngLeft, lngTop, _
lngWidth, lngHeight, True, True
End If
End If
End If
End Sub
Private Sub BitmapViewer1_DoubleBufferPaint(ByVal Hdc As stdole.OLE_HANDLE)
If blnMouseDown Then
'Attach Graphics to the temporary DC (control's double buffer)
objGraphics.Hdc = Hdc
'Adjust pen and brush
objGraphics.Brush.PrimaryColor = lngBrushColor
objGraphics.Pen.ForeColor = lngPenColor
'Scale pen weight proportionally to zoom
objGraphics.Pen.Weight = lngPenWeight * BitmapViewer1.Zoom
'Draw line on control
If OptionLine.Value Then
objGraphics.DrawLine lngStartX, lngStartY, lngEndX, lngEndY
Else
Dim lngLeft As Long, lngTop As Long, lngWidth As Long, lngHeight As Long
If lngStartX < lngEndX Then
lngLeft = lngStartX
lngWidth = lngEndX - lngStartX
Else
lngLeft = lngEndX
lngWidth = lngStartX - lngEndX
End If
If lngStartY < lngEndY Then
lngTop = lngStartY
lngHeight = lngEndY - lngStartY
Else
lngTop = lngEndY
lngHeight = lngStartY - lngEndY
End If
'Draw rectangle on control
If OptionRectangle.Value Then
objGraphics.DrawRectangle lngLeft, lngTop, lngWidth, lngHeight, True, True
'Draw ellipse on control
Else
objGraphics.DrawEllipse lngLeft, lngTop, lngWidth, lngHeight, True, True
End If
End If
End If
End Sub
You can download GraphicsMill_DrawingShapes.zip with full project code. Edited by user Monday, December 21, 2009 3:12:04 AM(UTC)
| Reason: Not specified File Attachment(s): Fedor attached the following image(s): |
Best regards, Fedor Skvortsov
|