-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (121 loc) · 4 KB
/
Copy pathmain.cpp
File metadata and controls
143 lines (121 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "mainwindow.h"
#include <QApplication>
#include <QSettings>
#include <QTranslator>
#include <QLocale>
#include <QSplashScreen>
#include <QSharedMemory>
#include <QSystemSemaphore>
#ifdef Q_OS_WIN
#include <windows.h>
struct SharedData
{
DWORD processId;
HWND windowHandle;
};
#elif defined(Q_OS_LINUX)
#include <X11/Xlib.h>
typedef Window SharedData;
#elif defined(Q_OS_MACOS)
#include <AppKit/AppKit.h>
typedef pid_t SharedData;
#endif
int main(int argc, char *argv[])
{
QSystemSemaphore semaphore("MTC_Now_Semaphore", 1, QSystemSemaphore::Open);
semaphore.acquire();
QSharedMemory instanceCheck("MTC_Now_Instance_Check");
bool isRunning = false;
if (!instanceCheck.create(1))
{
if (instanceCheck.attach())
{
instanceCheck.detach();
if (!instanceCheck.create(1))
isRunning = true;
}
else
isRunning = true;
}
if (isRunning)
{
QSharedMemory windowHandleMem("MTC_Now_Window_Handle");
if (windowHandleMem.attach(QSharedMemory::ReadOnly))
{
windowHandleMem.lock();
SharedData data;
memcpy(&data, windowHandleMem.constData(), sizeof(SharedData));
#ifdef Q_OS_WIN
if (IsWindow(data.windowHandle))
{
AllowSetForegroundWindow(data.processId);
ShowWindow(data.windowHandle, SW_RESTORE);
SetForegroundWindow(data.windowHandle);
}
#elif defined(Q_OS_LINUX)
Display* display = XOpenDisplay(nullptr);
if (display && data)
{
XRaiseWindow(display, data);
XSetInputFocus(display, data, RevertToParent, CurrentTime);
XFlush(display);
XCloseDisplay(display);
}
#elif defined(Q_OS_MACOS)
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:data];
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif
windowHandleMem.unlock();
windowHandleMem.detach();
}
semaphore.release();
return 0;
}
semaphore.release();
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
QApplication a(argc, argv);
QApplication::setOrganizationName("MTC");
QApplication::setApplicationName("Now");
//翻译
bool success = false;
QLocale locale = QLocale::system();
QSettings settings;
QString language = settings.value("Language", locale.name()).toString();
//如果语言为空字符串,表示使用系统默认语言
if(language.isEmpty())
language = locale.name();
QString file = QString(":/translation/MTC_Now_%1.qm").arg(language);
QTranslator translator;
success = translator.load(file);
if(success)
a.installTranslator(&translator);
//启动界面
QSplashScreen splash(QPixmap(":/resource/splash.png"));
splash.setFont(QFont("Arial", 18, QFont::Bold));
splash.showMessage("Now V1.0.0\nCopyright MTC(TEAM) All Rights Reserved", Qt::AlignCenter|Qt::AlignBottom, Qt::black);
splash.show();
MainWindow w;
w.show();
semaphore.acquire();
QSharedMemory windowHandleMem("MTC_Now_Window_Handle");
if (windowHandleMem.create(sizeof(SharedData)))
{
windowHandleMem.lock();
SharedData data;
#ifdef Q_OS_WIN
data.processId = GetCurrentProcessId();
data.windowHandle = (HWND)w.winId();
#elif defined(Q_OS_LINUX)
data = w.winId();
#elif defined(Q_OS_MACOS)
data = getpid();
#endif
memcpy(windowHandleMem.data(), &data, sizeof(SharedData));
windowHandleMem.unlock();
}
semaphore.release();
splash.finish(&w);
return a.exec();
}