Hi,
ages ago (when the drag-drop lib was first added to PB), I hacked up a demo which showed one way of displaying an image whilst dragging some text. Now that I finally get around to making use of that code, I find that it does not work very well (since it attempts to draw atop windows containing controls etc. which is a very 'dodgy' thing to attempt!)
Here is a better (and simpler) method which uses a layered window (Win 2000 onwards) as the basis for the drag image. It seems to work a lot better than my first attempt, though admittedly I haven't tested a great deal as yet (that will change when I integrate this into the application I am working on) :
Code: Select all
Declare.i DragCallBack(Action)
;A couple of global fields for use when dragging our image window.
Structure _drag
winDrag.i
width.i
height.i
EndStructure
Global gDrag._drag
If OpenWindow(0,0,0,480,400,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ListIconGadget(0,20,20,200,300,"Drag from", 195, #PB_ListIcon_FullRowSelect)
ListIconGadget(1,250,20,200,300,"Drag to", 195, #PB_ListIcon_FullRowSelect)
For i = 0 To 99
AddGadgetItem(0, -1, "Row "+Str(i))
AddGadgetItem(1, -1, "Row "+Str(i))
Next
EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)
Repeat
eventID = WaitWindowEvent()
Select eventID
Case #PB_Event_GadgetDrop
If EventGadget() =1
AddGadgetItem(EventGadget(), GetGadgetState(EventGadget()), EventDropText())
EndIf
Case #PB_Event_Gadget
If EventType() = #PB_EventType_DragStart And EventGadget()=0
dragrow = GetGadgetState(EventGadget())
dragtxt.s = GetGadgetItemText(EventGadget(), dragrow)
draggadget = EventGadget()
;Create drag image window.
If StartDrawing(WindowOutput(0))
DrawingFont(GetGadgetFont(draggadget))
gDrag\width = TextWidth(dragtxt)
gDrag\height = TextHeight(dragtxt)
StopDrawing()
imageID = CreateImage(#PB_Any, gDrag\width, gDrag\height)
If imageID
If StartDrawing(ImageOutput(imageID))
Box(0, 0, gDrag\width, gDrag\height, #White)
DrawingFont(GetGadgetFont(draggadget))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(0,0,dragtxt,#Black)
StopDrawing()
hBrush = CreatePatternBrush_(ImageID(imageID))
If hBrush
gDrag\winDrag = OpenWindow(#PB_Any, 0, 0, 0, 0, "", #PB_Window_BorderLess|#PB_Window_NoGadgets, WindowID(0))
If gDrag\winDrag
hWnd = WindowID(gDrag\winDrag)
SetWindowLong_(hWnd, #GWL_EXSTYLE, GetWindowLong_(hWnd, #GWL_EXSTYLE) | #WS_EX_LAYERED)
SetLayeredWindowAttributes_(hWnd, #White, 180, #LWA_COLORKEY|#LWA_ALPHA)
;We enable drops (mimicking our intended drop type) on this window so that the drag cursor behaves itself!
EnableWindowDrop(gDrag\winDrag, #PB_Drop_Text, #PB_Drag_Copy)
SetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND, hBrush)
;Instigate the drag.
SetDragCallback(@DragCallBack())
SetFocus_(GadgetID(0))
DragText(dragtxt, #PB_Drag_Copy)
;Now tidy up.
CloseWindow(gDrag\winDrag)
EndIf
DeleteObject_(hBrush)
EndIf
EndIf
FreeImage(imageID)
EndIf
EndIf
EndIf
EndSelect
Until eventID = #PB_Event_CloseWindow
EndIf
Procedure.i DragCallBack(Action)
Protected pt.point, rc.RECT, top
GetCursorPos_(pt)
GetWindowRect_(GadgetID(1), rc)
If PtInRect_(rc, pt\x + pt\y<<32)
ResizeWindow(gDrag\winDrag, pt\x, pt\y, gDrag\width, gDrag\height)
Else
ResizeWindow(gDrag\winDrag, 0, 0, 0, 0)
EndIf
ProcedureReturn 1
EndProcedure