Drag and drop in Windows

Previous topic - Next topic

JohnnyB

Hi all,

has anyone yet done (I know there is a solution for e.g. clipboard) an example of dropping a file into your program window, so you could e.g. automatically load the file?


Kitty Hello

Oh. You need the wm_dropfiles message. Or so. Phew. That's really hard to do, because of the stuff you can drop. You just want the filenames?

JohnnyB

#2
Thanks for the reply.

Yes, basically I just want to get the file name(s) / paths of something that is dragged into the app window.

dreamerman

Some workaround is to use SDL2 functions, thanks goes to Schranz0r, get his code from this post: https://www.glbasic.com/forum/index.php?topic=11401.msg100530#msg100530
And add:
in SDL2_window.gbas -> FUNCTION CreateFromGLBasicWindow
after inits add this line:
Code (glbasic) Select
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
in same file add those two functions to SDL2_Window type
Code (glbasic) Select

    FUNCTION EventFileDrop:
INLINE
return events(eventHandle).type == SDL_DROPFILE;
ENDINLINE
    ENDFUNCTION
   
    FUNCTION GetDroppedFile$:
        LOCAL ret$
        INLINE
            char* dropped_filedir;
            dropped_filedir = events(eventHandle).drop.file;
            ret_Str = dropped_filedir;
            SDL_free(dropped_filedir);
        ENDINLINE
        RETURN ret$
    ENDFUNCTION

in main file (SDL_calls.gbas) change PollEvent loop for something like this:
Code (glbasic) Select

WHILE Win.PollEvent()
IF Win.EventKeyDown()
IF KEY(1) THEN Win.CloseWindow() // Close window on ESCAPE-Key
INC counter, 1
ELSEIF Win.EventFileDrop()
LOCAL file$ = Win.GetDroppedFile$()
ENDIF
WEND

It's working, sorry that I doesn't provide clean source, but I'm playing around with other SDL functions so my test project is a complete mess :D
If You need only drop file support maybe there is no need to create SDL window and so on, 'SDL_INIT_EVENTS' could be sufficient to work with drag&drop, not sure.
Check my source code editor for GLBasic - link Update: 20.04.2020

JohnnyB

That's works perfectly and is very very awesome ;) Thanks!