Another drag-drop example with drag image (PB 4.1)
Posted: Fri Jun 01, 2007 10:39 am
Not as sophisticated as netmaestro's example and it uses a different method to render the image.
Still, we begin to see what an excellent library the drag and drop one is.
Still, we begin to see what an excellent library the drag and drop one is.
Code: Select all
;An example of the new Drag and Drop library for Purebasic 4.1.
;==============================================================
;Uses a DragCallback to display an image whilst an item of text is being dragged between
;two listicons.
; Example by netmaestro with the image drag added by srod.
; Created with Purebasic 4.1 (beta 1) for Windows.
; Date: May 2007.
; Platforms: Windows.
Declare.l DragCallBack(Action)
Structure _drag
image.l
hdc.l
ihdc.l
oldimage.l
x.l
y.l
width.l
height.l
EndStructure
Global gDrag._drag
OpenWindow(0,0,0,480,400,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ListIconGadget(0,20,20,200,300,"Gadget 0", 195, #PB_ListIcon_FullRowSelect)
ListIconGadget(1,250,20,200,300,"Gadget 1", 195, #PB_ListIcon_FullRowSelect)
AddGadgetItem(0, -1, "Arthur")
AddGadgetItem(0, -1, "Beryl")
AddGadgetItem(0, -1, "Charles")
AddGadgetItem(0, -1, "Daniel")
AddGadgetItem(0, -1, "Ernest")
AddGadgetItem(0, -1, "Francis")
AddGadgetItem(0, -1, "Gordon")
AddGadgetItem(0, -1, "Harold")
AddGadgetItem(0, -1, "Ian")
AddGadgetItem(0, -1, "John")
EnableGadgetDrop(0,#PB_Drop_Text, #PB_Drag_Copy)
EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)
Repeat
ev = WaitWindowEvent()
Select ev
Case #PB_Event_GadgetDrop
RemoveGadgetItem(draggadget, dragrow)
AddGadgetItem(EventGadget(), GetGadgetState(EventGadget()), EventDropText())
Case #PB_Event_Gadget
If EventType() = #PB_EventType_DragStart
dragrow = GetGadgetState(EventGadget())
dragtxt.s = GetGadgetItemText(EventGadget(), dragrow)
draggadget = EventGadget()
;Create drag image.
StartDrawing(WindowOutput(0))
DrawingFont(GetGadgetFont(draggadget))
gDrag\width = TextWidth(dragtxt) : gDrag\height = TextHeight(dragtxt)
StopDrawing()
gDrag\image = CreateImage(#PB_Any, gDrag\width, gDrag\height)
StartDrawing(ImageOutput(gDrag\image))
DrawingFont(GetGadgetFont(draggadget))
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(0,0,dragtxt,#White)
StopDrawing()
gDragx = -1 : gDrag\y = -1
gDrag\hdc = CreateDC_("DISPLAY",0,0,0)
gDrag\ihdc = CreateCompatibleDC_(gDrag\hdc)
gDrag\oldimage=SelectObject_(gDrag\ihdc, ImageID(gDrag\image))
SetDragCallback(@DragCallBack())
DragText(dragtxt, #PB_Drag_Copy)
;Now tidy up.
DragCallBack(#PB_Drag_None) ;This will effectively remove the last drag image.
SelectObject_(gDrag\ihdc, gDrag\oldimage)
DeleteDC_(gDrag\ihdc)
DeleteDC_(gDrag\hdc)
FreeImage(gDrag\image)
EndIf
EndSelect
Until ev = #PB_Event_CloseWindow
Procedure.l DragCallBack(Action)
If gDrag\x>-1 And gDrag\y>-1
BitBlt_(gDrag\hdc,gDrag\x,gDrag\y,gDrag\width,gDrag\height,gDrag\ihdc,0,0,#SRCINVERT)
EndIf
RedrawWindow_(WindowID(0), 0, 0, #RDW_UPDATENOW)
If action<>#PB_Drag_None
gDrag\x = DesktopMouseX()
gDrag\y = DesktopMouseY()
BitBlt_(gDrag\hdc,gDrag\x,gDrag\y,gDrag\width,gDrag\height,gDrag\ihdc,0,0,#SRCINVERT)
Else
gDrag\x = -1 : gDrag\y = -1
EndIf
ProcedureReturn 1
EndProcedure