Qt实现放大镜功能

20 篇文章 1 订阅
利用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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pailugou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值