44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
#include "mainwindow.h"
|
||
#include <windows.h>
|
||
|
||
#include <QApplication>
|
||
#include <QDir>
|
||
#include <QFileInfoList>
|
||
#include <QDebug>
|
||
#include <QSettings>
|
||
|
||
|
||
void SetProcessAutoRunSelf(const QString &appPath)
|
||
{
|
||
//注册表路径需要使用双反斜杠,如果是32位系统,要使用QSettings::Registry32Format
|
||
QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
|
||
QSettings::Registry64Format);
|
||
|
||
//以程序名称作为注册表中的键
|
||
//根据键获取对应的值(程序路径)
|
||
QFileInfo fInfo(appPath);
|
||
QString name = fInfo.baseName();
|
||
QString path = settings.value(name).toString();
|
||
|
||
//如果注册表中的路径和当前程序路径不一样,
|
||
//则表示没有设置自启动或自启动程序已经更换了路径
|
||
//toNativeSeparators的意思是将"/"替换为"\"
|
||
QString newPath = QDir::toNativeSeparators(appPath);
|
||
if (path != newPath)
|
||
{
|
||
|
||
settings.setValue(name, newPath);
|
||
}
|
||
}
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
|
||
|
||
QApplication a(argc, argv);
|
||
MainWindow w;
|
||
w.show();
|
||
|
||
return a.exec();
|
||
}
|