forked from itchio/node-asfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_asfw.cc
56 lines (45 loc) · 1.35 KB
/
node_asfw.cc
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
#include "node_asfw.h"
#include <string>
#include <sstream>
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
NAN_METHOD(_SetForegroundWindow) {
if (info[0]->IsUndefined()) {
Nan::ThrowError("SetForegroundWindow needs one argument");
}
auto maybeArg = Nan::To<int64_t>(info[0]);
if (maybeArg.IsNothing()) {
Nan::ThrowError("SetForegroundWindow needs a number argument");
}
HWND hwnd = (HWND)(maybeArg.FromJust());
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
if (GetWindowPlacement(hwnd, &wp)) {
if (wp.showCmd == SW_MINIMIZE || wp.showCmd == SW_SHOWMINIMIZED || wp.showCmd == SW_SHOWMINNOACTIVE) {
ShowWindow(hwnd, SW_RESTORE);
}
else {
ShowWindow(hwnd, SW_SHOW);
}
}
else {
ShowWindow(hwnd, SW_SHOW);
}
BOOL ret = SetForegroundWindow(hwnd);
if (ret == 0) {
DWORD lastError = GetLastError();
if (lastError != 0) {
std::stringstream ss;
ss << "Windows error code " << lastError;
auto err = ss.str();
Nan::ThrowError(err.c_str());
}
}
}
#else // defined(WIN32)
NAN_METHOD(_SetForegroundWindow) {
Nan::ThrowError("SetForegroundWindow is not implemented on this platform");
}
#endif // !defined(WIN32)