Files
OfficeAssistant_Win10/OfficeAssistant_msvc/switchbutton.cpp
2023-07-31 19:06:37 +08:00

189 lines
4.1 KiB
C++

#include "switchbutton.h"
#include "config.h"
#include <Windows.h>
#include <QMessageBox>
#include <QFile>
SwitchButton::SwitchButton(int action,QWidget *parent=nullptr)
: QWidget(parent)
{
ui.setupUi(this);
checked = false;
this->action = action;
//开、闭时背景颜色
bgColorOff = QColor(225, 225, 225);
bgColorOn = QColor(250, 250, 250);
//开、闭时滑块颜色
sliderColorOff = QColor(100, 100, 100);
sliderColorOn = QColor(100, 184, 255);
space = 2; //背景间隔
rectRadius = 5;
step = width() / 50; //步长
//起始、目标位置
startX = 0;
endX = 0;
timer = new QTimer(this);
timer->setInterval(10);
connect(timer, &QTimer::timeout, this, &SwitchButton::updateValue);
}
void SwitchButton::updateValue()
{
if (checked) {
if (startX < endX) {
startX = startX + step;
}
else {
startX = endX;
timer->stop();
}
}
else {
if (startX > endX) {
startX = startX - step;
}
else {
startX = endX;
timer->stop();
}
}
update();
}
void SwitchButton::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
checked = !checked;
if (action == ADD_AUTOSTART) {
if (checked == true) {
HKEY hRoot = HKEY_CURRENT_USER;
wchar_t *szSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY; // 如果不存在不创建
LONG lRet = RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS) {
QMessageBox::critical(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建开机启动项失败"));
return;
}
QString app = QApplication::applicationFilePath();
lRet = RegSetValueEx(hKey, L"SelfRun", 0, REG_SZ, (BYTE*)app.toStdWString().c_str(), app.length());
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建开机启动项成功"));
}
RegCloseKey(hKey);
}
else {
HKEY hRoot = HKEY_CURRENT_USER;
wchar_t *szSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY; // 如果不存在不创建
LONG lRet = RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS) {
QMessageBox::critical(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("移除开机启动项失败"));
return;
}
QString app = QApplication::applicationFilePath();
lRet = RegDeleteValue(hKey, L"SelfRun");
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("移除开机启动项成功"));
}
RegCloseKey(hKey);
}
}
//每次移动的步长为宽度的 50分之一
step = width() / 50;
//状态切换改变后自动计算终点坐标
if (checked)
{
endX = width() - width() / 2;
}
else
{
endX = 0;
}
timer->start();
}
void SwitchButton::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
//每次移动的步长为宽度的 50分之一
step = width() / 50;
//尺寸大小改变后自动设置起点坐标为终点
if (checked)
{
startX = width() - width() / 2;
}
else
{
startX = 0;
}
update();
}
void SwitchButton::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//绘制背景
drawBg(&painter);
//绘制滑块
drawSlider(&painter);
}
void SwitchButton::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
if (!checked)
{
painter->setBrush(bgColorOff);
}
else
{
painter->setBrush(bgColorOn);
}
painter->drawRoundedRect(rect(), rectRadius, rectRadius);
painter->restore();
}
void SwitchButton::drawSlider(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
if (!checked) {
painter->setBrush(sliderColorOff);
}
else {
painter->setBrush(sliderColorOn);
}
int sliderWidth = width() / 2 - space * 2;
int sliderHeight = height() - space * 2;
QRect sliderRect(startX + space, space, sliderWidth, sliderHeight);
painter->drawRoundedRect(sliderRect, rectRadius, rectRadius);
painter->restore();
}
SwitchButton::~SwitchButton()
{
}