Jump to content
Nuno Teixeira

Apagar Shapes com clique do rato ou com botão

Recommended Posts

Nuno Teixeira

Olá a todos,

Estou a construir uma aplicação no Visual Studio 2010, com várias funcionalidades. Uma delas é inserir círculos (shapes) sucessivamente numa PictureBox, nas coordenadas que previamente defini. Neste momento necessito de criar um botão que permita apagar a última shape inserida ou, em alternativa, criar uma função que me permita apagar uma dada shape clicando com o rato quando o cursor se encontra por cima da mesma. Também poderia ser a função Undo. Já tentei alguns eventos (mouseclicke, Undo, etc) mas sem sucesso. Alguma sugestão?

O meu código até agora é este:

Public Class Form1
Dim draw_options As Integer = 0
Dim pen_one As Pen
Dim brush_one As Brush
Dim hatch_brush As System.Drawing.Drawing2D.HatchBrush


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim graphicsObject As Graphics
	graphicsObject = PictureBox1.CreateGraphics()

	pen_one = New Pen(Color.Black, 2)
	brush_one = Brushes.Blue
	hatch_brush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Blue, Color.Salmon)
	draw_options = 1
	Me.Invalidate()

	Select draw_options
		Case 1
			graphicsObject.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
			graphicsObject.DrawEllipse(pen_one, 10, 10, 10, 10)
			graphicsObject.FillEllipse(Brushes.Blue, 10, 10, 10, 10)
			graphicsObject.FillEllipse(hatch_brush, 10, 10, 10, 10)
		Case 0
	End Select
End Sub

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
	Dim graphicsObject As Graphics
	graphicsObject = PictureBox1.CreateGraphics()

	pen_one = New Pen(Color.Black, 2)
	brush_one = Brushes.Blue
	hatch_brush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Sphere, Color.Blue, Color.Aqua)
	draw_options = 1
	Me.Invalidate()

	Select Case draw_options
		Case 1
			graphicsObject.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
			graphicsObject.DrawEllipse(pen_one, 50, 50, 10, 10)
			graphicsObject.FillEllipse(Brushes.Blue, 50, 50, 10, 10)
			graphicsObject.FillEllipse(hatch_brush, 50, 50, 10, 10)
		Case 0
	End Select
End Sub

End Class

Obrigado a todos.

Cumprimentos,

Nuno Teixeira

Edited by Rui Carlos
GeSHi

Share this post


Link to post
Share on other sites
vikcch

fiz aqui uma especie de undo a partir do teu code..

adicionei uma lista para receber as imagens a medida que vao sendo desenhadas e um botão para apagar a ultima da lista e desenhar as que ainda lá estão...

não deves desenhar "directamente" na picturebox (graphicsObject = PictureBox1.CreateGraphics() ).... desenha-se na bitmap e depois "envia-se" para a propriedade image da picturebox

Option Explicit On
Option Strict On

Public Class Form1
Dim draw_options As Integer = 0
Dim pen_one As Pen
Dim brush_one As Brush
Dim hatch_brush As System.Drawing.Drawing2D.HatchBrush

' Lista que irá receber Bitmaps
Private lstBitmap As New List(Of Bitmap)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 ' Criar Bitmap, recebe a imagem da PictureBox
 Dim bmp As Bitmap = New Bitmap(Me.PictureBox1.Image)
 Dim graphicsObject As Graphics = Graphics.FromImage(bmp)
 'graphicsObject = PictureBox1.CreateGraphics()

 pen_one = New Pen(Color.Black, 2)
 brush_one = Brushes.Blue
 hatch_brush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Blue, Color.Salmon)
 draw_options = 1
 Me.Invalidate()

 Select Case draw_options
	 Case 1
		 graphicsObject.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
		 graphicsObject.DrawEllipse(pen_one, 10, 10, 10, 10)
		 graphicsObject.FillEllipse(Brushes.Blue, 10, 10, 10, 10)
		 graphicsObject.FillEllipse(hatch_brush, 10, 10, 10, 10)
	 Case 0
 End Select

 ' Libertando Objecto Grafico
 graphicsObject.Dispose()
 ' Mandar bmp para a PictureBox
 PictureBox1.Image = bmp
 ' Adicionar Bitmap à Lista
 lstBitmap.Add(bmp)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 Dim bmp As Bitmap = New Bitmap(Me.PictureBox1.Image)
 Dim graphicsObject As Graphics = Graphics.FromImage(bmp)
 'graphicsObject = PictureBox1.CreateGraphics()

 pen_one = New Pen(Color.Black, 2)
 brush_one = Brushes.Blue
 hatch_brush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Sphere, Color.Blue, Color.Aqua)
 draw_options = 1
 Me.Invalidate()

 Select Case draw_options
	 Case 1
		 graphicsObject.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
		 graphicsObject.DrawEllipse(pen_one, 50, 50, 10, 10)
		 graphicsObject.FillEllipse(Brushes.Blue, 50, 50, 10, 10)
		 graphicsObject.FillEllipse(hatch_brush, 50, 50, 10, 10)
	 Case 0
 End Select

 graphicsObject.Dispose()
 PictureBox1.Image = bmp
 lstBitmap.Add(bmp)

End Sub

'Botão UNDO
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

 ' Remove ultimo da Lista
 If lstBitmap.Count > 0 Then lstBitmap.RemoveAt(lstBitmap.Count - 1)

 ' Desenha na pictureBox se houver items na Lista
 If lstBitmap.Count > 0 Then

	 For Each elipse As Bitmap In lstBitmap
		 PictureBox1.Image = elipse
	 Next

 Else
	 Dim bmp As Bitmap = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
	 PictureBox1.Image = bmp
 End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ' Criar Bitmap sem imagem e com a medida da PictureBox
 Dim bmp As Bitmap = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
 PictureBox1.Image = bmp

End Sub

End Class

diz aí se serviu

Share this post


Link to post
Share on other sites
vikcch

basta desenhar a ultima da lista.....

troca isto:

		 For Each elipse As Bitmap In lstBitmap
		 PictureBox1.Image = elipse
	 Next

por isto:

PictureBox1.Image = lstBitmap.Item(lstBitmap.Count - 1)

Share this post


Link to post
Share on other sites
Nuno Teixeira

Obrigado pelo código e pelas dicas Vikcch. Apenas vi agora o teu post. Já testei e funciona perfeitamente!

Obrigado pelo teu tempo.

Cumprimentos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.