How do I get file paths of items dropped onto a co

2019-04-23 16:25发布

问题:

I'd like to be able to let users drag and drop files onto my console app's window so they are not forced to drag'n'drop files onto the app's icon (or link, or even worse write a command line in console). How do I get the list of paths of files I drop onto my app's window?

回答1:

You can just listen for the keyboard. When dragging a file onto a console window the window gets keyboard events as if the file name was entered directly.

That's how you can drop a file on a cmd or PowerShell window and get the full file name as if entered directly and it works the same for your own applications too.



回答2:

A console application doesn't own its window, csrss.exe does. As a result, even if you locate the window HANDLE, you won't be able to register for drag-and-drop or handle drop messages. Your console application is therefore limited to the types of messages forwarded by csrss.exe through the Console API. They are listed here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683499.aspx Drag-and-drop isn't among them, although mouse event inside the console are.

Your best bet is probably to make an application that LOOKS like a console application, but doesn't use a Windows console. Instead you'd draw text and a cursor on the screen and take keyboard input, creating a command-line interface.

This sort of thing is called a "console emulator", and you can probably find one already built that meets your needs.



回答3:

You cant because console application has no window. It has standard input, output and error streams.

Window that contain console application is window of CMD application (cmd.exe) and you cant change fundamental behaviors of windows console.

CMD works like terminal (putty, telnet, ssh etc.) - it sends characters from keyboard to application (precisely - it sends characters to "standard input") and displays characters generated by "standard output" of application.

Read a little about standard streams. http://en.wikipedia.org/wiki/Standard_streams

Behavior of CMD.exe under my Windows 7

I can pass filename (with full path) from drag/drop. Dropping one "file icon" on cmd window will "type" filename - but there is no CR/LF (line feed) character, so "readline" function will not work.

Dropping multiple file icons will pass only one file.

Sorry, if i made some language mistakes.