[Tuto] How to drop a file over ButtonImageGadget and create image + text to set it as launcher

Windows specific forum
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

[Tuto] How to drop a file over ButtonImageGadget and create image + text to set it as launcher

Post by Marc56us »

In reply to question https://www.purebasic.fr/english/viewto ... 48#p638148
here is a full functionnal sample of How to drop a file over ButtonImageGadget and create image + text to set it as launcher

This is a quick and dirty example, but it explains how to use program drag and drop from the desktop or Explorer to a buttongadget.
The aim is to grab the image and file name and turn it into a program launcher.
This sample extract ico only from EXE but works for all files, links, folders.
This is how I created my acmedesk5, except that I use Canvas rather than button images (Canvas has more possibilities and looks the same (flat) on Windows 10 and later).
To extract icons other than EXE ones, you need to use another API

Code: Select all

; Mini launcher sample
; (C)Marc56us - 2025-03-27
; https://www.purebasic.fr/english/viewtopic.php?p=638172#p638172
; How to extract and fill ButtonImageGadget with dropped ico (does not work with links)
; Icon is extract only from EXE (change ExtractIcon_ API for others file type)

EnableExplicit

OpenWindow(0, 0, 0, 300, 300, 
           "Drop program, link or folder on button", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonImageGadget(101, 50,  45, 200, 50, 0)
ButtonImageGadget(102, 50, 125, 200, 50, 0)
ButtonImageGadget(103, 50, 205, 200, 50, 0)

EnableGadgetDrop(101, #PB_Drop_Files, #PB_Drag_Copy)
EnableGadgetDrop(102, #PB_Drop_Files, #PB_Drag_Copy)
EnableGadgetDrop(103, #PB_Drop_Files, #PB_Drag_Copy)

Global Dim Prog$(3)

Procedure Make_Button_Image_Prog(Id_Button, File$)
    Debug "Id_Button    : " + Id_Button
    Debug "Dropped file : " + File$
    
    Prog$(Id_Button - 100) = File$

    ; Create image for button
    Protected Img_Button = 99
    CreateImage(Img_Button, 200, 50, 32, #White)
    ShowLibraryViewer("image", Img_Button)
    
    ; Extract ico
    Protected Tmp_Ico = 98
    Tmp_Ico = ExtractIcon_(0, File$, 0)
    
    ; Extract Filename
    Protected Tmp_Txt$ = GetFilePart(File$, #PB_FileSystem_NoExtension)
    Debug "Filename     : " + Tmp_Txt$
    
    ; Font
    Protected Font = LoadFont(0, "Tahoma", 9)
    
    ; Draw image
    StartDrawing(ImageOutput(Img_Button))
    DrawingMode(#PB_2DDrawing_Transparent)
    Box(0, 0, 200, 50, #White)
    If Tmp_Ico 
        DrawImage(Tmp_Ico, 10, 9)
    EndIf 
    DrawingFont(Font)
    DrawText(65, 17, Tmp_Txt$, #Black)
    StopDrawing()
    
    ShowLibraryViewer("image", Img_Button)
    
    SetGadgetAttribute(Id_Button, #PB_Button_Image, ImageID(Img_Button))
EndProcedure


Repeat
    
    Select WaitWindowEvent()
            
        Case #PB_Event_GadgetDrop
            Select EventGadget()
                    
                Case 101 
                    Make_Button_Image_Prog(101, EventDropFiles())   
                    
                Case 102 
                    Make_Button_Image_Prog(102, EventDropFiles())   
                    
                Case 103 
                    Make_Button_Image_Prog(103, EventDropFiles())
                    
            EndSelect
            
        Case #PB_Event_Gadget
            RunProgram(Prog$(EventGadget() - 100), "", "")

        Case #PB_Event_CloseWindow
            CloseWindow(0)
            End
    EndSelect
    
ForEver

End
I've used numerical identifiers just to make the example clearer. Always prefer text identifiers or enumerations.
Often use ShowLibraryViewer(“image”, ...) (put breakpoint after) to follow the construction of the image, this a good method when working with images.

Enjoy
:wink:
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: [Tuto] How to drop a file over ButtonImageGadget and create image + text to set it as launcher

Post by blueb »

Thanks Marc56us.. greatly appreciated.
Works well. Another puzzle solved. :)

Only thing I can add is that the buttons all appear 'white'. I'll work on changing the background, if possible and making the text have a tranaparent background as well.

Thanks again.
Blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply