In article <
XXXX@XXXXX.COM >, Miles wrote:
Quote
I am studying to draw some region with Windows Api , polygons and rects.
The problem is how can I edit a existed Region? for Example,how to drag a
point or an edge of a region to resize it, just like a resizable control do?
Could anyone can help give some hint or an example?
Well, don't get too fixated on the regions themselves. They are OK for
display, but for your purpose (hittest detection) you need to keep a list of
"objects" (in the generic sense, implementation does not matter for this
discussion) that make up your graphic. Each object would a list of the control
points with coordinates. In your handlers for the mouse events you can now
walk over the list, find the point or edge the mouse is over, and act
accordingly.
To speed up the search process each object should have a BoundsRect property
that defines a rectangle including all elements of the object. That allows you
to quickly determine if the mouse is over the objects area. For overlapping
objects the list sequence would define the Z-order of the objects. Once you
have found the object the mouse is over you need to find the control point
near enough to the mouse. With many points to check it pays to invest some
brain power in a sorting scheme that allows some kind of binary search to be
performed on the points, so you don't need to iterate over all of them. There
should be plenty of literature around on this subject.
Moving a control point basically goes like this:
On a mouse down you determine which point is near enough to the mouse position
to consider it hit. Remember that point (the object it is in, the index of the
point in the object, whatever), set a boolean flag that indicates that a drag
is in progress, perhaps change the mouse cursor as a visual cue, and store the
mouse position.
On a mouse move you first check if you are in a drag operation. If not,
nothing needs to be done (or perhaps you want to highlight things near the
mouse the user might select). If a drag is in progress you determine the
distance the mouse has moved from the last stored position, move the "active"
point the same distance, and tell the object being modified to redraw the part
that has changed, which may also require parts beneath to be redrawn that
belong to other objects.
On a mouse up you clear the drag state flag to indicate that the drag has
finished, and restore the mouse cursor to the default pointer.
Peter Below (TeamB)
Don't be a vampire (
slash7.com/pages/vampires),
use the newsgroup archives :
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be