mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//重写下面2个函数来完成接受外来拖拽的操作.
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDropEvent>
#include <QMimeData>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
{
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls())
{
QList<QUrl> urlList = event->mimeData()->urls();
if (urlList.size() == 1)
{
QString filePath = urlList.first().toLocalFile();
QPixmap pixmap(filePath);
pixmap.scaled(ui->pictureLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->pictureLabel->setPixmap(pixmap);
}
}
event->acceptProposedAction();
event->setDropAction(Qt::MoveAction);
event->accept();
}
QQ录屏20231225103401
CustomDragDrap.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -lOle32
else:win32:CONFIG(debug, debug|release): LIBS += -lOle32d
else:unix: LIBS += -lOle32
INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib
win32:CONFIG(release, debug|release): LIBS += -lUser32
else:win32:CONFIG(debug, debug|release): LIBS += -lUser32d
else:unix: LIBS += -lUser32
INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "Windows.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//处理当以管理员身份运行程序时,不能捕捉到文件拖放事件的问题
{
void* user32 = LoadLibraryA("user32");
FARPROC func = GetProcAddress((HMODULE)user32, "ChangeWindowMessageFilter");
user32 = LoadLibraryA("user32");
func = GetProcAddress((HMODULE)user32, "ChangeWindowMessageFilter");
ChangeWindowMessageFilter(WM_DROPFILES, 1);
qDebug() << w.winId() << w.effectiveWinId();
ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), WM_DROPFILES, MSGFLT_ALLOW, NULL);
ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), WM_COPYDATA, MSGFLT_ALLOW, NULL);
ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), 0x0049, MSGFLT_ALLOW, NULL);
DragAcceptFiles((HWND)w.effectiveWinId(), true);
RevokeDragDrop((HWND)w.winId());
}
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
//重新该函数实现捕获系统的拖放事件和数据
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMimeData>
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <Windowsx.h>
#endif
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
if (eventType == "windows_generic_MSG")
{
MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_DROPFILES)
{
*result = 0;
#ifdef Q_OS_WIN
MSG* msg = reinterpret_cast<MSG*>(message);
HDROP dropHandle = reinterpret_cast<HDROP>(msg->wParam);
int numFiles = DragQueryFile(dropHandle, 0xFFFFFFFF, nullptr, 0);
if (numFiles > 0)
{
//获取鼠标释放时的位置
HWND hWnd = msg->hwnd;
POINT point;
GetCursorPos(&point);
ScreenToClient(hWnd, &point);
WCHAR filePath[MAX_PATH];
DragQueryFileW(dropHandle, 0, filePath, MAX_PATH);
// 处理拖入文件的路径
QString picturepath = QString::fromWCharArray(filePath);
//显示图片
QPixmap pixmap(picturepath);
ui->pictureLabel->setPixmap(pixmap);
}
DragFinish(dropHandle);
return true;
#else
Q_UNUSED(message)
return false;
#endif
}
}
return QWidget::nativeEvent(eventType, message, result);
}
QQ录屏20231225105502