elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Picturebox C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Picturebox C#  (Leído 9,263 veces)
MHMC777

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Picturebox C#
« en: 9 Noviembre 2014, 03:25 am »

Estoy desarrollando una aplicación donde el usuario podrá dibujar figuras dentro de un Picturebox, pero después de haber dibujado las figuras, seleccionar alguna y moverla dentro del picturebox con el mouse, pero no tengo idea de como, alguna idea???


En línea

MHMC777

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: Picturebox C#
« Respuesta #1 en: 9 Noviembre 2014, 03:31 am »



después de que estén las figuras puestas en el picturebox, debo seleccionarlas de alguna manera y moverlas con el mouse


En línea

El Benjo


Desconectado Desconectado

Mensajes: 392



Ver Perfil WWW
Re: Picturebox C#
« Respuesta #2 en: 9 Noviembre 2014, 06:56 am »

Creas una lista o un arreglo donde puedas guardar la posición y el tamaño de las figuras.

Cuando el usuario hace el dibujo de la figura dentro del pictureBox (hacer click, arrastrar y soltar) en vez de dibujar la figura guardas la información de la posición y el tamaño en la lista o array de figuras dibujadas.

Para dibujar las figuras recorres el arreglo y las dibujas todas.

Cuando el usuario quiera mover alguna comparas las coordenadas donde hizo click con las coordenas de las figuras y vez qué figura está en esas coordenadas.

Saludos.
En línea

www.es.neftis-ai.com

Sí hay un mejor lenguaje de programación y es ese con el que puedes desarrollar tus objetivos.
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.822



Ver Perfil
Re: Picturebox C#
« Respuesta #3 en: 9 Noviembre 2014, 13:56 pm »

@MHMC777:
Está prohibido el doble post.
Lee las normas del foro, por favor.




Buenas

Me he interesado bastante por el problema que tienes hasta el punto de tomarme la molestia en crear un user-control bastante configurable... ya que realmente me pareció una tarea interesante para pasar el rato xD, el control tiene transparencia (bueno, la basura transparente que ofrece WinForms), se puede elegir entre 3 diseños: Círculo, Triángulo, o Cuadrado/Rectángulo, y por supuesto se puede mover/arrastrar el control al hacer click sobre él, como veo que que las figuras de tu imagen son del mismo tamaño he supuesto que no necesitas redimensionar el control en tiempo de ejecución (aunque es algo fácil de llevar a cabo).

Aquí tienes una demo de las funcionalidades que le añadí a este control:


Por cierto el color de fondo del control (dentro del círculo/cuadrado) lo puedes poner en Transparente (Color.Transparent), se me olvidó mostrarlo en el video-demostración.



El control de usuario lo he desarrollado en VB.Net, puedes referenciar la Class desde C#, puedes compilarlo en un .dll para usarlo en C#, o también puedes convertirlo a C# usando algún conversor online (ej: Telerik converter).

Espero que te sirva, el source:
EDITO: Versión mejorada con un pequeño fallo corregido...
Código
  1. ' ***********************************************************************
  2. ' Author           : Elektro
  3. ' Last Modified On : 11-09-2014
  4. ' ***********************************************************************
  5. ' <copyright file="ElektroShape.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Imports "
  11.  
  12. Imports System.ComponentModel
  13. Imports System.Drawing.Drawing2D
  14. Imports System.Drawing.Text
  15.  
  16. #End Region
  17.  
  18. ''' <summary>
  19. ''' A custom shape control.
  20. ''' </summary>
  21. Public Class ElektroShape : Inherits Control
  22.  
  23. #Region " Properties "
  24.  
  25. #Region " Visible "
  26.  
  27.    ''' <summary>
  28.    ''' Gets the default size of the control.
  29.    ''' </summary>
  30.    ''' <value>The default size.</value>
  31.    Protected Overrides ReadOnly Property DefaultSize As Size
  32.        Get
  33.            Return New Size(100I, 100I)
  34.        End Get
  35.    End Property
  36.  
  37.    ''' <summary>
  38.    ''' Gets or sets the shape figure.
  39.    ''' </summary>
  40.    ''' <value>The shape figure.</value>
  41.    <Browsable(True)>
  42.    <EditorBrowsable(EditorBrowsableState.Always)>
  43.    <Category("Figure")>
  44.    <Description("Indicates the shape figure.")>
  45.    Public Property Figure As Figures
  46.        Get
  47.            Return Me._Figure
  48.        End Get
  49.        Set(ByVal value As Figures)
  50.            Me._Figure = value
  51.            MyBase.Refresh()
  52.        End Set
  53.    End Property
  54.    ''' <summary>
  55.    ''' The shape figure.
  56.    ''' </summary>
  57.    Private _Figure As Figures = Figures.Circle
  58.  
  59.    ''' <summary>
  60.    ''' Gets or sets a value indicating whether this <see cref="ElektroShape"/> is draggable.
  61.    ''' </summary>
  62.    ''' <value><c>true</c> if draggable; otherwise, <c>false</c>.</value>
  63.    <Browsable(True)>
  64.    <EditorBrowsable(EditorBrowsableState.Always)>
  65.    <Category("Figure")>
  66.    <Description("Determines whether this control is draggable.")>
  67.    Public Property Draggable As Boolean
  68.        Get
  69.            Return Me._Draggable
  70.        End Get
  71.        Set(ByVal value As Boolean)
  72.            Me._Draggable = value
  73.        End Set
  74.    End Property
  75.    ''' <summary>
  76.    ''' A value indicating whether this <see cref="ElektroShape"/> is draggable.
  77.    ''' </summary>
  78.    Private _Draggable As Boolean = False
  79.  
  80.    ''' <summary>
  81.    ''' Gets or sets the inner color of this <see cref="ElektroShape"/>.
  82.    ''' </summary>
  83.    ''' <value>The inner color of this <see cref="ElektroShape"/>.</value>
  84.    <Browsable(True)>
  85.    <EditorBrowsable(EditorBrowsableState.Always)>
  86.    <Category("Figure")>
  87.    <Description("The inner color of this shape.")>
  88.    Public Property InnerColor As Color
  89.        Get
  90.            Return Me._InnerColor
  91.        End Get
  92.        Set(ByVal value As Color)
  93.            Me._InnerColor = value
  94.            MyBase.Refresh()
  95.        End Set
  96.    End Property
  97.    ''' <summary>
  98.    ''' The inner color of this <see cref="ElektroShape"/>.
  99.    ''' </summary>
  100.    Private _InnerColor As Color = Control.DefaultBackColor
  101.  
  102.    ''' <summary>
  103.    ''' Gets or sets the border color of this <see cref="ElektroShape"/>.
  104.    ''' </summary>
  105.    ''' <value>The border color of this <see cref="ElektroShape"/>.</value>
  106.    ''' <PermissionSet>
  107.    '''   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
  108.    ''' </PermissionSet>
  109.    <Browsable(True)>
  110.    <EditorBrowsable(EditorBrowsableState.Always)>
  111.    <Category("Figure")>
  112.    <Description("The border color of this shape.")>
  113.    Public Property BorderColor As Color
  114.        Get
  115.            Return Me._BorderColor
  116.        End Get
  117.        Set(ByVal value As Color)
  118.            Me._BorderColor = value
  119.            Me.DoPaint()
  120.        End Set
  121.    End Property
  122.    ''' <summary>
  123.    ''' The border color of this <see cref="ElektroShape"/>.
  124.    ''' </summary>
  125.    Private _BorderColor As Color = Control.DefaultForeColor
  126.  
  127.    ''' <summary>
  128.    ''' Gets or sets the border width of this <see cref="ElektroShape"/>.
  129.    ''' </summary>
  130.    ''' <value>The border width of this <see cref="ElektroShape"/>.</value>
  131.    <Browsable(True)>
  132.    <EditorBrowsable(EditorBrowsableState.Always)>
  133.    <Category("Figure")>
  134.    <Description("The border width of this figure.")>
  135.    Public Property BorderWidth As Integer
  136.        Get
  137.            Return Me._BorderWidth
  138.        End Get
  139.        Set(ByVal value As Integer)
  140.            Me._BorderWidth = value
  141.            Me.DoPaint()
  142.        End Set
  143.    End Property
  144.    ''' <summary>
  145.    ''' The border width of the this <see cref="ElektroShape"/>.
  146.    ''' </summary>
  147.    Private _BorderWidth As Integer = 5I
  148.  
  149.    ''' <summary>
  150.    ''' Gets or sets the interpolation mode of this <see cref="ElektroShape"/>.
  151.    ''' </summary>
  152.    ''' <value>The circle's interpolation mode.</value>
  153.    <Browsable(True)>
  154.    <EditorBrowsable(EditorBrowsableState.Always)>
  155.    <Category("Quality")>
  156.    <Description("The interpolation mode of this shape.")>
  157.    Public Property InterpolationMode As InterpolationMode
  158.        Get
  159.            Return Me._InterpolationMode
  160.        End Get
  161.        Set(ByVal value As InterpolationMode)
  162.            Me._InterpolationMode = value
  163.            Me.DoPaint()
  164.        End Set
  165.    End Property
  166.    ''' <summary>
  167.    ''' The interpolation mode of this <see cref="ElektroShape"/>.
  168.    ''' </summary>
  169.    Private _InterpolationMode As InterpolationMode = Drawing2D.InterpolationMode.Default
  170.  
  171.    ''' <summary>
  172.    ''' Gets or set a value specifying how pixels are offset during rendering this <see cref="ElektroShape"/>.
  173.    ''' </summary>
  174.    ''' <value>The circle's pixel offset mode.</value>
  175.    <Browsable(True)>
  176.    <EditorBrowsable(EditorBrowsableState.Always)>
  177.    <Category("Quality")>
  178.    <Description("A value specifying how pixels are offset during rendering this shape.")>
  179.    Public Property PixelOffsetMode As PixelOffsetMode
  180.        Get
  181.            Return Me._PixelOffsetMode
  182.        End Get
  183.        Set(ByVal value As PixelOffsetMode)
  184.            Me._PixelOffsetMode = value
  185.            Me.DoPaint()
  186.        End Set
  187.    End Property
  188.    ''' <summary>
  189.    ''' A value specifying how pixels are offset during rendering this <see cref="ElektroShape"/>.
  190.    ''' </summary>
  191.    Private _PixelOffsetMode As PixelOffsetMode = Drawing2D.PixelOffsetMode.Default
  192.  
  193.    ''' <summary>
  194.    ''' Gets or sets the rendering quality of this <see cref="ElektroShape"/>.
  195.    ''' </summary>
  196.    ''' <value>The circle's smoothing mode.</value>
  197.    <Browsable(True)>
  198.    <EditorBrowsable(EditorBrowsableState.Always)>
  199.    <Category("Quality")>
  200.    <Description("The rendering quality of this shape.")>
  201.    Public Property SmoothingMode As SmoothingMode
  202.        Get
  203.            Return Me._SmoothingMode
  204.        End Get
  205.        Set(ByVal value As SmoothingMode)
  206.            Me._SmoothingMode = value
  207.            Me.DoPaint()
  208.        End Set
  209.    End Property
  210.    ''' <summary>
  211.    ''' The rendering quality of this <see cref="ElektroShape"/>.
  212.    ''' </summary>
  213.    Private _SmoothingMode As SmoothingMode = Drawing2D.SmoothingMode.Default
  214.  
  215. #End Region
  216.  
  217. #Region " Hidden "
  218.  
  219.    ''' <summary>
  220.    ''' Gets or sets the background color of the control.
  221.    ''' </summary>
  222.    ''' <value>The background color of the control.</value>
  223.    ''' <PermissionSet>
  224.    '''   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  225.    ''' </PermissionSet>
  226.    <Browsable(False)>
  227.    <EditorBrowsable(EditorBrowsableState.Never)>
  228.    <Bindable(False)>
  229.    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
  230.    <Description("The background color of the control.")>
  231.    Public Overrides Property BackColor() As Color
  232.        Get
  233.            Dim currentColor As Color = Color.White
  234.            If MyBase.Parent IsNot Nothing Then
  235.                currentColor = MyBase.Parent.BackColor
  236.            End If
  237.            Return currentColor
  238.        End Get
  239.        Set(ByVal Value As Color)
  240.            MyBase.BackColor = Value
  241.        End Set
  242.    End Property
  243.  
  244.    ''' <summary>
  245.    ''' Gets or sets the foreground color of the control.
  246.    ''' </summary>
  247.    ''' <value>The foreground color of the control.</value>
  248.    ''' <PermissionSet>
  249.    '''   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  250.    ''' </PermissionSet>
  251.    <Browsable(False)>
  252.    <EditorBrowsable(EditorBrowsableState.Never)>
  253.    <Bindable(False)>
  254.    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
  255.    <Description("The foreground color of the control.")>
  256.    Public Overrides Property ForeColor As Color
  257.        Get
  258.            Return MyBase.ForeColor
  259.        End Get
  260.        Set(ByVal value As Color)
  261.            MyBase.ForeColor = value
  262.        End Set
  263.    End Property
  264.  
  265.    ''' <summary>
  266.    ''' Gets or sets the text associated with this control.
  267.    ''' </summary>
  268.    ''' <value>The text associated with this control.</value>
  269.    <Browsable(False)>
  270.    <EditorBrowsable(EditorBrowsableState.Never)>
  271.    <Bindable(False)>
  272.    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
  273.    <Category("Figure")>
  274.    <Description("The text associated with this control.")>
  275.    Public Overrides Property Text As String
  276.        Get
  277.            Return MyBase.Text
  278.        End Get
  279.        Set(value As String)
  280.            MyBase.Text = value
  281.        End Set
  282.    End Property
  283.  
  284.    ''' <summary>
  285.    ''' Gets or sets the rendering mode for text associated with this <see cref="ElektroShape"/>.
  286.    ''' </summary>
  287.    ''' <value>The circle's text rendering hint.</value>
  288.    <Browsable(False)>
  289.    <EditorBrowsable(EditorBrowsableState.Never)>
  290.    <Category("Quality")>
  291.    <Description("The rendering mode for text associated with this control.")>
  292.    Public Property TextRenderingHint As TextRenderingHint
  293.        Get
  294.            Return Me._TextRenderingHint
  295.        End Get
  296.        Set(ByVal value As TextRenderingHint)
  297.            Me._TextRenderingHint = value
  298.            Me.DoPaint()
  299.        End Set
  300.    End Property
  301.    ''' <summary>
  302.    ''' The rendering mode for text associated with this <see cref="ElektroShape"/>.
  303.    ''' </summary>
  304.    Private _TextRenderingHint As TextRenderingHint = Drawing.Text.TextRenderingHint.SystemDefault
  305.  
  306. #End Region
  307.  
  308. #End Region
  309.  
  310. #Region " Miscellaneous Objects "
  311.  
  312.    ''' <summary>
  313.    ''' The draggable information to drag the control.
  314.    ''' </summary>
  315.    Private Property draggableInfo As DragInfo = DragInfo.Empty
  316.  
  317. #End Region
  318.  
  319. #Region " Enumerations "
  320.  
  321.    ''' <summary>
  322.    ''' Contains the default figure designs.
  323.    ''' </summary>
  324.    Public Enum Figures As Integer
  325.  
  326.        ''' <summary>
  327.        ''' Circle figure.
  328.        ''' </summary>
  329.        Circle = 0I
  330.  
  331.        ''' <summary>
  332.        ''' Square figure.
  333.        ''' </summary>
  334.        Square = 1I
  335.  
  336.        ''' <summary>
  337.        ''' Triangle figure.
  338.        ''' </summary>
  339.        Triangle = 2I
  340.  
  341.    End Enum
  342.  
  343. #End Region
  344.  
  345. #Region " Types "
  346.  
  347.    ''' <summary>
  348.    ''' Maintains information about a draggable operation.
  349.    ''' </summary>
  350.    Private NotInheritable Class DragInfo
  351.  
  352. #Region " Properties "
  353.  
  354.        ''' <summary>
  355.        ''' Gets or sets the initial mouse coordinates.
  356.        ''' </summary>
  357.        ''' <value>The initial mouse coordinates.</value>
  358.        Friend Property InitialMouseCoords As Point = Point.Empty
  359.  
  360.        ''' <summary>
  361.        ''' Gets or sets the initial location.
  362.        ''' </summary>
  363.        ''' <value>The initial location.</value>
  364.        Friend Property InitialLocation As Point = Point.Empty
  365.  
  366.        ''' <summary>
  367.        ''' Represents a <see cref="T:DragInfo"/> that is <c>Nothing</c>.
  368.        ''' </summary>
  369.        ''' <value><c>Nothing</c></value>
  370.        Friend Shared ReadOnly Property Empty As DragInfo
  371.            Get
  372.                Return Nothing
  373.            End Get
  374.        End Property
  375.  
  376. #End Region
  377.  
  378. #Region " Constructors "
  379.  
  380.        ''' <summary>
  381.        ''' Initializes a new instance of the <see cref="DragInfo"/> class.
  382.        ''' </summary>
  383.        ''' <param name="mouseCoordinates">The current mouse coordinates.</param>
  384.        ''' <param name="location">The current location.</param>
  385.        Public Sub New(ByVal mouseCoordinates As Point, ByVal location As Point)
  386.  
  387.            Me.InitialMouseCoords = mouseCoordinates
  388.            Me.InitialLocation = location
  389.  
  390.        End Sub
  391.  
  392. #End Region
  393.  
  394. #Region " Public Methods "
  395.  
  396.        ''' <summary>
  397.        ''' Return the new location.
  398.        ''' </summary>
  399.        ''' <param name="mouseCoordinates">The current mouse coordinates.</param>
  400.        ''' <returns>The new location.</returns>
  401.        Public Function NewLocation(ByVal mouseCoordinates As Point) As Point
  402.  
  403.            Return New Point(InitialLocation.X + (mouseCoordinates.X - InitialMouseCoords.X),
  404.                             InitialLocation.Y + (mouseCoordinates.Y - InitialMouseCoords.Y))
  405.  
  406.        End Function
  407.  
  408. #End Region
  409.  
  410.    End Class
  411.  
  412. #End Region
  413.  
  414. #Region " Constructors "
  415.  
  416.    ''' <summary>
  417.    ''' Initializes a new instance of the <see cref="ElektroShape"/> class.
  418.    ''' </summary>
  419.    Private Sub New()
  420.  
  421.        Me.New(Figures.Circle)
  422.  
  423.    End Sub
  424.  
  425.    ''' <summary>
  426.    ''' Initializes a new instance of the <see cref="ElektroShape"/> class.
  427.    ''' </summary>
  428.    ''' <param name="figure">The shape figure.</param>
  429.    Public Sub New(ByVal figure As Figures)
  430.  
  431.        With Me
  432.            .DoubleBuffered = True
  433.            _Figure = figure
  434.        End With
  435.  
  436.    End Sub
  437.  
  438. #End Region
  439.  
  440. #Region " Overriden Events "
  441.  
  442.    ''' <summary>
  443.    ''' Raises the <see cref="E:System.Windows.Forms.Control.MouseDown"/> event.
  444.    ''' </summary>
  445.    ''' <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data.</param>
  446.    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
  447.  
  448.        MyBase.OnMouseDown(e)
  449.        Me.StartDrag()
  450.  
  451.    End Sub
  452.  
  453.    ''' <summary>
  454.    ''' Raises the <see cref="E:System.Windows.Forms.Control.MouseMove"/> event.
  455.    ''' </summary>
  456.    ''' <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data.</param>
  457.    Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
  458.  
  459.        MyBase.OnMouseMove(e)
  460.        Me.Drag()
  461.  
  462.    End Sub
  463.  
  464.    ''' <summary>
  465.    ''' Raises the <see cref="E:System.Windows.Forms.Control.MouseUp"/> event.
  466.    ''' </summary>
  467.    ''' <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data.</param>
  468.    Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
  469.  
  470.        MyBase.OnMouseUp(e)
  471.        Me.StopDrag()
  472.  
  473.    End Sub
  474.  
  475.    ''' <summary>
  476.    ''' Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event.
  477.    ''' </summary>
  478.    ''' <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
  479.    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  480.  
  481.        With e.Graphics
  482.  
  483.            .InterpolationMode = Me._InterpolationMode
  484.            .PixelOffsetMode = Me._PixelOffsetMode
  485.            .SmoothingMode = Me._SmoothingMode
  486.            ' .TextRenderingHint = Me._TextRenderingHint
  487.  
  488.            Select Case Me._Figure
  489.  
  490.                Case Figures.Circle
  491.                    Me.DrawCircle(e.Graphics)
  492.  
  493.                Case Figures.Square
  494.                    Me.DrawSquare(e.Graphics)
  495.  
  496.                Case Figures.Triangle
  497.                    Me.DrawTriangle(e.Graphics)
  498.  
  499.            End Select ' Me._Figure
  500.  
  501.        End With ' e.Graphics
  502.  
  503.        MyBase.OnPaint(e)
  504.  
  505.    End Sub
  506.  
  507. #End Region
  508.  
  509. #Region " Private Methods "
  510.  
  511. #Region " Drag "
  512.  
  513.    ''' <summary>
  514.    ''' Initializes a drag operation.
  515.    ''' </summary>
  516.    Private Sub StartDrag()
  517.  
  518.        If (Me._Draggable) Then
  519.  
  520.            Me.draggableInfo = New DragInfo(MousePosition, MyBase.Location)
  521.  
  522.        End If
  523.  
  524.    End Sub
  525.  
  526.    ''' <summary>
  527.    ''' Drags the control.
  528.    ''' </summary>
  529.    Private Sub Drag()
  530.  
  531.        If (Me._Draggable) AndAlso (Me.draggableInfo IsNot DragInfo.Empty) Then
  532.  
  533.            MyBase.Location = New Point(Me.draggableInfo.NewLocation(MousePosition))
  534.  
  535.        End If
  536.  
  537.    End Sub
  538.  
  539.    ''' <summary>
  540.    ''' Destroys a drag operation.
  541.    ''' </summary>
  542.    Private Sub StopDrag()
  543.  
  544.        Me.draggableInfo = DragInfo.Empty
  545.  
  546.    End Sub
  547.  
  548. #End Region
  549.  
  550. #Region " Paint "
  551.  
  552.    ''' <summary>
  553.    ''' Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event for the specified control.
  554.    ''' </summary>
  555.    Private Sub DoPaint()
  556.  
  557.        Using g As Graphics = MyBase.CreateGraphics
  558.            MyBase.InvokePaint(Me, New PaintEventArgs(g, MyBase.ClientRectangle))
  559.        End Using
  560.  
  561.    End Sub
  562.  
  563.    ''' <summary>
  564.    ''' Gets a <see cref="System.Drawing.Rectangle"/> with the bounds fixed according to the specified <see cref="System.Drawing.Pen.Width"/>.
  565.    ''' </summary>
  566.    ''' <param name="pen">The <see cref="System.Drawing.Pen"/>.</param>
  567.    ''' <returns>The <see cref="System.Drawing.Rectangle"/> with the bounds fixed.</returns>
  568.    Private Function GetFigueRectangle(ByVal pen As Pen) As Rectangle
  569.  
  570.        Return New Rectangle With
  571.                    {
  572.                        .x = 0.0F + (pen.Width / 2.0F),
  573.                        .y = 0.0F + (pen.Width / 2.0F),
  574.                        .width = (CSng(MyBase.Width) - pen.Width),
  575.                        .height = (CSng(MyBase.Height) - pen.Width)
  576.                    }
  577.  
  578.    End Function
  579.  
  580.    ''' <summary>
  581.    ''' Draws a circle on the specified <see cref="System.Drawing.Graphics"/> object.
  582.    ''' </summary>
  583.    ''' <param name="g">The <see cref="System.Drawing.Graphics"/> object to paint.</param>
  584.    Private Sub DrawCircle(ByRef g As Graphics)
  585.  
  586.        With g
  587.  
  588.            Using pen As New Pen(Me._BorderColor, Me._BorderWidth)
  589.  
  590.                Dim rect As Rectangle = Me.GetFigueRectangle(pen)
  591.  
  592.                If Not Me._InnerColor = Color.Transparent Then
  593.  
  594.                    ' Fill circle background.
  595.                    Using brush As New SolidBrush(Me._InnerColor)
  596.                        .FillEllipse(brush, rect)
  597.                    End Using
  598.  
  599.                End If ' Not Me._InnerColor = Color.Transparent
  600.  
  601.                ' Draw the circle figure.
  602.                .DrawEllipse(pen, rect)
  603.  
  604.            End Using ' pen As New Pen(Me._BorderColor, Me._BorderWidth)
  605.  
  606.        End With ' g
  607.  
  608.    End Sub
  609.  
  610.    ''' <summary>
  611.    ''' Draws an square on the specified <see cref="System.Drawing.Graphics"/> object.
  612.    ''' </summary>
  613.    ''' <param name="g">The <see cref="System.Drawing.Graphics"/> object to paint.</param>
  614.    Private Sub DrawSquare(ByRef g As Graphics)
  615.  
  616.        With g
  617.  
  618.            Using pen As New Pen(Me._BorderColor, Me._BorderWidth)
  619.  
  620.                Dim rect As Rectangle = Me.GetFigueRectangle(pen)
  621.  
  622.                If Not Me._InnerColor = Color.Transparent Then
  623.  
  624.                    ' Fill the square background.
  625.                    Using brush As New SolidBrush(Me._InnerColor)
  626.                        .FillRectangle(brush, rect)
  627.                    End Using
  628.  
  629.                End If ' Not Me._InnerColor = Color.Transparent
  630.  
  631.                ' Draw the square figure.
  632.                .DrawRectangle(pen, rect)
  633.  
  634.            End Using ' pen As New Pen(Me._BorderColor, Me._BorderWidth)
  635.  
  636.        End With ' g
  637.  
  638.    End Sub
  639.  
  640.    ''' <summary>
  641.    ''' Draws a triangle on the specified <see cref="System.Drawing.Graphics"/> object.
  642.    ''' </summary>
  643.    ''' <param name="g">The <see cref="System.Drawing.Graphics"/> object to paint.</param>
  644.    Private Sub DrawTriangle(ByRef g As Graphics)
  645.  
  646.        With g
  647.  
  648.            Using pen As New Pen(Me._BorderColor, Me._BorderWidth)
  649.  
  650.                Using path As New GraphicsPath(FillMode.Alternate)
  651.  
  652.                    Dim rect As Rectangle = MyBase.ClientRectangle
  653.  
  654.                    ' Set the triangle path coordinates.
  655.                    Dim trianglePoints As PointF() =
  656.                        {
  657.                            New PointF(CSng(rect.Left + CSng(rect.Width / 2.0F)), CSng(rect.Top + pen.Width)),
  658.                            New PointF(CSng(rect.Right - pen.Width), CSng(rect.Bottom - (pen.Width / 2.0F))),
  659.                            New PointF(CSng(rect.Left + pen.Width), CSng(rect.Bottom - (pen.Width / 2.0F))),
  660.                            New PointF(CSng(rect.Left + CSng(rect.Width / 2.0F)), CSng(rect.Top + pen.Width))
  661.                        }
  662.  
  663.                    path.AddLines(trianglePoints)
  664.                    path.CloseFigure()
  665.  
  666.                    If Not Me._InnerColor = Color.Transparent Then
  667.  
  668.                        ' Fill the triangle background.
  669.                        Using brush As New SolidBrush(Me._InnerColor)
  670.                            .FillPath(brush, path)
  671.                        End Using
  672.  
  673.                    End If ' Not Me._InnerColor = Color.Transparent
  674.  
  675.                    ' Draw the triangle figure.
  676.                    .DrawPath(pen, path)
  677.  
  678.                End Using ' path As New GraphicsPath(FillMode.Alternate)
  679.  
  680.            End Using ' pen As New Pen(Me._BorderColor, Me._BorderWidth)
  681.  
  682.        End With ' g
  683.  
  684.    End Sub
  685.  
  686. #End Region
  687.  
  688. #End Region
  689.  
  690. End Class

EDITO:

Aquí tienes un ejemplo real de uso dinámico en tiempo de ejecución, creación/instanciado de controles y supscripción a eventos del ratón.

Código
  1. Public Class Form1
  2.  
  3.    Private Sub Test() Handles MyBase.Shown
  4.  
  5.        For count As Integer = 0 To 5
  6.  
  7.            Dim shape As New ElektroShape(ElektroShape.Figures.Circle)
  8.            With shape
  9.  
  10.                .Name = String.Format("shape_{0}", CStr(count))
  11.                .Size = New Size(48, 48)
  12.                .Location = New Point(Me.ClientRectangle.Left + (.Size.Width * count) + (10 * count), .Size.Height)
  13.                .Draggable = True
  14.  
  15.                .InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  16.                .PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
  17.                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  18.  
  19.                .BorderWidth = 1I
  20.                .BorderColor = Color.DodgerBlue
  21.                .InnerColor = Color.Thistle
  22.  
  23.                AddHandler shape.Click, AddressOf shape_Click
  24.  
  25.            End With ' shape
  26.  
  27.            Me.Controls.Add(shape)
  28.  
  29.        Next count
  30.  
  31.    End Sub
  32.  
  33.    ''' <summary>
  34.    ''' Handles the Click event of the shape control.
  35.    ''' </summary>
  36.    ''' <param name="sender">The source of the event.</param>
  37.    ''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  38.    Private Sub shape_Click(ByVal sender As Object, ByVal e As EventArgs)
  39.  
  40.        Dim mouseArgs = DirectCast(e, MouseEventArgs)
  41.  
  42.        Dim sb As New System.Text.StringBuilder
  43.        With sb
  44.            .AppendLine("Has hecho 'Click' en una figura !!")
  45.            .AppendLine()
  46.            .AppendFormat("Nombre: {0}", DirectCast(sender, ElektroShape).Name)
  47.            .AppendLine()
  48.            .AppendFormat("Coordenadas internas: {0}", DirectCast(e, MouseEventArgs).Location.ToString)
  49.            .AppendLine()
  50.            .AppendFormat("Coordenadas externas: {0}", MousePosition.ToString)
  51.        End With
  52.  
  53.        MessageBox.Show(sb.ToString, Me.Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
  54.  
  55.    End Sub
  56.  
  57. End Class

C# (conversión al vuelo):
Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. public class Form1
  8. {
  9.  
  10.  
  11. private void Test()
  12. {
  13.  
  14. for (int count = 0; count <= 5; count++) {
  15. ElektroShape shape = new ElektroShape(ElektroShape.Figures.Circle);
  16. var _with1 = shape;
  17.  
  18. _with1.Name = string.Format("shape_{0}", Convert.ToString(count));
  19. _with1.Size = new Size(48, 48);
  20. _with1.Location = new Point(this.ClientRectangle.Left + (_with1.Size.Width * count) + (10 * count), _with1.Size.Height);
  21. _with1.Draggable = true;
  22.  
  23. _with1.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
  24. _with1.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality;
  25. _with1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
  26.  
  27. _with1.BorderWidth = 1;
  28. _with1.BorderColor = Color.DodgerBlue;
  29. _with1.InnerColor = Color.Thistle;
  30.  
  31. shape.Click += shape_Click;
  32.  
  33. // shape
  34.  
  35. this.Controls.Add(shape);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. /// <summary>
  42. /// Handles the Click event of the shape control.
  43. /// </summary>
  44. /// <param name="sender">The source of the event.</param>
  45. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  46.  
  47. private void shape_Click(object sender, EventArgs e)
  48. {
  49. dynamic mouseArgs = (MouseEventArgs)e;
  50.  
  51. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  52. var _with2 = sb;
  53. _with2.AppendLine("Has hecho 'Click' en una figura !!");
  54. _with2.AppendLine();
  55. _with2.AppendFormat("Nombre: {0}", ((ElektroShape)sender).Name);
  56. _with2.AppendLine();
  57. _with2.AppendFormat("Coordenadas internas: {0}", ((MouseEventArgs)e).Location.ToString);
  58. _with2.AppendLine();
  59. _with2.AppendFormat("Coordenadas externas: {0}", MousePosition.ToString);
  60.  
  61. MessageBox.Show(sb.ToString, this.Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
  62.  
  63. }
  64. public Form1()
  65. {
  66. Shown += Test;
  67. }
  68.  
  69. }
  70.  
  71. //=======================================================
  72. //Service provided by Telerik (www.telerik.com)
  73. //Conversion powered by NRefactory.
  74. //=======================================================
  75.  

Saludos.
« Última modificación: 9 Noviembre 2014, 16:13 pm por Eleкtro » En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.822



Ver Perfil
Re: Picturebox C#
« Respuesta #4 en: 10 Noviembre 2014, 19:29 pm »

Bueno, aquí tienes una versión (más) mejorada y completa de este Control, puedes personalizarlo de la manera que quieras para tus intenciones...

   



Source:
https://www.mediafire.com/?0webv666y7h6b8u

Saludos.
« Última modificación: 10 Noviembre 2014, 21:08 pm por Eleкtro » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
PictureBox
Programación Visual Basic
CsarGR 3 3,036 Último mensaje 15 Diciembre 2005, 23:34 pm
por NYlOn
Manejo de Picturebox con un Timer en C#
.NET (C#, VB.NET, ASP)
romybe 2 4,131 Último mensaje 5 Diciembre 2014, 01:34 am
por romybe
dibujar 3 puntos en un picturebox
Programación Visual Basic
AnaCarolina28 2 4,448 Último mensaje 28 Enero 2015, 19:03 pm
por okik
Capturar contenido en un Picturebox
Programación Visual Basic
Brian1511 2 2,454 Último mensaje 15 Enero 2015, 19:02 pm
por Brian1511
PictureBox
Programación C/C++
pikoc 3 1,983 Último mensaje 15 Julio 2015, 01:02 am
por x64core
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines