Implementation of rubber band drawing in Delphi instance
In the exercise in this book Delphi7 basic tutorial, I mentioned an example of a rubber band drawing. The source code in the book is incorrect! I don't know whether the error is printed or the source code is incorrect. I changed it.
Place an image component on form1 and set align of image1 to client.
1 unit unit1; 2 3 interface 4 5 6 uses 7 windows, messages, sysutils, variants, classes, graphics, controls, forms, 8 dialogs, extctrls; 9 10 type11 tform1 = Class (tform) 12 image1: timage; 13 procedure image1mousedown (Sender: tobject; button: tmousebutton; 14 shift: tshiftstate; X, Y: integer ); 15 procedure image1mousemove (Sender: tobject; shift: tshiftstate; X, 16 Y: integer); 17 procedure image1mouseup (Sender: tobject; button: tmousebutton; 18 shift: tshiftstate; X, y: integer); 19 private20 {private Declarations} 21 public22 {public declarations} 23 end; 24 25 var26 form1: tform1; 27 Prior, origin: tpoint; {oringin is used to record the start position, that is, the position where the mouse is pressed, and prior is used to record the last position, that is, the position where the mouse is opened} 28 isdown: Boolean = false; {used to determine whether the mouse is still pressed} 29 30 implementation31 32 {$ R *. DFM} 33 34 procedure tform1.image1mousedown (Sender: tobject; button: tmousebutton; 35 shift: tshiftstate; X, Y: integer); 36 begin37 isdown: = true; 38 image1.canvas. moveTo (x, y); 39 origin: = point (x, y); 40 prior: = point (x, y) {record the position when you press, at this time, origin and prior coincide} 41 end; 42 43 Procedure tform1.image1mousemove (Sender: tobject; shift: tshiftstate; X, 44 Y: integer); 45 46 var47 PX, Py: integer; 48 begin49 form1.doublebuffered: = true; 50 if isdown then51 begin52 Px: = prior. x; 53 py: = prior. y; {two values are used to save the last position} 54 prior: = point (x, y); 55 image1.canvas. moveTo (origin. x, origin. y); 56 image1.canvas. lineto (Prior. x, Prior. y); {display current line} 57 image1.canvas. pen. mode: = pmnotxor; {the previous line is deleted through an exception or operation} 58 image1.canvas. moveTo (origin. x, origin. y); 59 image1.canvas. lineto (PX, Py); 60 61 62 end; 63 64 end; 65 66 procedure tform1.image1mouseup (Sender: tobject; button: tmousebutton; 67 shift: tshiftstate; X, Y: integer); 68 begin69 isdown: = false; 70 image1.canvas. pen. mode: = pmcopy; 71 72 image1.canvas. moveTo (origin. x, origin. y); 73 image1.canvas. lineto (Prior. x, Prior. y); {open the mouse and draw the last line} 74 75 end; 76 77 end.