修改设置界面为独立弹窗

master
Mike Solar 2023-08-01 15:22:48 +08:00
parent ca35018346
commit 9b53ef9764
20 changed files with 319 additions and 472 deletions

Binary file not shown.

View File

@ -101,9 +101,7 @@
<ClInclude Include="config.h" />
<ClInclude Include="globalvariables.h" />
<QtMoc Include="qminiblink.h" />
<QtMoc Include="settingsscreen.h" />
<QtMoc Include="switchbutton.h" />
<QtMoc Include="mythread.h" />
<QtMoc Include="mysettingsdialog.h" />
<ClInclude Include="wke.h" />
<QtMoc Include="mainwindowlayout.h" />
<QtMoc Include="mainwindow.h" />
@ -118,22 +116,18 @@
<ClCompile Include="mainwindow.cpp" />
<ClCompile Include="mainwindowlayout.cpp" />
<ClCompile Include="MyButton.cpp" />
<ClCompile Include="mythread.cpp" />
<ClCompile Include="mysettingsdialog.cpp" />
<ClCompile Include="navbar.cpp" />
<ClCompile Include="netio.cpp" />
<ClCompile Include="qminiblink.cpp" />
<ClCompile Include="settingsscreen.cpp" />
<ClCompile Include="switchbutton.cpp" />
</ItemGroup>
<ItemGroup>
<QtUic Include="mainscreen.ui" />
<QtUic Include="mainwindow.ui" />
<QtUic Include="mainwindowlayout.ui" />
<QtUic Include="mysettingsdialog.ui" />
<QtUic Include="navbar.ui" />
<QtUic Include="qminiblink.ui" />
<QtUic Include="qtwidgetsclass.ui" />
<QtUic Include="settingsscreen.ui" />
<QtUic Include="switchbutton.ui" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">

View File

@ -63,13 +63,7 @@
<QtMoc Include="qminiblink.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="settingsscreen.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="switchbutton.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="mythread.h">
<QtMoc Include="mysettingsdialog.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup>
@ -98,13 +92,7 @@
<ClCompile Include="qminiblink.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="settingsscreen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="switchbutton.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="mythread.cpp">
<ClCompile Include="mysettingsdialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
@ -121,17 +109,11 @@
<QtUic Include="mainwindowlayout.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="settingsscreen.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="qtwidgetsclass.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="switchbutton.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="qminiblink.ui">
<Filter>Form Files</Filter>
</QtUic>
<QtUic Include="mysettingsdialog.ui">
<Filter>Form Files</Filter>
</QtUic>
</ItemGroup>
</Project>

View File

@ -5,9 +5,9 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtLastBackgroundBuild>2023-07-30T18:20:21.4819233Z</QtLastBackgroundBuild>
<QtLastBackgroundBuild>2023-08-01T03:05:45.8140795Z</QtLastBackgroundBuild>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<QtLastBackgroundBuild>2023-07-30T18:20:22.3001457Z</QtLastBackgroundBuild>
<QtLastBackgroundBuild>2023-08-01T03:05:46.4588999Z</QtLastBackgroundBuild>
</PropertyGroup>
</Project>

View File

@ -11,7 +11,6 @@
#include <exception>
#include <QResizeEvent>
#include "globalvariables.h"
#include "settingsscreen.h"
#include "mythread.h"
#include "config.h"
@ -33,12 +32,8 @@ MainScreen::MainScreen(QWidget *parent) :
button->setText(QString::fromLocal8Bit("´ò¿ªÎ¢ÐÅ"));
layout = new QHBoxLayout;
miniblink = new QMiniBlink(this);
settingScreen = new SettingsScreen;
settingScreen->setMinimumHeight(height());
settingScreen->setHidden(true);
layout->addWidget(widget, 1);
layout->addWidget(miniblink, 4);
layout->addWidget(settingScreen);
this->setLayout(layout);
layout->setMargin(0);
//miniblink->setAttribute(Qt::WA_DeleteOnClose);
@ -65,10 +60,8 @@ MainScreen::~MainScreen() {
delete button;
emit ondestroy();
layout->removeWidget(miniblink);
layout->removeWidget(settingScreen);
layout->removeWidget(widget);
delete miniblink;
delete settingScreen;
delete widget;
delete layout;
delete ui;
@ -83,26 +76,17 @@ void MainScreen::clickButton1(QString text, QString url) {
if (text == QString::fromLocal8Bit("΢ÐŶ࿪")) {
widget->setHidden(false);
miniblink->setHidden(false);
settingScreen->setHidden(true);
emit changeUrl(url_full);
}
else if (text == QString::fromLocal8Bit("Èí¼þÉèÖÃ")){
miniblink->setHidden(true);
widget->setHidden(true);
settingScreen->setHidden(false);
}
else {
widget->setHidden(true);
miniblink->setHidden(false);
settingScreen->setHidden(true);
emit changeUrl(url_full);
}
}
void MainScreen::startWeChat() {
QString path = QApplication::applicationDirPath();
QString exe = path + "/wxdk.exe";
QString exe = path + "/app/wxdk.exe";
ShellExecute(GetDesktopWindow(), L"open", exe.toStdWString().c_str(), L"79c86fb12b36dfa33d1a537c9af100b4c7928a9c", path.toStdWString().c_str(), SW_SHOW);
}
//void MainScreen::resizeEvent(QResizeEvent *event) {

View File

@ -11,7 +11,6 @@
#include <QPushButton>
#include "wke.h"
#include "qminiblink.h"
#include "settingsscreen.h"
class MyThread;
QT_BEGIN_NAMESPACE
namespace Ui { class MainScreen; }
@ -43,7 +42,6 @@ private:
QWidget *widget;
MyThread *thread;
QMiniBlink *miniblink;
SettingsScreen *settingScreen;
private slots:
void startWeChat();

View File

@ -6,6 +6,7 @@
#include "mainwindowlayout.h"
#include "ui_MainWindowLayout.h"
#include "mysettingsdialog.h"
MainWindowLayout::MainWindowLayout(QWidget *parent) :
QWidget(parent), ui(new Ui::MainWindowLayout) {
@ -47,10 +48,15 @@ void MainWindowLayout::resizeEvent(QResizeEvent *event) {
void MainWindowLayout::clickButton(QString text, QString url) {
QString path = QApplication::applicationDirPath();
path += "/startcalc.exe";
path += "/app/startcalc.exe";
if (text== QString::fromLocal8Bit("¼ÆËãÆ÷")) {
WinExec(path.toStdString().c_str(), SW_SHOW);
}
else if (text == QString::fromLocal8Bit("软件设置")) {
MySettingsDialog *settings = new MySettingsDialog;
settings->exec();
delete settings;
}
else{
emit clickButton1(text, url);
}

View File

@ -0,0 +1,107 @@
#include "mysettingsdialog.h"
#include <Windows.h>
#include <QClipBoard>
#include "netio.h"
#include "globalvariables.h"
MySettingsDialog::MySettingsDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
this->setFixedSize(width(), height());
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) {
ui.autostart->setDisabled(true);
}
QString app = QApplication::applicationFilePath();
app.replace("/", "\\");
DWORD size=128;
wchar_t reg[128];
lRet = RegGetValue(hRoot, szSubKey, L"OfficeAssistant", RRF_RT_ANY, nullptr, reg, &size);
if (wcscmp(reg,app.toStdWString().c_str())==0) {
ui.autostart->setChecked(true);
}
copyButton = ui.copy;
connect(copyButton, &QPushButton::clicked, this, &MySettingsDialog::copyToCLipboard);
connect(ui.autostart, &QCheckBox::stateChanged, this, &MySettingsDialog::autoStart);
connect(ui.addshortcut, &QPushButton::clicked, this, &MySettingsDialog::createShortcut);
connect(ui.law_button, &QPushButton::clicked, this, &MySettingsDialog::law);
ui.code->setText(QString(QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5).toHex()));
}
MySettingsDialog::~MySettingsDialog()
{
}
void MySettingsDialog::autoStart(int state)
{
if (state == Qt::Checked) {
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();
app.replace("/", "\\");
qDebug() << app.toStdWString().c_str();
lRet = RegSetValueEx(hKey, L"OfficeAssistant", 0, REG_SZ, (BYTE*)app.toStdWString().c_str(), app.length()*sizeof(wchar_t));
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建开机启动项成功"));
}
RegCloseKey(hKey);
}
else if (state == Qt::Unchecked) {
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"OfficeAssistant");
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("移除开机启动项成功。"));
}
RegCloseKey(hKey);
}
}
void MySettingsDialog::law()
{
QString path = QApplication::applicationDirPath();
QString license = path + "/license.txt";
license.replace("/", "\\");
ShellExecute(GetDesktopWindow(), L"open", L"notepad.exe" , license.toStdWString().c_str(), nullptr, SW_SHOW);
}
void MySettingsDialog::copyToCLipboard() {
QClipboard *clip = QApplication::clipboard();
clip->setText(QString(QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5).toHex()));
}
void MySettingsDialog::createShortcut() {
QString deskTopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
deskTopPath = deskTopPath + QString::fromLocal8Bit("/办公助手.lnk");
QString srcFile = QApplication::applicationFilePath();
bool succeed=QFile::link(srcFile, deskTopPath);
if (succeed) {
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建快捷方式成功。"));
}
else {
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建快捷方式失败。"));
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <QDialog>
#include "ui_mysettingsdialog.h"
#include <QPushButton>
class MySettingsDialog : public QDialog
{
Q_OBJECT
public:
MySettingsDialog(QWidget *parent = Q_NULLPTR);
~MySettingsDialog();
private:
Ui::MySettingsDialog ui;
QPushButton *copyButton;
public slots:
void copyToCLipboard();
void createShortcut();
void autoStart(int state);
void law();
};

View File

@ -0,0 +1,158 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MySettingsDialog</class>
<widget class="QDialog" name="MySettingsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>736</width>
<height>540</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>设置</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>70</x>
<y>70</y>
<width>581</width>
<height>381</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="hardware">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>硬件信息</string>
</property>
<widget class="QLabel" name="codetitle">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>108</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>硬件ID</string>
</property>
</widget>
<widget class="QLabel" name="code">
<property name="geometry">
<rect>
<x>130</x>
<y>40</y>
<width>401</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="copy">
<property name="geometry">
<rect>
<x>130</x>
<y>70</y>
<width>141</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>复制</string>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QGroupBox" name="settings">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>设置</string>
</property>
<widget class="QCheckBox" name="autostart">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>138</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>开机启动</string>
</property>
</widget>
<widget class="QPushButton" name="addshortcut">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y>
<width>291</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>创建桌面快捷方式</string>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QGroupBox" name="law">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>法律声明</string>
</property>
<widget class="QPushButton" name="law_button">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>291</width>
<height>46</height>
</rect>
</property>
<property name="text">
<string>查看法律声明和许可协议</string>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -70,12 +70,6 @@ RequestBodyBase::RequestBodyBase(){
exit(1);
}
release=obj_root.value("release").toString();
if(obj_root.value("version")==QJsonValue::Undefined){
QMessageBox::warning(nullptr, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("配置文件损坏"));
exit(1);
}
version=obj_root.value("version").toString();
device_id=obj_root.value("device_id").toString();
}else{
//处理错误
QMessageBox::warning(nullptr, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("配置文件损坏"));
@ -113,30 +107,14 @@ RequestBodyBase::RequestBodyBase(){
default:
os_version="Unknown";
}
//如果device_id是空值
if(device_id.isEmpty()){
//读取MachineGUID并取MD5作为device_id
QByteArray hash = QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5);
device_id=hash.toHex();
infFile=new QFile(dir + "/config/information.kmd");
if(!infFile->open(QIODevice::WriteOnly|QIODevice::Text)){
//处理错误
QMessageBox::warning(nullptr, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("无法覆写配置文件"));
infFile->close();
delete infFile;
exit(1);
}
device_id = hash.toHex();
//加入json序列
QJsonValue value=device_id;
QJsonValue value = device_id;
QJsonObject obj_root = qJsonDocument.object();
obj_root.insert("device_id",value);
obj_root.insert("device_id", value);
qJsonDocument.setObject(obj_root);
//写入配置文件
infFile->write(qJsonDocument.toJson());
//关闭文件;
infFile->close();
delete infFile;
}
QJsonValue os_json(this->os);
QJsonValue os_version_json(this->os_version);
QString key_hash = QCryptographicHash::hash(QString(KEY).toUtf8(), QCryptographicHash::Md5).toHex();
@ -145,7 +123,7 @@ RequestBodyBase::RequestBodyBase(){
sign = sign_byte.toHex();
QJsonValue sign_json(sign);
QJsonValue requestId_json=QJsonValue(request_id);
QJsonObject obj_root=qJsonDocument.object();
obj_root=qJsonDocument.object();
//插入request_id
obj_root.insert(QString::fromLocal8Bit("request_id"),requestId_json);
obj_root.insert(QString::fromLocal8Bit("os"), os_json);

View File

@ -1,23 +0,0 @@
<UI version="4.0" >
<class>QtWidgetsClass</class>
<widget class="QWidget" name="QtWidgetsClass" >
<property name="objectName" >
<string notr="true">QtWidgetsClass</string>
</property>
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>QtWidgetsClass</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</UI>

View File

@ -1,58 +0,0 @@
#include "settingsscreen.h"
#include "config.h"
#include <QCryptographicHash>
#include "netio.h"
SettingsScreen::SettingsScreen(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
autoStart = new SwitchButton(ADD_AUTOSTART,this);
addDesktopLink = new QPushButton(this);
addDesktopLink->setText(QString::fromLocal8Bit("创建桌面快捷方式"));
connect(addDesktopLink, &QPushButton::pressed, this, &SettingsScreen::createShortCut);
layout = new QHBoxLayout(this);
left = new QVBoxLayout(this);
addDesktopLink_label = new QLabel(QString::fromLocal8Bit("添加桌面快捷方式"),this);
autoStart_label = new QLabel(QString::fromLocal8Bit("开机启动"),this);
autoStart->setMaximumSize(100, 50);
autoStart->setMinimumSize(50, 25);
left->addWidget(autoStart_label);
left->addWidget(autoStart);
left->addWidget(addDesktopLink_label);
left->addWidget(addDesktopLink);
left->setAlignment(Qt::AlignCenter);
left->setMargin(0);
layout->addLayout(left);
information = new QLabel;
QString device_id = QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5).toHex();
QString inf_str = QString::fromLocal8Bit("硬件ID:\n");
inf_str += device_id;
inf_str+= QString::fromLocal8Bit("\n本软件采用了QtQt是The Qt Company的产品采用LGPL协议发布。\n");
inf_str += QString::fromLocal8Bit("本软件采用了MiniBlinkMiniBlink是珠海独角兽科技有限公司的产品采用Apache 2.0协议发布。\n");
information->setText(inf_str);
layout->addWidget(information);
}
void SettingsScreen::createShortCut() {
QString deskTopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
deskTopPath = deskTopPath + QString::fromLocal8Bit("/办公助手.lnk");
QString srcFile = QApplication::applicationFilePath();
QFile::link(srcFile, deskTopPath);
}
SettingsScreen::~SettingsScreen()
{
left->removeWidget(autoStart);
left->removeWidget(addDesktopLink);
left->removeWidget(addDesktopLink_label);
left->removeWidget(autoStart_label);
delete autoStart;
delete addDesktopLink;
delete addDesktopLink_label;
delete autoStart_label;
layout->removeItem(left);
delete left;
layout->removeWidget(information);
delete information;
delete layout;
}

View File

@ -1,31 +0,0 @@
#pragma once
#include <QWidget>
#include <QHBoxLayout>
#include <QVboxLayout>
#include <QPushButton>
#include <QLabel>
#include "ui_settingsscreen.h"
#include "switchbutton.h"
class SettingsScreen : public QWidget
{
Q_OBJECT
public:
SettingsScreen(QWidget *parent = Q_NULLPTR);
~SettingsScreen();
private:
Ui::SettingsScreen ui;
QLabel *autoStart_label;
QLabel *addDesktopLink_label;
QPushButton *addDesktopLink;
SwitchButton *autoStart;
QHBoxLayout *layout;
QVBoxLayout *left;
QLabel *information;
private slots:
void createShortCut();
};

View File

@ -1,23 +0,0 @@
<UI version="4.0" >
<class>SettingsScreen</class>
<widget class="QWidget" name="SettingsScreen" >
<property name="objectName" >
<string notr="true">SettingsScreen</string>
</property>
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>SettingsScreen</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</UI>

View File

@ -1,188 +0,0 @@
#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()
{
}

View File

@ -1,41 +0,0 @@
#pragma once
#include <QWidget>
#include <QPainter>
#include <QTimer>
#include "ui_switchbutton.h"
class SwitchButton : public QWidget
{
Q_OBJECT
public:
SwitchButton(int action, QWidget * parent);
~SwitchButton();
private:
int action;
Ui::SwitchButton ui;
bool checked;
QColor bgColorOff;
QColor bgColorOn;
QColor sliderColorOff;
QColor sliderColorOn;
int space;
int rectRadius;
int step;
int startX;
int endX;
QTimer *timer;
void updateValue();
void drawBg(QPainter * painter);
void drawSlider(QPainter * painter);
protected:
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
};

View File

@ -1,23 +0,0 @@
<UI version="4.0" >
<class>SwitchButton</class>
<widget class="QWidget" name="SwitchButton" >
<property name="objectName" >
<string notr="true">SwitchButton</string>
</property>
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>SwitchButton</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</UI>

View File

@ -71,12 +71,14 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)$(Configuration)\app\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)$(Configuration)\app</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>

View File

@ -71,12 +71,14 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)$(Configuration)\app\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)$(Configuration)\app</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>