Use the mouse drag and drop to adjust the size of TCONTROL in BCB

zhaozj2021-02-16  59

For controls that inherit from TwinControl, if you want to adjust its size through the mouse, you only need to add WS_SIZEBOX to the GWL_Style style through SETWINDOWLONG. But for other objects that inherit from TControl, such as TGRAPHICCONTROL, because this method cannot be used because it is not a Windows control. Therefore, you must do it yourself.

The sample code for BCB is given below (assuming that the change in the size is TBevel):

variable:

// definition of an enumerated type, determines from which position the mouse enters into the control enum TMoveInType {mtNone, mtLeft, mtTop, mtRight, mtBottom, mtTopLeft, mtTopRight, mtBottomLeft, mtBottomRight}; TMoveInType moveInType = mtNone; TRect SizeRect; bool SizeState = false; Const int size = 8; __ inline int min (int a, int b) {return (A

__INline Int Max (Int A, INT B) {RETURN (A> B)? A: B;}

Handle in the MouseMove, MouseUp event of the control:

Void __fastcall tform1 :: bevel1Mousemove (Tobject * sender, tshiftstate shift, int x, int y) {TBevel * sdbevel; sdbevel = (tbevel *) sender; // judge the position IF (! SIZESTATE) {IF (ABS) {IF (ABS) Y -SDBevel-> Height / 2) <= SIZERANGE && (ABS (x) <= sizerAnge)) {moveintype = mtleft; sdbevel-> cursor = CrsizeWe;} else if (ABS (Y -SDBevel-> height / 2) < = SIZERANGE && ABS (SDBevel-> width -x <= sizer)) {moveintype = mtright; sdbevel-> cursor = CrsizeWe;} else if (abs (x -sdbevel-> width / 2) <= sizerange && (ABS (Y) < = SIZERANGE)) {moveintype = mttop; sdbevel-> cursor = Crsizens;} else if (abs (x -sdbevel-> width / 2) <= sizeRange && ABS (SDBevel-> HEIGHT - Y <= sizerAnge)) {MoveInType = MTBOTTOM SDBevel-> Cursor = CrsizENS;} else IF ((ABS (x) <= SIZERANGE) && (ABS (Y) <= SIZERANGE)) {MoveIntype = Mttopleft; SDBevel-> Cursor = Crsizenwse;} else f (ABS) SDBevel-> width -x <= sizerAnge) && (ABS (Y) <= SIZERANGE)) {moveintype = mttopright; sdbevel-> cursor = CrsizENESW;} else IF ((ABS (x) <= sizerange) && ABS (SDBevel > HEIGHT - Y <= Si zeRange)) {moveInType = mtBottomLeft; SdBevel-> Cursor = crSizeNESW;} else if (abs (SdBevel-> Width -X) <= SizeRange && abs (SdBevel-> Height - Y <= SizeRange)) {moveInType = mtBottomRight; SdBevel- > Cursor = crsizenwse;} else if (! Sizestate) {sdbevel-> cursor = crarrow; moveintype = mtnone;}}} f ((moveintype! = Mtnone) && (SHIFT.CONTAINS (SSLEFT))) {sizestate = true; canvas -> Pen-> Mode = PMNotXor;

Canvas-> Pen-> style = psdot; canvas-> pen-> color = CLDKGRAY; canvas-> brush-> style = bsclear; // use xor to erase canvas-> Rectangle last SIZERECT (SizeRect.Left) , SizeRect.top, SizeRect.right, SizeRect.Bottom; TRECT BRECT; / / May Right> Left, Top> Bottom, so use temporary BRECT to do some adjustments BRECT = SDBevel-> BoundSRect;

Switch (MoveInType) {Case MTLEFT: BRECT.LAK; CASE MTRIGHT: BRECT.Right = BRIGT X; BREAK; CASE MTTOP: BRECT.TOP = Y; Break; Case Mtbottom: BRECT.BOTTOM = BRECT.TOP Y; BREAK; CASE MTTOPLEFT: BRECT.TOP = x; BRECT.TOP = Y; Break; Case Mttopright: BRECT.Right = BRECT.LEFT X; BRECT.TOP = Y; BREAK; Case MtbottomLeft: BRECT.EFT = X ;; BRECT.BOTTOM = BRECT.TOP Y; BREAK; Case Mtbottomright: BRECT.Right = BRECT.LEFT X; BRECT.BOTTOM = BRECT.TOP Y; BREAK;}

SIZERECT.LEFT = Min (BRECT.LIGT = Max); SizeRect.top = MIN (BRECT.TOP, BRECT.BOTTOM); SIZERECT.BOTTOM = Max (BRECT.TOP, BRECT.BOTTOM); // Painting SizeRect Canvas-> Rectangle (SizeRect.right, SizeRect.top, Sizerct.right, SizeRect.Bottom);

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------

转载请注明原文地址:https://www.9cbs.com/read-18119.html

New Post(0)