Solved

Graphics - Drawing a box by dragging the cursor

Posted on 1998-06-03
8
287 Views
Last Modified: 2006-11-17
I need to allow the user to draw a box by clicking on a corner and dragging the cursor to the opposite corner.  The user must see the box being created.   I do this now, but the function draws multiple boxes and looks like crap.
0
Comment
Question by:hubermar
  • 4
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
zsi earned 100 total points
ID: 1462402
hubermar,

Create a copy of the area that the box is being drawn on and store it in a buffer.  When you draw the new box, copy the buffer back to the window, copy the new region to the buffer, and draw the box.  Repeat as necessary.

You can do this by either using a Picture object or (if you're the adventurous type), using the API (bitblts and DCCompatibleBitmaps).

If you need help with the code, I will be more than happy to assist you.

zsi
0
 

Author Comment

by:hubermar
ID: 1462403
Yes,  Some code would be excellent.  Thank you for your effort.
0
 
LVL 4

Expert Comment

by:zsi
ID: 1462404
hubermar,

I can do this for you but I won't be able to do it until Monday.  I have two projects due in the next 72 hours.

zsi
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:hubermar
ID: 1462405
Thanks a bunch...

Mark
0
 

Expert Comment

by:entretec
ID: 1462406
The following is from MSDN KB Q71488
Creating rubber bands within Visual Basic can be done using the DrawMode property. Rubber bands are lines that stretch as you move the mouse cursor from a specified point to a new location. This can be very useful in graphics programs and when defining sections of the screen for clipping routines.


MORE INFORMATION


The theory of drawing a rubber-band box is as follows:

Draw a line from the initial point to the location of the mouse cursor using:

      [form].DrawMode = 6. {INVERT}
Move the mouse cursor.
Save the DrawMode.
Set the [form].DrawMode to 6. {INVERT}
Draw the same line that was drawn in step 1. This will restore the image underneath the line.
Set the [form].DrawMode back to the initial DrawMode saved in step 3.
Repeat the cycle again.

DrawMode equal to INVERT allows the line to be created using the inverse of the background color. This allows the line to be always displayed on all colors.
The sample below will demonstrate the rubber-band line and the rubber-band box. Clicking the command buttons will allow the user to select between rubber-band line or a rubber-band box. The user will also be able to select a solid line or a dashed line.

Create and set the following controls and properties:


   Control Name   Caption      Picture
   ------------------------------------------------
   Form1          Form1        c:\windows\chess.bmp
   Command1       RubberBand
   Command2       RubberBox
   Command3       Dotted
   Command4       Solid

In the general section of your code, define the following constants:

Const INVERSE = 6       '*Characteristic of DrawMode property(XOR).
Const SOLID = 0         '*Characteristic of DrawStyle property.
Const DOT = 2           '*Characteristic of DrawStyle property.
Const TRUE = -1 Const FALSE = 0 Dim DrawBox As Integer '*Boolean-whether drawing Box or Line Dim OldX, OldY, StartX, StartY As Single '* Mouse locations
In the appropriate procedures, add the following code:

Sub Form_MouseDown (Button As Integer, Shift As Integer, X As

                                       Single, Y As Single)
   '* Store the initial start of the line to draw.
   StartX = X
   StartY = Y

   '* Make the last location equal the starting location
   OldX = StartX
   OldY = StartY
End Sub
Sub Form_MouseMove (Button As Integer, Shift As Integer, X As

                      Single, Y As Single)
   '* If the button is depressed then...
   If Button Then
      '* Erase the previous line.
      Call DrawLine(StartX, StartY, OldX, OldY)

      '* Draw the new line.
      Call DrawLine(StartX, StartY, X, Y)

      '* Save the coordinates for the next call.
      OldX = X
      OldY = Y
   End If
End Sub
Sub DrawLine (X1, Y1, X2, Y2 As Single)

   '* Save the current mode so that you can reset it on
   '* exit from this sub routine. Not needed in the sample
   '* but would need it if you are not sure what the
   '* DrawMode was on entry to this procedure.
   SavedMode% = DrawMode

   '* Set to XOR
   DrawMode = INVERSE

   '*Draw a box or line
   If DrawBox Then
      Line (X1, Y1)-(X2, Y2), , B
   Else
      Line (X1, Y1)-(X2, Y2)
   End If

   '* Reset the DrawMode
   DrawMode = SavedMode%
End Sub
Sub Form_MouseUp (Button As Integer, Shift As Integer, X As Single,

                  Y As Single)
   '* Stop drawing lines/boxes.
   StartEvent = FALSE
End Sub
Sub Command2_Click ()

   '* Boolean value to determine whether to draw a line or box.
   DrawBox = TRUE
End Sub
Sub Command1_Click ()

   '* Boolean value to determine whether to draw a line or box.
   DrawBox = FALSE
End Sub
Sub Command3_Click ()

   '* Create a dotted line
   Form1.DrawStyle = DOT
End Sub
Sub Command4_Click ()

   '* Create a solid line.
   Form1.DrawStyle = SOLID
End Sub

0
 

Author Comment

by:hubermar
ID: 1462407
I am very grateful.  Not only did you answer the question, but you provided the code to boot.  I had to change a few things duue to compiler errors, but they were minor.  Your commitment to helping others is worthy of praise.

Thanks,
hubermar
0
 
LVL 4

Expert Comment

by:zsi
ID: 1462408
And to think, I did it all without entering a single keystroke.

hubermar, I did not post the code - entretec did.  He should have gotten the points.

entretec, e-mail me at expert@zs.com to resolve this.

zsi
0
 

Author Comment

by:hubermar
ID: 1462409
I'm so sorry about being a freeking idiot.  I guess old age is creeping up on me.  

I have already implemented the suggested method into my program and it works great.

sorry again zsi and thanks a bunch entretec...

hubermar
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question