打开exe,再次打开进行提示。
main.cpp添加:
#include "QtFilePreview.h"
#include <QtWidgets/QApplication>
#include <windows.h>
#include <qmessagebox.h>
#pragma execution_character_set("utf-8")bool CheckRun()
{// 创建互斥量HANDLE m_hMutex = CreateMutex(NULL, FALSE, L"onlyone");// 检查错误代码if (GetLastError() == ERROR_ALREADY_EXISTS) {// 如果已有互斥量存在则释放句柄并复位互斥量CloseHandle(m_hMutex);m_hMutex = NULL;// 程序退出return false;}elsereturn true;
}int main(int argc, char *argv[])
{QApplication a(argc, argv);if (CheckRun() == false){QMessageBox::information(nullptr,"tips","this program has already been run\n程序已经运行");return 0;}QtFilePreview w;w.show();return a.exec();
}