// DragDropTest.cc
#include <string>
#include <windows.h>
#include <shlobj.h>
#include <exdisp.h>
#include <DragDropTest.hh>
HWND toplevel_window_under_mouse() {
const auto mouse_pos
= QCursor::pos();
HWND under_mouse_hwnd = WindowFromPoint(POINT { mouse_pos.x(), mouse_pos.y() });
for (auto parent = GetParent(under_mouse_hwnd); parent != nullptr; parent = GetParent(parent))
under_mouse_hwnd = parent;
return under_mouse_hwnd;
}
std::filesystem::path path_of_window_under_mouse() {
TCHAR explorer_window_directory_path[MAX_PATH];
HWND under_mouse_hwnd = toplevel_window_under_mouse();
explorer_window_directory_path[0] = TEXT('\0');
IShellWindows* shell_windows;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void**)&shell_windows))) {
VARIANT v;
V_VT(&v) = VT_I4;
IDispatch* dispatch;
bool fFound = false;
for (V_I4(&v) = 0; !fFound && shell_windows->Item(v, &dispatch) == S_OK; V_I4(&v)++) {
IWebBrowserApp* webbrowser_app;
if (SUCCEEDED(dispatch->QueryInterface(IID_IWebBrowserApp, (void** )&webbrowser_app))) {
HWND webbrowser_hwnd;
if (SUCCEEDED(webbrowser_app->get_HWND((LONG_PTR* )&webbrowser_hwnd))) {
if (webbrowser_hwnd == under_mouse_hwnd) {
fFound = true;
IServiceProvider* service_provider;
if (SUCCEEDED(webbrowser_app->QueryInterface(IID_IServiceProvider, (void** )&service_provider))) {
IShellBrowser* shell_browser;
if (SUCCEEDED(
service_provider->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, (void** )&shell_browser))) {
IShellView* shell_view;
if (SUCCEEDED(shell_browser->QueryActiveShellView(&shell_view))) {
IFolderView* folder_view;
if (SUCCEEDED(shell_view->QueryInterface(IID_IFolderView, (void** )&folder_view))) {
IPersistFolder2* folder;
if (SUCCEEDED(folder_view->GetFolder(IID_IPersistFolder2, (void** )&folder))) {
LPITEMIDLIST folder_item_ids;
if (SUCCEEDED(folder->GetCurFolder(&folder_item_ids))) {
SHGetPathFromIDList(folder_item_ids, explorer_window_directory_path);
CoTaskMemFree(folder_item_ids);
}
folder->Release();
}
folder_view->Release();
}
shell_view->Release();
}
shell_browser->Release();
}
service_provider->Release();
}
}
}
webbrowser_app->Release();
}
dispatch->Release();
}
shell_windows->Release();
}
return explorer_window_directory_path;
}
void DragDropTester
::keyPressEvent(QKeyEvent* e
) { switch (e->key()) {
case Qt::Key_M:
if (!e->isAutoRepeat()) {
const std::filesystem::path to_move { m_file.toStdString() };
const auto destination = path_of_window_under_mouse();
if (std::filesystem::is_regular_file(to_move) && std::filesystem::is_directory(destination))
std::filesystem::rename(to_move, destination / to_move.filename());
}
break;
default:
}
}
DragDropTester::DragDropTester(const QString& path) :
setFixedSize(400, 400);
show();
}
// DragDropTest.cc
#include <string>
#include <windows.h>
#include <shlobj.h>
#include <exdisp.h>
#include <DragDropTest.hh>
HWND toplevel_window_under_mouse() {
const auto mouse_pos = QCursor::pos();
HWND under_mouse_hwnd = WindowFromPoint(POINT { mouse_pos.x(), mouse_pos.y() });
for (auto parent = GetParent(under_mouse_hwnd); parent != nullptr; parent = GetParent(parent))
under_mouse_hwnd = parent;
return under_mouse_hwnd;
}
std::filesystem::path path_of_window_under_mouse() {
TCHAR explorer_window_directory_path[MAX_PATH];
HWND under_mouse_hwnd = toplevel_window_under_mouse();
explorer_window_directory_path[0] = TEXT('\0');
IShellWindows* shell_windows;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void**)&shell_windows))) {
VARIANT v;
V_VT(&v) = VT_I4;
IDispatch* dispatch;
bool fFound = false;
for (V_I4(&v) = 0; !fFound && shell_windows->Item(v, &dispatch) == S_OK; V_I4(&v)++) {
IWebBrowserApp* webbrowser_app;
if (SUCCEEDED(dispatch->QueryInterface(IID_IWebBrowserApp, (void** )&webbrowser_app))) {
HWND webbrowser_hwnd;
if (SUCCEEDED(webbrowser_app->get_HWND((LONG_PTR* )&webbrowser_hwnd))) {
if (webbrowser_hwnd == under_mouse_hwnd) {
fFound = true;
IServiceProvider* service_provider;
if (SUCCEEDED(webbrowser_app->QueryInterface(IID_IServiceProvider, (void** )&service_provider))) {
IShellBrowser* shell_browser;
if (SUCCEEDED(
service_provider->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, (void** )&shell_browser))) {
IShellView* shell_view;
if (SUCCEEDED(shell_browser->QueryActiveShellView(&shell_view))) {
IFolderView* folder_view;
if (SUCCEEDED(shell_view->QueryInterface(IID_IFolderView, (void** )&folder_view))) {
IPersistFolder2* folder;
if (SUCCEEDED(folder_view->GetFolder(IID_IPersistFolder2, (void** )&folder))) {
LPITEMIDLIST folder_item_ids;
if (SUCCEEDED(folder->GetCurFolder(&folder_item_ids))) {
SHGetPathFromIDList(folder_item_ids, explorer_window_directory_path);
CoTaskMemFree(folder_item_ids);
}
folder->Release();
}
folder_view->Release();
}
shell_view->Release();
}
shell_browser->Release();
}
service_provider->Release();
}
}
}
webbrowser_app->Release();
}
dispatch->Release();
}
shell_windows->Release();
}
return explorer_window_directory_path;
}
void DragDropTester::keyPressEvent(QKeyEvent* e) {
switch (e->key()) {
case Qt::Key_M:
if (!e->isAutoRepeat()) {
const std::filesystem::path to_move { m_file.toStdString() };
const auto destination = path_of_window_under_mouse();
if (std::filesystem::is_regular_file(to_move) && std::filesystem::is_directory(destination))
std::filesystem::rename(to_move, destination / to_move.filename());
}
break;
default:
QMainWindow::keyPressEvent(e);
}
}
DragDropTester::DragDropTester(const QString& path) :
QMainWindow { }, m_file { path } {
setFixedSize(400, 400);
show();
}
To copy to clipboard, switch view to plain text mode
Bookmarks