#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QTimer>
#include <QWidget>
#include <windowsx.h>
#include <Windows.h>
#include <QtTest>
#include <QDesktopWidget>
#include <QApplication>
std::vector<QPoint> poz;
int i=0;
int a=0;
double timer_time=10;
bool certain=false;
bool mouse_clicked=true;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->centralWidget->setStyleSheet("background-color: #535353;" "color: white;");
ui->click_on->setStyleSheet("color: #ceb008;");
ui->speed_label->setText(QString::number(timer_time));
//this->setWindowFlags(Qt::WindowStaysOnTopHint);
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); //no_frames and always_on_top
Qt::X11BypassWindowManagerHint;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(clicked())); //set cursor position timer
timer_move = new QTimer(this);
connect(timer_move, SIGNAL(timeout()), this, SLOT(repeat())); //move cursor timer
timer_points = new QTimer(this);
connect(timer_points, SIGNAL(timeout()), this, SLOT(points())); //points timer
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_start_clicked()
{
if (!certain) timer->start(timer_time);
else timer_points->start(timer_time);
}
void MainWindow::on_stop_clicked()
{
timer->stop();
}
void MainWindow::clicked()
{
p = QCursor::pos();
poz.push_back(p);
ui->xy->setText("x: "+QString::number(p.rx())+" y: "+QString::number(p.ry()));
ui->list->appendPlainText("x: "+QString::number(poz[i].rx())+" y: "+QString::number(poz[i].ry()));
i++;
}
void MainWindow::on_hide_clicked()
{
this->setWindowFlags(Qt::WindowStaysOnBottomHint);
}
void MainWindow::on_off_clicked()
{
exit(0);
}
void MainWindow::on_repeat_clicked()
{
ui->repeat->setStyleSheet("color: #ceb008;");
repeat();
}
void MainWindow::repeat(){
//int x=1027;
//int y=322;
int x=rand()%1920;
int y=rand()%1080;
on_stop_clicked();
if(a==0) {timer_move->start(timer_time);}
if(a<i) {
this->cursor().setPos(poz[a].rx(), poz[a].ry());
// QPointF p(rand()%1920,rand()%1080);
// QMouseEvent(QEvent::MouseButtonPress, p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
// QMouseEvent(QEvent::MouseButtonRelease, p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
//QTest::mousePress(this,);
// QTest::mouseRelease();
//QTest::mouseClick(this,Qt::LeftButton,Qt::NoModifier,poz[a]);
// QTest::mouseClick (this, Qt::LeftButton, Qt::NoModifier, QPoint(660, 1060), 100);
//QWidget *d = QApplication::desktop()->screen();
//QTest::mouseClick(d, Qt::LeftButton, Qt::NoModifier, QPoint(x,y));
HWND h = GetForegroundWindow();
WORD mouseX = 10;// x coord of mouse
WORD mouseY = 10;// y coord of mouse
PostMessage(h,WM_LBUTTONDOWN,0,MAKELPARAM(mouseX,mouseY));
//QMouseEvent e(QEvent::MouseButtonPress, poz[a], Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
//QApplication::sendEvent(ui->centralWidget, &e);
a++;}
else a=0;
}
void MainWindow::on_repeat_off_clicked()
{
timer_move->stop();
a=0;
ui->repeat->setStyleSheet("color: white;");
}
void MainWindow::on_points_clicked()
{
if (certain==false) {
certain=true;
ui->points->setStyleSheet("color: #ceb008;");
}
else {
certain=false;
ui->points->setStyleSheet("color: white;");
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *e){
switch(e->key()){
case Qt::Key_Comma: clicked();
e->accept();
break;
}
}
void MainWindow::points(){
p = QCursor::pos();
ui->xy->setText("x: "+QString::number(p.rx())+" y: "+QString::number(p.ry()));
}
void MainWindow::on_clear_clicked()
{
timer->stop();
timer_move->stop();
poz.erase(poz.begin(),poz.end());
i=0;
a=0;
timer_time=10;
certain=false;
ui->xy->setText("");
ui->list->clear();
}
void MainWindow::on_speed_clicked()
{
ui->speed_label->setReadOnly(false);
ui->speed->setStyleSheet("color: #ceb008;");
}
void MainWindow::on_speed_label_textChanged()
{
ui->speed_label->setReadOnly(true);
timer_time=(ui->speed_label->toPlainText()).toDouble();
ui->speed->setStyleSheet("color: white;");
}
void MainWindow::on_click_on_clicked()
{
if (mouse_clicked) {
ui->click_on->setStyleSheet("color: white;");
mouse_clicked=false;
}
else {
ui->click_on->setStyleSheet("color: #ceb008;");
mouse_clicked=true;}
}
mainwidnow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTimer *timer; //timer for capture a cursor
QTimer *timer_move; //timer for set cursor position
QTimer *timer_points; //timer for certain points
QPoint p;
private slots:
void on_start_clicked();
void on_stop_clicked();
void clicked();
void on_hide_clicked();
void on_off_clicked();
void on_repeat_clicked();
void repeat();
void on_repeat_off_clicked();
void on_points_clicked(); //choose points with tab
void points();
void keyReleaseEvent(QKeyEvent *e);
void on_clear_clicked();
void on_speed_clicked();
void on_speed_label_textChanged();
void on_click_on_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
autoclicker.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-04-30T09:25:20
#
#-------------------------------------------------
QT += core gui testlib
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = autoclicker
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#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
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>419</width>
<height>431</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QStackedWidget" name="gridStackedWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>421</width>
<height>381</height>
</rect>
</property>
<widget class="QWidget" name="gridStackedWidgetPage1">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<property name="verticalSpacing">
<number>25</number>
</property>
<item row="10" column="1">
<widget class="QTextEdit" name="speed_label">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QPushButton" name="repeat">
<property name="text">
<string>REPEAT (SHIFT + R)</string>
</property>
<property name="shortcut">
<string>Shift+R</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="points">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>CHOOSE POINTS (SHIFT + P) - PRESS COMMA ( , )</string>
</property>
<property name="shortcut">
<string>Shift+P</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="start">
<property name="text">
<string>START (SHIFT + S)</string>
</property>
<property name="shortcut">
<string>Shift+S</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="xy">
<property name="maximumSize">
<size>
<width>195</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="hide">
<property name="text">
<string>HIDE (SHIFT + H)</string>
</property>
<property name="shortcut">
<string>Shift+H</string>
</property>
</widget>
</item>
<item row="6" column="0" rowspan="3">
<widget class="QPlainTextEdit" name="list"/>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="stop">
<property name="text">
<string>STOP (SHIFT + SPACE)</string>
</property>
<property name="shortcut">
<string>Shift+Space</string>
</property>
<property name="autoRepeat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QPushButton" name="repeat_off">
<property name="text">
<string>REPEAT OFF (SHIFT + F)</string>
</property>
<property name="shortcut">
<string>Shift+F</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QRadioButton" name="off">
<property name="text">
<string>OFF (SHIFT + O)</string>
</property>
<property name="shortcut">
<string>Shift+O</string>
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QPushButton" name="speed">
<property name="text">
<string>SET SPEED (SHIFT + A)</string>
</property>
<property name="shortcut">
<string>Shift+A</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QPushButton" name="clear">
<property name="text">
<string>CLEAR (SHIFT + C)</string>
</property>
<property name="shortcut">
<string>Shift+C</string>
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QPushButton" name="click_on">
<property name="text">
<string>MOUSE CLICK ON/OFF (SHIFT + M)</string>
</property>
<property name="shortcut">
<string>Shift+M</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Why there is an error?
mainwindow.obj:-1: błąd: LNK2019: unresolved external symbol __imp_GetForegroundWindow referenced in function "private: void __cdecl MainWindow::repeat(void)" (?repeat@MainWindow@@AEAAXXZ)
AND
mainwindow.obj:-1: błąd: LNK2019: unresolved external symbol __imp_PostMessageW referenced in function "private: void __cdecl MainWindow::repeat(void)" (?repeat@MainWindow@@AEAAXXZ)
How to do this right? I want in void repeat to mouse click over the screen (even outside app). How to do this?
.h
file and the.ui
file - they are not necessary. The declarations and definitions can be in one file. You can add the widgets and layouts directly to theMainWindow
class. – Kuba hasn't forgotten Monica