[Window] Launcher d'applications & co
Publié : sam. 28/nov./2015 20:39
PureBasic 5.40 minimum.
Un code minimaliste montrant comment glisser une application, un fichier, un dossier, un raccourci, une url sur un des emplacement de ce launcher.
Cliquer et c'est lancer. Si vous souhaitez sauvegarder le contexte, sauver la map au format JSON avant de fermer la fenêtre sans oublier de recharger cette map au lancement du code.
Un code minimaliste montrant comment glisser une application, un fichier, un dossier, un raccourci, une url sur un des emplacement de ce launcher.
Cliquer et c'est lancer.
Code : Tout sélectionner
EnableExplicit
Enumeration Window
#MainForm
EndEnumeration
Enumeration Gadget
#Canvas0 = 0
#Canvas9 = 9
EndEnumeration
Global NewMap Launcher.s()
Declare MainFormOpen()
Declare OnDropFile()
Declare GetAssociatedFileIcon(IconPath.s, IconSize)
Declare OnClick()
Declare MainFormClose()
MainFormOpen()
Procedure MainFormOpen()
Protected Gadget
InitNetwork()
UsePNGImageDecoder()
If OpenWindow(#MainForm, 0, 0, 0, 350, "MemoLink", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
SetWindowColor(#MainForm, RGB(128, 128, 128))
For Gadget = #Canvas0 To #Canvas9
CanvasGadget(Gadget, 1, Gadget*34 + 3, 127, 32)
EnableGadgetDrop(Gadget, #PB_Drop_Text, #PB_Drag_Copy)
EnableGadgetDrop(Gadget, #PB_Drop_Files, #PB_Drag_Copy)
BindGadgetEvent(Gadget, @OnClick(), #PB_EventType_LeftClick)
Next
WindowBounds(#MainForm, 130, 35, 130, 1000)
StickyWindow(#MainForm, #True) ;Toujours en avant
BindEvent(#PB_Event_GadgetDrop, @OnDropFile())
BindEvent(#PB_Event_CloseWindow, @MainFormClose())
Repeat : WaitWindowEvent(10) : ForEver
EndIf
EndProcedure
Procedure OnDropFile()
Protected Gadget = EventGadget()
Protected ItemIcon, ItemText.s, *Buffer, DomainUrl.s
Select EventDropType()
Case #PB_Drop_Text
Itemtext = EventDropText() ;Return text
;Recupération du favicon
DomainUrl = StringField(ItemText, 3, "/")
If DomainUrl =""
DomainUrl = ItemText
EndIf
*Buffer = ReceiveHTTPMemory("http://www.google.com/s2/favicons?domain=" + DomainUrl)
If *Buffer
ItemIcon = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
FreeMemory(*Buffer)
ItemIcon = ImageID(ItemIcon)
EndIf
Case #PB_Drop_Files
ItemText = StringField(EventDropFiles(), 1, Chr(10)) ;Return file, shortcut, folder, etc ....
ItemIcon = GetAssociatedFileIcon(ItemText, #SHGFI_LARGEICON)
EndSelect
StartDrawing(CanvasOutput(Gadget))
Box(0, 0, 127, 32, RGBA(255, 255, 255, 0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ItemIcon, 0, 0, 32, 32)
StopDrawing()
AddMapElement(Launcher(), Str(Gadget))
Launcher() = ItemText
GadgetToolTip(Gadget, GetFilePart(ItemText))
EndProcedure
;Retrieves the icon associated to a file (By Goznal)
;Récupère l'icône associée à un fichier
Procedure GetAssociatedFileIcon(IconPath.s, IconSize)
Protected FileInfo.SHFILEINFO
; http://msdn.microsoft.com/en-us/library/bb762179%28VS.85%29.aspx
; IconSize peut prendre les valeur ci-dessous :
; IconSize can take the value :
; #SHGFI_SMALLICON -> small (usually 16x16) icon
; #SHGFI_LARGEICON -> large (usually 32x32) icon
; #SHGFI_SHELLICONSIZE -> shell default icon size
SHGetFileInfo_(IconPath, 0, @FileInfo, SizeOf(SHFILEINFO), #SHGFI_ICON | IconSize)
ProcedureReturn FileInfo\hIcon
EndProcedure
Procedure OnClick()
Protected Gadget = EventGadget()
If FindMapElement(Launcher(), Str(Gadget))
RunProgram(Launcher())
EndIf
EndProcedure
;Your end code here (Example : Save map json file)
Procedure MainFormClose()
End
EndProcedure