lothos12345 Posted October 7, 2005 Posted October 7, 2005 I have a visual basic.net application I need the user to be able to click at one point on a form and drag, once they let off the click it will draw a square. However I am not sure how to accomplish this. Any programming examples would greatly be appreciated. Quote
Leaders Iceplug Posted October 8, 2005 Leaders Posted October 8, 2005 In MouseDown store the point where the user clicked (e.X and e.Y) into a point structure (let's say, P1) In MouseUp store the point where the user released (e.X and e.Y) into another point structure (let's say, P2) Create a rectangle, not a square, from these two points by calculating a left, top, right, and bottom value for the rectangle. The left is the minimum of the X coordinate of the points P1 and P2 The top is the minimum of the Y coordinate of P1 and P2 Right is the maximum of the X coordinates and Bottom is the maximum of the Y coordinates. You should now have a rectangle (let's say, R1) To draw the square, you have your choice of getting access to a graphics object. Dim GFX As Graphics GFX = Me.CreateGraphics() gets a graphics object that draws onto the form. Use this graphics object to draw a rectangle. GFX.DrawRectangle(Pens.Black, R1) :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
lothos12345 Posted October 10, 2005 Author Posted October 10, 2005 User See Thanks for the help that worked great, but I also need the user to see the rectangle has they are dragging to create it. How can this be accomplished? Any help given is greatly appreiciated. Quote
Leaders Iceplug Posted October 11, 2005 Leaders Posted October 11, 2005 You do the same thing as you did above, except that your P2 is the coordinates of the points created from MouseMove when the button is pressed. Then, to draw the rectangle, you'll want to clear the area, otherwise you'll get a slimetrail rectangle. If you have a background, draw it before you draw the rectangle. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.