利用Qt实现放大镜功能,目前只实现了程序启动时获取当前系统窗口进行放大,具体实现如下:
#pragma once
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPixmap>
#include <QScreen>
class BigGlass : public QWidget
{
Q_OBJECT
public:
explicit BigGlass(QWidget* parent = nullptr);
private:
void initWindow();
private slots:
void TimerSlot();
protected:
void paintEvent(QPaintEvent* pEvent);
private:
QPixmap m_pixMap;
};
#include "BigGlass.h"
#include <QGuiApplication>
#include <QTimer>
BigGlass::BigGlass(QWidget* parent)
: QWidget(parent)
{
this->initWindow();
}
void BigGlass::initWindow()
{
this->setWindowFlags(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setMouseTracking(true);
QScreen* pScreen = QGuiApplication::primaryScreen();
m_pixMap = pScreen->grabWindow(0);
this->resize(pScreen->size());
this->setCursor(Qt::OpenHandCursor);
QTimer* pTimer = new QTimer(this);
pTimer->start(100);
connect(pTimer, &QTimer::timeout, this, &BigGlass::TimerSlot);
}
void BigGlass::TimerSlot()
{
this->update();
}
void BigGlass::paintEvent(QPaintEvent* pEvent)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
int x = QCursor::pos().x();
int y = QCursor::pos().y();
QPen pen(Qt::white, 2);
painter.setPen(pen);
painter.save();
painter.setBrush(QBrush(QColor(0, 0, 0)));
painter.translate(x, y);
painter.rotate(45);
painter.drawRect(60, -8, 80, 16);
painter.restore();
QTransform transform;
transform.translate(x-60, y-60);
transform.scale(3.0, 3.0);
QPixmap map = m_pixMap.copy(x - 20, y - 20, 40, 40);
QBrush brush(map);
brush.setTransform(transform);
painter.setBrush(brush);
painter.save();
pen.setColor(Qt::white);
painter.setPen(pen);
painter.drawEllipse(x - 60, y -60, 120, 120);
painter.restore();
QWidget::paintEvent(pEvent);
}
Qt实现放大镜功能
于 2022-09-01 09:21:26 首次发布