zoukankan      html  css  js  c++  java
  • Qt实现鼠标拖拉窗口,并实现模拟鼠标拖拉窗口代码实例

    效果解释(不知道他们是怎么往博客上传动图显示程序效果的,我只能苦逼的用文字来描述):

    功能1、鼠标左键按下111这个框之后,在不松掉鼠标左键下移动,这个框会跟着移动。

    功能2、鼠标点击Mouse这个按钮之后,那个111框会被模拟鼠标左键按下时移动的效果而被拖动

    这个文件需要导入User32.Lib这个库,可以看我上篇博客:

    qt模拟鼠标左击下移动

    下面全是代码(就不过多解释了,不懂的话评论问我下吧。。。)

    form.h

    #ifndef FORM_H
    #define FORM_H
    
    #include <QWidget>
    #include <QTextEdit>
    #include <QMouseEvent>
    #include <QPoint>
    #include <QPushButton>
    
    namespace Ui {
    class Form;
    }
    
    class Form : public QPushButton
    {
        Q_OBJECT
    
    public:
        explicit Form(QPushButton *parent = 0);
        ~Form();
        void mousePressEvent(QMouseEvent *e);
        void mouseReleaseEvent(QMouseEvent *e);
        void mouseMoveEvent(QMouseEvent *e);
        QPoint point;
    private:
        //Ui::Form *ui;
    signals:
        void toMove(int x,int y);
    
    };
    
    #endif // FORM_H
    //LNK2019: 无法解析的外部符号 "public: __cdecl Form::Form(class QObject *)" (??0Form@@QEAA@PEAVQObject@@@Z),该符号在函数 "public: __cdecl Widget::Widget(class QWidget *)" (??0Widget@@QEAA@PEAVQWidget@@@Z) 中被引用

    form.cpp

    #include "form.h"
    #include "ui_form.h"
    #include <QDebug>
    
    Form::Form(QPushButton *parent) :
        QPushButton(parent)
        //ui(new Ui::Form)
    {
        //ui->setupUi(this);
        //this->setStyleSheet("background-color:black;");
        this->resize(500,500);
        this->setText("1111");
    }
    
    Form::~Form()
    {
        //delete ui;
    }
    void Form::mousePressEvent(QMouseEvent *e)
    {
        if(e->button() == Qt::LeftButton){
            qDebug() << e->globalX() << " --------- " << e->globalY() << endl;
            QPoint tmp(e->globalX(),e->globalY());
            point = tmp;
        }
    }
    
    void Form::mouseReleaseEvent(QMouseEvent *e)
    {
    
    }
    
    void Form::mouseMoveEvent(QMouseEvent *e)
    {
        //qDebug() << e->x() << " " << this->y() << endl;
        if(e->buttons() & Qt::LeftButton){
            qDebug() << e->globalX() << " +++++++++ " << e->globalY() << endl;
            QPoint tmp(e->globalX(),e->globalY());
            int x=(tmp.x()-point.x());
            int y=(tmp.y()-point.y());
            point.setX(tmp.x());
            point.setY(tmp.y());
            qDebug() << x << " ***** " << y << endl;
            //this->move(QPoint(x,y));
            emit toMove(x,y);
        }
    }

    movethread.h

    #ifndef MOVETHREAD_H
    #define MOVETHREAD_H
    
    #include <QWidget>
    #include <QThread>
    
    namespace Ui {
    class MoveThread;
    }
    
    class MoveThread : public QThread
    {
        Q_OBJECT
    
    public:
        explicit MoveThread(QThread *parent = 0);
        ~MoveThread();
        void run();
        void Stop();
        void St();
    
    private:
        //Ui::MoveThread *ui;
        bool condition,st;
    };
    
    #endif // MOVETHREAD_H

    movethread.cpp

    #include "movethread.h"
    #include "ui_movethread.h"
    #include "Windows.h"
    #include "WinUser.h"
    
    MoveThread::MoveThread(QThread *parent) :
        QThread(parent),
        condition(true),
        st(false)
        //ui(new Ui::MoveThread)
    {
        //ui->setupUi(this);
    }
    
    MoveThread::~MoveThread()
    {
        //delete ui;
    }
    
    void MoveThread::run()
    {
        while(condition)
        {
            if(st){
                mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0);//原位置按下
                        //Sleep(33);
                mouse_event(MOUSEEVENTF_MOVE,0,150,0,0);//向上移动150
                        //Sleep(33);
                mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//在移动后的位置释放左键
    
                mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0);//原位置按下
                        //Sleep(33);
                for(int i=1;i<=150;++i){
                    mouse_event(MOUSEEVENTF_MOVE,0,1,0,0);//向上移动150
                    Sleep(1);
                }
                        //Sleep(33);
                mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//在移动后的位置释放左键
                st=false;
            }
        }
    }
    
    void MoveThread::Stop()
    {
        condition=false;
    }
    
    void MoveThread::St()
    {
        st=true;
    }

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QMouseEvent>
    #include "form.h"
    #include "movethread.h"
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void MouseMove(int x, int y);
        Form *f;
    
    private slots:
        void on_pushButton_clicked();
        void MOVE(int x,int y);
    private:
        Ui::Widget *ui;
        MoveThread *moveThread;
    };
    
    #endif // WIDGET_H

    widget.cpp

    #include "widget.h"
    #include "ui_widget.h"
    #include <QDebug>
    #include <QApplication>
    #include "Windows.h"
    #include "WinUser.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        f = new Form();
        f->move(100,100);
        f->setParent(this);
        f->show();
        connect(f,SIGNAL(toMove(int,int)),this,SLOT(MOVE(int,int)));
        //this->setStyleSheet("background-color:black;");
        moveThread = new MoveThread();
        moveThread->start();
    }
    
    void Widget::MOVE(int x,int y)
    {
        qDebug() << x << "qweqweqweqwe" << y <<endl;
        f->move(f->x()+x,f->y()+y);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    void Widget::mousePressEvent(QMouseEvent *event)
    {
        qDebug() << "qqq" << endl;
    }
    void Widget::mouseMoveEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::LeftButton)
        qDebug() << event->x() << " " << event->y() << endl;
    }
    void Widget::mouseReleaseEvent(QMouseEvent *event)
    {
        qDebug() << "www" << endl;
    }
    
    void Widget::on_pushButton_clicked()
    {
    //    qDebug() << "111" << endl;
    //    QPoint pos;
    //    pos.setX(0);
    //    pos.setY(0);
    //    QMouseEvent *mEvnPress;
    //    QMouseEvent *mEvnRelease;
    //    mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    //    QApplication::sendEvent(f,mEvnPress);
    //    for(int i=1;i<10;++i){
    //        delete mEvnPress;
    //        pos.setX(0+50);
    //        pos.setY(0);
    //        mEvnPress = new QMouseEvent(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    //        QApplication::sendEvent(f,mEvnPress);
    //    }
    //    mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    //    QApplication::sendEvent(f,mEvnRelease);
        //MouseMove(100,100);
        moveThread->St();
    }
    void Widget::MouseMove(int x, int y)//鼠标移动到指定位置
    {
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0);//原位置按下
                //Sleep(33);
        mouse_event(MOUSEEVENTF_MOVE,0,300,0,0);//向上移动150
                //Sleep(33);
        mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//在移动后的位置释放左键
    }
    /*
    "No Target Architecture"
    无法解析的外部符号 __imp_mouse_event,该符号在函数 "public: void __cdecl Widget::MouseMove(int,int)" (?MouseMove@Widget@@QEAAXHH@Z) 中被引用
    */

    main.cpp

    #include "widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }

    只有Widget.ui文件用了一下,就加了一个按钮

  • 相关阅读:
    大数据架构师技能图谱
    2018年,Java程序员转型大数据开发,是不是一个好选择?
    如何将java web项目上线/部署到公网
    Flume调优
    Spark流处理调优步骤
    zookeeper的WEB客户端zkui使用
    HBase各版本对Hadoop版本的支持情况
    java 代码实现使用Druid 链接池获取数据库链接
    安装postgreSQL出现configure: error: zlib library not found解决方法
    修改postgres密码
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/15097924.html
Copyright © 2011-2022 走看看