Click here to Skip to main content
13,447,415 members (38,611 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Thanks in advance

I want to make an application which will draw shapes according to user entered dimension (width , Height), then user can place it on the form according to his
wish(he can drag/move left , right or top/bottom) with the mouse with 1/2 D interaction.

Need advice that i should do this in WPF or Winforms(Is it possible to do it in Winforms). Just going to start working on GDI etc so need help in going to right direction.

Any recommendations / suggestions will be highly appreciated
Posted 10-Feb-13 0:12am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Yes it's possible in winforms:
I would start by creating a UserControl called Shape which draws itself via it's Paint event - you can then set it's location, size and width and so forth from outside, and handle its Resize event to cause a redraw by calling the Invalidate method.

You then just create each instance of the Shape you need, and add it to the form (or panel, or whatever) Controls Collection in the normal way.
εїзεїзεїз 10-Feb-13 7:28am
Thanks , i will try
OriginalGriff 10-Feb-13 7:39am
You're welcome!
εїзεїзεїз 10-Feb-13 9:03am
I used a button and was able to drag and drop it on mouse up and down event, but how i will drag the box which i have drawn and re position it on users location, i hope so i am able to clarify , here is the code for drawing rectangular and
button drag.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Sub Draw()
Dim graphic As Graphics = CreateGraphics()
graphic.FillRectangle(New SolidBrush(Color.Black), 10, 10, 4, 40)
graphic.FillRectangle(New SolidBrush(Color.Black), 40, 10, 4, 40)
graphic.FillRectangle(New SolidBrush(Color.Black), 10, 46, 34, 4)
graphic.FillRectangle(New SolidBrush(Color.Black), 10, 10, 34, 4)
End Sub

--For button Drag

#Region "Declaration"
Dim startx As Integer
Dim starty As Integer
Dim endy As Integer
Dim endx As Integer
Dim finalx As Integer
Dim finaly As Integer
Dim mdown As Boolean
Dim valx As Boolean
Dim valy As Boolean
#End Region

Private Sub BtnDrag_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles BtnDrag.MouseDown
startx = MousePosition.X
starty = MousePosition.Y
mdown = True
valx = False
valy = False
End Sub
Private Sub BtnDrag_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles BtnDrag.MouseMove
If mdown = True Then
endx = (MousePosition.X - Me.Left)
endy = (MousePosition.Y - Me.Top)

If valy = False Then
starty = endy -
valy = True
End If
If valx = False Then
startx = endx - sender.left
valx = True
End If
sender.left = endx - startx = endy - starty

End If
End Sub
Private Sub BtnDrag_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles BtnDrag.MouseUp
mdown = False
valx = False
valy = False
End Sub
OriginalGriff 10-Feb-13 9:32am
As I suggested, create a UserControl - a HollowBox is probably a good name.
Move the control, rather than trying to re-draw your box each time.
εїзεїзεїз 11-Feb-13 0:47am
Visual Basic Power Packs have line , Oval, rectangle shape controls which has mouse down, up and other events , Its much easier to use them and let user drag anywhere on the canvas.Thanks to OriginalGriff for giving the idea

2D part is still pending , Working on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180318.3 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100