在正在运行的Windows控制台应用程序中处理拖放文件 [英] Handling drag and drop files in a running Windows console application

查看:91
本文介绍了在正在运行的Windows控制台应用程序中处理拖放文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,要澄清一下,我不是在问他们如何将文件拖放到exe图标上.我想知道如何处理拖放到已运行 win32控制台应用程序上的操作.我也询问如何通过Windows消息泵处理基于WinMain的应用程序中的拖放.我想在没有WndProc(尚未)或任何东西的入口点 int main()的程序中执行此操作.

First, to clarify, I am not asking how to drag-and-drop a file onto an exe's icon. I want to know how to handle drag and drop onto an already running win32 console application. I'm also not asking how to handle drag and drop inside of WinMain based applications through the Windows message pump. I want to do this inside of a program with the entry point int main() that doesn't have a WndProc (yet) or anything.

也就是说,我想知道我的目标是否可以实现(并希望是可以实现的).

That said, I'm wondering if my goal is achievable (and hoping that it is).

我有一个在控制台窗口中运行的服务器应用程序.由于庞大的代码库和大量奇怪的耦合,因此它是用于所有密集用途的仅输出"控制台.但是在其中,由于有更新循环标记,因此我仍然可以处理按键操作.我希望能够将充满命令的文件(使用自定义语法)拖放到正在运行的应用程序中,并对其进行处理.

I have a server application that is running within a console window. Due to a large codebase and a lot of weird coupling, it is an 'output only' console for all intensive purposes. Within it though, I can still handle things like key presses, as I have an update loop ticking. I'd like to be able to drag and drop files full of commands (which use a custom syntax) onto my running application and have it process them.

这有可能吗?我当时在想,我可能会获得一个指向控制台HWND的指针(希望是这样吗?),然后可以将该窗口子类化,以使用自定义WndProc来侦听WM_DROPFILES消息.

Is this possible to do? I was thinking that potentially I could get a pointer to the HWND of the console (which hopefully is a thing?), and then maybe subclass that window to use a custom WndProc to listen for the WM_DROPFILES message.

我从未真正尝试过在 int main()程序而不是 WinMain 程序中设置Windows消息的处理,但我希望它能以某种方式可能.

I've never really tried to set up handling of windows messages in an int main() program instead of a WinMain program, but I'm hoping it's somehow possible.

任何帮助将不胜感激! 奇怪的解决方案很好.

Any help would be greatly appreciated! Weird solutions are fine.

推荐答案

AFAIK,默认情况下,控制台窗口不支持拖放.您始终可以使用自己的消息循环创建自己的单独弹出窗口,以便用户可以将项目拖到上面.

AFAIK, a console window does not support drag&drop by default. You can always create your own separate popup window with its own message loop so the user has something to drag items onto.

要在控制台窗口本身上使用拖放,请尝试使用

To use drag&drop on the console window itself, try using GetConsoleWindow() to get the console HWND, then either:

  1. 使用 SetWindowSubClass() ,然后使用

  1. subclass the HWND using SetWindowLong/Ptr() or SetWindowSubClass(), then register the HWND using DragAcceptFiles() to start receiving WM_DROPFILES messages. Be sure to call DragAcceptFiles() again to stop receiving the messages and then unhook your subclass before exiting the app.

实现 IDropTarget 接口,然后使用 RevokeDragDrop() 退出应用程序.

implement the IDropTarget interface and then register the HWND using RegisterDragDrop() to start receiving notifications. Be sure to call RevokeDragDrop() before exiting the app.

WM_DROPFILES 易于编码,但是 IDropTarget 更加灵活,因为它可以处理虚拟项目和物理文件.

WM_DROPFILES is easier to code for, but IDropTarget is more flexible as it handles virtual items as well as physical files.

这篇关于在正在运行的Windows控制台应用程序中处理拖放文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆