Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Drag and draw shapes on form using mouse in VB.net - C #.net.

Author: Maliha Atteeq
Download Source Code : 1091_code.zip

In this simple article you will learn that how to drag and draw shapes on the form using mouse in vb and C #.net.

 Mouse events:

In order to draw with the help of your mouse, you have to know about the device and their events. There are certain events associated with the mouse, depending on your requirements we can use them. The most common event of mouse is click and double clicks. For drawing with the mouse you don’t have to bother about these two events. For drawing purpose we concern with the dragging movement of the mouse.

In this article I will tell you that how we can drag and draw shapes on a form with the help of your mouse.

Drag and draw:

In order to drag the shape with mouse when it drags on the form you have to use its move, up and down events. All these events are defined; you just implement your logic behind them.

For the mouse down event, consider this fragment of code:

C#:

protected override void OnMouseDown(MouseEventArgs e)

        {

            if (region.GetBounds(CreateGraphics()).Contains(e.X, e.Y))

            {

                oldX = e.X;

                oldY = e.Y;

            }

            base.OnMouseDown(e);

        }

VB:

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)

    If region.GetBounds(CreateGraphics()).Contains(e.X, e.Y) Then

        oldX = e.X

        oldY = e.Y

    End If

    MyBase.OnMouseDown(e)

End Sub

For the mouse up event, consider this fragment of code:

C#:

protected override void OnMouseUp(MouseEventArgs e)

        {

            region.Translate(e.X - oldX, e.Y - oldY);

            oldX = e.X;

            oldY = e.Y;

            Invalidate();

            base.OnMouseUp(e);

        }

VB:

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)

    region.Translate(e.X - oldX, e.Y - oldY)

    oldX = e.X

    oldY = e.Y

    Invalidate()

    MyBase.OnMouseUp(e)

End Sub

In this article I chose a polygon to draw on the form and this polygon will move in that direction in which direction you drag your mouse.

To demonstrate make a new window application and write the following code on mouse’s onpaint event:

C#:

    protected override void OnPaint(PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            g.FillRegion(Brushes.Red, region);

            base.OnPaint(e);

        }

                 

VB:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    Dim g As Graphics = e.Graphics

    g.FillRegion(Brushes.Red, region)

    MyBase.OnPaint(e)

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP APPLICATION";

            Size = new Size(300, 200);

            BackColor = Color.White;

            GraphicsPath p = new GraphicsPath();

            p.AddPolygon(new Point[] { new Point(50, 10), new Point(10, 50), new Point(150, 100) });

            region = new Region(p);

        }

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

    Me.Text = "DEVASP APPLICATION"

    Size = New Size(300, 200)

    BackColor = Color.White

    Dim p As New GraphicsPath()

    p.AddPolygon(New Point() {New Point(50, 10), New Point(10, 50), New Point(150, 100)})

    region = New Region(p)

End Sub

This simple article tells that how to drag and draw shapes on the form using mouse in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to make a thumbnail image in VB - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net