RE:Pintar sobre un picture box
con el siguiente codigo logro tener llebar un control de los puntos de cada uno de los rectangulos dibujados, les pongo un label, y una escala cuando se le da un resize manda un msgbox con el % de incriemento por cada lado.... y tiene 2 botones uno ke dice Repintar para volver a pintar los rectangulos desde del redraw (esto por ke no se cual evento se dispara despues del resize) y otro ke sirve para Eliminar (esto por ke no se ejecuta el evento on keyup, o keydown, esta raro) y sirve para eliminar rectangulos, solo le ingresamos el label del rectangulo ke se kiere eliminar nos confirma y se elimina del arreglo de rectangulos, se tiene ke dar repintar para volver a pintar los rectangulos y tambien un resize por ke si no aparecen aun los rectangulos eliminados.
Existen dos tipos de rectangulos Horizontal (click izquierdo) y Vertical (click Derecho). Lo ke kiero ahora es ke el Boton de Eliminar funciones directamente con la tecla Supr o Del por ke el evento keyup, kydown no se dispara y ke el Repintar se llame o se ejecute automaticamente despues del resize.... y que no se pueda agregar un rectangulo en donde ya existe uno.... alguna sugerencia?
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Dibuja
Inherits Form
Dim I As Integer = 0
Dim Arr(100, 2) As Integer
Dim Ancho As Integer = 0
Dim Alto As Integer = 0
Dim CajaAncho As Single = 0.1
Dim CajaAlto As Single = 0.035
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Dim gp As Graphics
Sub New()
InitializeComponent()
PictureBox1.Width = Me.Width
PictureBox1.Height = Me.Height
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Dibuja))
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(0, 29)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(295, 247)
Me.PictureBox1.TabIndex = 1
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Repintar"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(81, 0)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Eliminar"
Me.Button2.UseVisualStyleBackColor = True
'
'Dibuja
'
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Dibuja"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Ancho = ((Me.Width - PictureBox1.Width) * 100) / PictureBox1.Width
Alto = ((Me.Height - PictureBox1.Height) * 100) / PictureBox1.Height
Dim W As Integer
For W = 0 To I - 1
Arr(W, 0) *= (Ancho / 100) + 1
Arr(W, 1) *= (Alto / 100) + 1
Next W
PictureBox1.Width = Me.Width
PictureBox1.Height = Me.Height
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
MsgBox("Alto= " & Alto & " Ancho= " & Ancho)
End Sub
Public Sub DibujarCajas(Optional ByVal Tipo As Integer = 0, Optional ByVal X As Integer = 0, Optional ByVal Y As Integer = 0)
gp = PictureBox1.CreateGraphics
Dim pn As New Pen(Color.Orange, 2)
Dim W As Integer
If I = 1 Then
If Tipo = 0 Then
gp.DrawRectangle(pn, New Rectangle(X, Y, PictureBox1.Width * CajaAncho, PictureBox1.Height * CajaAlto))
gp.FillRectangle(Brushes.Red, New Rectangle(X, Y, PictureBox1.Width * CajaAncho, PictureBox1.Height * CajaAlto))
gp.DrawString(Str(W), Font, Brushes.Cyan, Arr(W, 0), Arr(W, 1))
ElseIf Tipo = 1 Then
gp.DrawRectangle(pn, New Rectangle(X, Y, PictureBox1.Width * CajaAlto, PictureBox1.Height * CajaAncho))
gp.FillRectangle(Brushes.Red, New Rectangle(X, Y, PictureBox1.Width * CajaAlto, PictureBox1.Height * CajaAncho))
gp.DrawString(Str(W), Font, Brushes.Cyan, Arr(W, 0), Arr(W, 1))
End If
Else 'entonces ya hay cajas antes y recorremos el arreglo de las N cajas
For W = 0 To I - 1
If Arr(W, 2) = 0 Then
gp.DrawRectangle(pn, New Rectangle(Arr(W, 0), Arr(W, 1), PictureBox1.Width * CajaAncho, PictureBox1.Height * CajaAlto))
gp.FillRectangle(Brushes.Red, New Rectangle(Arr(W, 0), Arr(W, 1), PictureBox1.Width * CajaAncho, PictureBox1.Height * CajaAlto))
gp.DrawString(Str(W), Font, Brushes.Cyan, Arr(W, 0), Arr(W, 1))
ElseIf Arr(W, 2) = 1 Then
gp.DrawRectangle(pn, New Rectangle(Arr(W, 0), Arr(W, 1), PictureBox1.Width * CajaAlto, PictureBox1.Height * CajaAncho))
gp.FillRectangle(Brushes.Red, New Rectangle(Arr(W, 0), Arr(W, 1), PictureBox1.Width * CajaAlto, PictureBox1.Height * CajaAncho))
gp.DrawString(Str(W), Font, Brushes.Cyan, Arr(W, 0), Arr(W, 1))
End If
Next W
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DibujarCajas()
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Arr(I, 0) = e.X
Arr(I, 1) = e.Y
Arr(I, 2) = 0
I += 1
DibujarCajas(0, e.X, e.Y)
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
Arr(I, 0) = e.X
Arr(I, 1) = e.Y
Arr(I, 2) = 1
I += 1
DibujarCajas(1, e.X, e.Y)
End If
End Sub
Private Sub Dibuja_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.A Then
Dim Del As Integer
Del = InputBox("Escribe la caja a eliminar: ", "Eliminar", "-1")
If Not Del = -1 Then
'cancelado
ElseIf Del <= I Then
If MsgBox("Seguro de eliminar la caja: " & Del, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim Z As Integer
For Z = Del To I - 1
Arr(Z, 0) = Arr(Z + 1, 0) 'x
Arr(Z, 1) = Arr(Z + 1, 1) 'y
Arr(Z, 2) = Arr(Z + 1, 2) 'tipo
I -= 1
Next
End If
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Del As Integer
Del = InputBox("Escribe la caja a eliminar: ", "Eliminar", "-1")
If Del = -1 Then
'cancelado
ElseIf Del <= I Then
If MsgBox("Seguro de eliminar la caja: " & Del, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim Z As Integer
For Z = Del To I - 1
Arr(Z, 0) = Arr(Z + 1, 0) 'x
Arr(Z, 1) = Arr(Z + 1, 1) 'y
Arr(Z, 2) = Arr(Z + 1, 2) 'tipo
Next
I -= 1
End If
End If
End Sub
End Class