I. Override the getDragimages method for the control.
Implementation steps: 1. Rewrive the GetDragimages method
class TMyPanel: public TPanel {private: TDragImageList * dragimgs; protected: virtual TDragImageList * __fastcall GetDragImages (void) {Graphics :: TBitmap * bmp = new Graphics :: TBitmap (); bmp-> Width = Width; bmp-> Height = Height; pain-> canvas-> handle, 0, 0); Dragimgs-> width = width; Dragimgs-> Height = height; Dragimgs-> add (bmp, null); Return Dragimgs;} public: __fastcall TmyPanel TComponent * AOwner): TPanel (AOwner) {Width = 300; Height = 200; DragMode = dmAutomatic; // set automatic drag ControlStyle << csDisplayDragImage; dragimgs = NULL; Font-> Color = clRed; Caption = "Hello This is A Test of Control Drag "; Dragimgs = New TDRAGIMAGELIST (THIS);
2. Add the following method to the form that uses the control, and calls fixcsStyle (this) when the form is created, enables all controls in the form to display drag and drop graphics.
Void fixcsStyle (Tcomponent * fc) // allows all controls to display drag and drop graphics
{For (int i = 0; i
IF (C-> ComponentCount> 0) FixcsStyle (C);}};
Method 2. Do not rewrite the control, use TDRAGCONTROLOBJECT implementation. I use the drag and drop of text objects as an example.
Steps: 1. Implement the derived class of TDRAGCONTROLOBJECT
Class TTextdragobject: Public TDRAGCONTROLOBJECT {private: tdragimagelist * Dragimgs; Ansistring FDRAGTEXT; Protected: Virtual TdragimageList * __fastcall getdragimages (void) {
if (dragImgs == NULL) dragImgs = new TDragImageList (NULL); Graphics :: TBitmap * bmp = new Graphics :: TBitmap (); bmp-> Height = bmp-> Canvas-.TextHeight (FDragText); bmp-> Width = BMP-> Canvas-> TextWidth (FDRAGTEXT);
BMP-> Canvas-> Textout (0, 0, FDRAGTEXT);
Dragimgs-> width = bmp-> width; Dragimgs-> Height = bmp-> height; int idx = Dragimgs-> addmasked (BMP, CLWHITE);
dragImgs-> SetDragImage (idx, 0,0);}; public: __fastcall TTextDragObject (TControl * AControl, AnsiString text); __fastcall virtual ~ TTextDragObject (void); __property AnsiString DragText = {read = FDragText, write = FDragText};} ;
2. Add the following a void __fastcall TForm1 :: Panel1StartDrag (TObject * Sender, TDragObject * & DragObject) in the event OnStartDrag drag source control of {DragObject = new TTextDragObject (Panel1, "Drag Text test");}