Advertisement

03.19.2008 at 01:47AM PDT, ID: 23252920
[x]
Attachment Details

Marching Ants on TImage inside TScrollbox (Erase Rect problem)

Asked by Alexander_Savenko in Graphics and Delphi Programming, Delphi Programming, Delphi Components

Tags: , ,

Dear Experts!

I have a TImage component with an ability to select a region and highlight it with "marching ants". The problem is: when I drop TImage on TForm all is ok and when user move mouse previous rectangle correctly erase, but when TImage is inside TScrollbox rectangles doesn't erase. Please help me!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
var
  Counter : Byte;
  CounterStart : Byte;
  Looper : LongInt;
  b:TBitmap;
  Region:TRect;
 
PROCEDURE RestrictCursorToDrawingArea (CONST Image: TImage);
  VAR
    CursorClipArea: TRect;
BEGIN
  CursorClipArea := Bounds(Image.ClientOrigin.X, Image.ClientOrigin.Y,
                                          Image.Width, Image.Height);
  Windows.ClipCursor(@CursorClipArea)
END {RestrictCursorToDrawingArea};
 
 
PROCEDURE RemoveCursorRestrictions;
BEGIN
  Windows.ClipCursor(NIL)
END {RemoveCursorRestrictions};
 
 
procedure MovingDots(X,Y: Integer; TheCanvas: TCanvas); stdcall;
begin
  Inc(Looper);
{$R-}
  Counter := Counter shl 1;              // Shift the bit left one
{$R+}
  if   Counter = 0
  then Counter := 1;          // If it shifts off left, reset it
  if   (Counter and 224) > 0  // Are any of the left 3 bits set?
  then
  begin
  TheCanvas.Pixels[X,Y] := clWhite   // Erase the pixel
  end else
  begin
    TheCanvas.Pixels[X,Y] := clBlack;  // Draw the pixel
  end;
 
end;
 
 
function NormalizeRect(R: TRect): TRect;
begin
  // This routine normalizes a rectangle. It makes sure that the Left,Top
  // coords are always above and to the left of the Bottom,Right coords.
  with R do
  BEGIN
    if   Left > Right
    then
      if   Top > Bottom
      then Result := Rect(Right,Bottom,Left,Top)
      else Result := Rect(Right,Top,Left,Bottom)
    else
      if   Top > Bottom
      then Result := Rect(Left,Bottom,Right,Top)
      else Result := Rect(Left,Top,Right,Bottom);
  END;
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
b:=Tbitmap.Create;
  X1 := 0;
  Y1 := 0;
  X2 := 0;
  Y2 := 0;
  Canvas.Pen.Color := Color;
  Canvas.Brush.Color := Color;
  CounterStart := 128;
  Timer1.Interval := 1;
  Timer1.Enabled := True;
  Looper := 0;
  scrollbox1.ControlStyle:=scrollbox1.ControlStyle+ [csOpaque];
end;
 
procedure TForm1.RemoveTheRect;
var
  R : TRect;
begin
  R := NormalizeRect(Rect(X1,Y1,X2,Y2));  // Rectangle might be flipped
  InflateRect(R,1,1);                     // Make the rectangle 1 pixel larger
  InvalidateRect(Handle,@R,True);         // Mark the area as invalid
  InflateRect(R,-2,-2);                   // Now shrink the rectangle 2 pixels
  ValidateRect(Handle,@R);                // And validate this new rectangle.
  // This leaves a 2 pixel band all the way around
  // the rectangle that will be erased & redrawn
  UpdateWindow(Handle);
end;
 
function TForm1.DrawTheRect:TRect;
begin
  // Determines starting pixel color of Rect
  Counter := CounterStart;
  // Use LineDDA to draw each of the 4 edges of the rectangle
  LineDDA(X1,Y1,X2,Y1,@MovingDots,LongInt(Image3.Canvas));
  LineDDA(X2,Y1,X2,Y2,@MovingDots,LongInt(Image3.Canvas));
  LineDDA(X2,Y2,X1,Y2,@MovingDots,LongInt(Image3.Canvas));
  LineDDA(X1,Y2,X1,Y1,@MovingDots,LongInt(Image3.Canvas));
  Result.Bottom:=y2;
  Result.Top:=y1;
  Result.Left:=x1;
  Result.Right:=x2;
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  CounterStart := CounterStart shr 2;    // Shl 1 will move rect slower
  if   CounterStart = 0                  // If bit is lost, reset it
  then CounterStart := 128;
  DrawTheRect                           // Draw the rectangle
end;
 
procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 X := X + Image3.Left;
 Y := Y + Image3.Top;
  RemoveTheRect;                               // Erase any existing rectangle
  X1 := X;
  Y1 := Y;
  X2 := X;
  Y2 := Y;
 // Force mouse movement to stay within TImage
  RestrictCursorToDrawingArea(Image3);
end;
 
procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if ssLeft in Shift
  then  begin
 X := X + (Image3).Left;
 Y := Y + (Image3).Top;
    RemoveTheRect;         // Erase any existing rectangle
    X2 := X; Y2 := Y;      // Save the new corner where the mouse is
    Region:=DrawTheRect;           // Draw the Rect now... don't wait for the timer!
  end;
end;
 
procedure TForm1.ImageMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
RemoveCursorRestrictions;
end;
 
Loading Advertisement...
 
[+][-]03.19.2008 at 01:54AM PDT, ID: 21159431

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 01:59AM PDT, ID: 21159447

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 02:36AM PDT, ID: 21159604

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Graphics and Delphi Programming, Delphi Programming, Delphi Components
Tags: Borland, Delphi, 6
Sign Up Now!
Solution Provided By: ciuly
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628