Esta página está en construcción: perdonen los errores y temas inacabados.
 
This page is being developed: I am sorry for errors and unfinished subjects.

Cómo: Crear objetos Graphics para dibujar III   (incluido en Danzante)

The Line control in Visual Basic 6.0 has no equivalent in Visual Basic 2008. However, you can use graphics methods to achieve the same results, or you can use the optional Visual Basic Power Packs LineShape control.

Conceptual Differences

In Visual Basic 6.0, the Line control provides an easy way to draw lines on a form at design time. The Line control is a "lightweight control"- that is, it does not have a Windows handle, also called an HWnd.

In Visual Basic 2008, there is no equivalent for the Line control, and lightweight controls are no longer supported. However, there are ways to draw lines on a form both at design time and at run time.

Note:
The optional Visual Basic Power Packs controls include LineShape, OvalShape, and RectangleShape controls that can be used to replace the Line and Shape controls. In addition to duplicating the behavior of the Visual Basic 6.0 Line and Shape controls, these controls add new capabilities. These include gradient fills, run time selection, and run time events.

You can download the Visual Basic Power Packs from the Microsoft Visual Basic 2005 Power Packs page on the MSDN Web site.

At design time, you can draw a vertical or horizontal line on a form by adding a Label control and setting the Text property to an empty string, the BorderStyle property to None, and the Width or Height property to 1.

At run time, you can draw vertical, horizontal, or diagonal lines in a form's Paint event handler by creating a new Graphics object and calling its methods.

In Visual Basic 6.0, you can use a Line control to draw a line on top of a container control such as a PictureBox or Frame control by adding a Line control to the container.

In Visual Basic 2008, you can achieve the same effect by calling the DrawLine method in the Paint event of the container control.

Code Changes for the Line Control

The following examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Drawing Horizontal or Vertical Lines

The following code demonstrates how to draw horizontal and vertical lines on a form at run time. In the Visual Basic 6.0 example, the Line control is used; assume that two Line controls were added at design time. The Visual Basic 2008 example demonstrates two methods - using a Label control and using Graphics methods.
Note:
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
  Copy imageCopy Code
' Visual Basic 6.0
Private Sub Form_Load()
    ' Draw a horizontal line 200 twips from the top of the form.
    Line1.X1 = 0
    Line1.X2 = Me.Width
    Line1.Y1 = 200
    Line1.Y2 = 200
    Line1.BorderColor = vbRed
    Line1.BorderWidth = 1
    ' Draw a vertical line 200 twips from the left of the form.
    Line1.Y1 = 0
    Line1.Y2 = Me.Height
    Line1.X1 = 200
    Line1.X2 = 200
    Line1.BorderColor = vbBlue
    Line1.BorderWidth = 1
Visual Basic  Copy imageCopy Code
' Visual Basic 2008
' Using Label controls.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Line1 As New System.Windows.Forms.Label
    Dim Line2 As New System.Windows.Forms.Label
    ' Draw a horizontal line 14 pixels from the top of the form.
    Line1.Location = New System.Drawing.Point(0, 14)
    Line1.Size = New System.Drawing.Size(Me.Width, 1)
    Line1.BorderStyle = BorderStyle.None
    Line1.BackColor = System.Drawing.Color.Red
    Line1.Text = ""
    Controls.Add(Line1)
    ' Draw a vertical line 14 pixels from the left of the form.
    Line2.Location = New System.Drawing.Point(14, 0)
    Line2.Size = New System.Drawing.Size(1, Me.Height)
    Line2.BorderStyle = BorderStyle.None
    Line2.BackColor = System.Drawing.Color.Blue
    Line2.Text = ""
    Controls.Add(Line2)
End Sub
Visual Basic  Copy imageCopy Code
' Visual Basic 2008
    ' Using Graphics methods.
Private Sub Form1Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a horizontal line 28 pixels from the top of the form.
    e.Graphics.DrawLine(Pens.Red, 0, 28, Me.Width, 28)
    ' Draw a vertical line 28 pixels from the left of the form.
    e.Graphics.DrawLine(Pens.Blue, 28, 0, 28, Me.Height)
End Sub

Drawing a Diagonal Line

The following code demonstrates how to draw a diagonal line on a form at run time. In the Visual Basic 6.0 example, the Line control is used; assume that a Line control was added at design time. The Visual Basic 2008 example uses Graphics methods.
 
Note:
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
  Copy imageCopy Code
' Visual Basic 6.0
Private Sub Form_Load()
    ' Draw a diagonal line from the top left to the lower right.
    Line1.X1 = 0
    Line1.X2 = Me.ScaleWidth
    Line1.Y1 = 0
    Line1.Y2 = Me.ScaleHeight
    Line1.BorderColor = vbBlack
    Line1.BorderWidth = 1
End Sub
Visual Basic  Copy imageCopy Code
' Visual Basic 2008
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a diagonal line from the top left to the lower right.
    e.Graphics.DrawLine(Pens.Black, 0, 0, Me.ClientSize.Width, _
    Me.ClientSize.Height)
End Sub

Upgrade Notes

See Also

'En 2016 obtengo esto de msd..., para WINDOWS FORMS, en

https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ES-ES&k=k(SYSTEM.WINDOWS.FORMS.APPLICATION.DOEVENTS);k(VS.OBJECTBROWSER);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22)&rd=true

    Private Sub InitializePictureBox()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.PictureBox1.BorderStyle = _
            System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        Me.PictureBox1.Location = New System.Drawing.Point(72, 112)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(160, 136)
        Me.PictureBox1.TabStop = False
    End Sub

    Private Sub InitializeOpenFileDialog()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

        ' Set the file dialog to filter for graphics files.
        Me.OpenFileDialog1.Filter = _
        "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"

        ' Allow the user to select multiple images.
        Me.OpenFileDialog1.Multiselect = True
        Me.OpenFileDialog1.Title = "My Image Browser"
    End Sub

    Private Sub fileButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles FileButton.Click
        OpenFileDialog1.ShowDialog()
    End Sub


    ' This method handles the FileOK event.  It opens each file
    ' selected and loads the image from a stream into PictureBox1.
    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
     Handles OpenFileDialog1.FileOk

        Me.Activate()
        Dim file, files() As String
        files = OpenFileDialog1.FileNames

        ' Open each file and display the image in PictureBox1.
        ' Call Application.DoEvents to force a repaint after each
        ' file is read.       
        For Each file In files
            Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(file)
            Dim fileStream As System.IO.FileStream = fileInfo.OpenRead()
            PictureBox1.Image = System.Drawing.Image.FromStream(fileStream)
            Application.DoEvents()
            fileStream.Close()

            ' Call Sleep so the picture is briefly displayed,
            'which will create a slide-show effect.
            System.Threading.Thread.Sleep(2000)
        Next
        PictureBox1.Image = Nothing
    End Sub
 


 
Vuelta al Principio    Última actualización: sábado, 04 de junio de 2016    Visitantes: contador de visitas