Re: Anchors and Objects



Well, I read this a few times and I have to admit it flew right over my
head.

I suppose that means there are no "Object Select and Modify/Move for
Dummies" book available.

Thanks. :)

Webbiz


"Mike Williams" <Mike@xxxxxxxxxxxxxxxxx> wrote in message
news:ODpVjogVJHA.5496@xxxxxxxxxxxxxxxxxxxxxxx
"Webbiz" <nospam@xxxxxxxxxxxxxxx> wrote in message
news:ozzZk.25209$Jl5.19088@xxxxxxxxxxxxxxx

Some of my programs allow you to DRAW an item, such as
a LINE. Now drawing a line is no big deal. But I was wondering
about those that you can click on later and see that it is
SELECTED because a little box shows up on each end of the
line. If you click on either of those little boxes and while holding
down the mouse move it, the line will move and stretch with the
mouse while the other end stays put. Once you let go of the
mouse, your line is now at a new location and/or length.

The first thing you'll need to do is to write some code that allows you to
detect which of your lines (or other objects) the user has clicked on, and
you'll need to do it in such a way that there is a bit of "leeway" so that
if the user clicks reasonably close to a line or other shape you will
detect it, otherwise the user will find that he needs to be "pixel
perfect" with his mouse when attempting to select something. For simple
objects like straight lines and rectangles this sort of detection
(reasonably close to the object) is fairly straightforward using a bit of
math, but for more complex shapes the math it is not so easy. You will
also need to make sure that your detection code runs through the various
lines and other objects in the reverse order that you drew them, otherwise
you will often end up detecting selection on the wrong object (one that is
partially or wholly hidden behind another).

One method which is not what most people would call "elegant" but which
eliminates the need for any math even on complex shapes and which should
work quite well and which should more or less automatically take care of
the things I have mentioned (clicking reasonably close to a shape
regardless of its complexity and the equivalent of the ZOrder stuff) is to
draw each object twice when you create your stock chart display, once into
your main display PictureBox and once into a hidden Autoredraw PictureBox.
The outline of the second drawing, or the actual line or rectangle itself
if it is not a filled shape, should be drawn into the hidden Autoredraw
PictureBox using a draw width two pixels greater than the draw width you
used for that specific object in the main display PictureBox (this
facilitates the "approximately on the line or shape" user selection).

Each line (or other object) you draw has to be assigned a number of
course, which you can store in an array of Longs or whatever, and you can
use the same number as the draw colour for the line or shape that you draw
into the hidden Autoredraw PictureBox (drawing the actual displayed object
itself into the displayed PictureBox at whatever colour you wish). Then
you track the mouse over the main displayed PictureBox using its MouseMove
and Click etc events but when the user clicks the button (to select an
object) you read the pixel colour at that position in the hidden
Autoredraw PictureBox. This colour will tell you the number of the
"object" that the user clicked on.

Naturally if you want this code to work on systems running at 16 bit
colour depth as well as the normal full colour depth then you will need to
choose object numbers (and therefore colour values) that are actually
available on a 16 bit display. Better still, you can use the full range of
colours (allowing you to use 1, 2, 3, 4, 5, 6, 7, etc for your object
numbers and colour numbers) by using a DIBSection instead of a hidden
Autoredraw PictureBox for your offscreen drawing. DIBSections are very
easy to set up, although you will of course need to draw into them using
the equivalent API drawing methods instead of the native VB drawing
methods.

I haven't actually written any code along these lines (unless I did so
many years ago and my advancing years have caused me to forget!) but I
imagine that it should work okay. It should allow very easy "approximately
selected" detection and by its nature it should automatically take account
of the order in which you drew the objects. Once you've got the "clicked
object detection" off the ground it should be relatively simple to add
code to effectively move the shape in response to the user's movement of
the mouse. With lines and simple shapes (which is what you seem to
require) you can simply use such an object (VB Line or Shape Control)
initially in your drawing or otherwise "undraw" the existing line and
create an equivalent Line or Shape control in its place that the user can
move, and for more complex drawings you can "undraw" the existing shape
and draw the moving shape as a sort of "sprite".

Anyway, those are my initial thoughts on the subject. No doubt others will
have plenty of alternative suggestions for you.

Mike





.