-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
105 lines (86 loc) · 2.85 KB
/
util.cpp
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
#include "util.h"
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <psapi.h>
#include <system_error>
#include <shobjidl.h>
#include <propkey.h>
#include <comdef.h>
#include <atlbase.h>
bool Util::setWindowTopMost(QWidget* widget, bool top)
{
if (widget == nullptr)
return false;
// SWP_NOACTIVATE 否则会激活FocusIn事件,导致误判
return SetWindowPos(HWND(widget->winId()), top ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
bool Util::setWindowTop(HWND hwnd)
{
return SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
QString Util::getDirPath(const QString& filePath)
{
return QFileInfo(filePath).absoluteDir().absolutePath();
}
QString Util::getFileName(const QString& filePath)
{
return QFileInfo(filePath).fileName();
}
QString Util::getWindowText(HWND hwnd)
{
if (hwnd == nullptr) return QString();
static WCHAR text[128];
GetWindowTextW(hwnd, text, _countof(text)); //sizeof(text)字节数256 内存溢出
return QString::fromWCharArray(text);
}
QString Util::getProcessExePath(HWND hwnd)
{
DWORD processId = 0;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == 0)
return "";
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (hProcess == NULL)
return "";
TCHAR processName[MAX_PATH] = TEXT("<unknown>");
// https://www.cnblogs.com/mooooonlight/p/14491399.html
if (GetModuleFileNameEx(hProcess, NULL, processName, MAX_PATH)) {
CloseHandle(hProcess);
return QString::fromWCharArray(processName);
}
CloseHandle(hProcess);
return "";
}
QString Util::getFileDescription(const QString& path)
{
CoInitialize(nullptr); // 初始化 COM 库
std::wstring wStr = path.toStdWString();
LPCWSTR pPath = wStr.c_str();
// 使用 CComPtr 自动释放 IShellItem2 接口
CComPtr<IShellItem2> pItem;
HRESULT hr = SHCreateItemFromParsingName(pPath, nullptr, IID_PPV_ARGS(&pItem));
if (FAILED(hr))
throw std::system_error(hr, std::system_category(), "SHCreateItemFromParsingName() failed");
QString desc = getFileName(path);
// 使用 CComHeapPtr 自动释放字符串(调用 CoTaskMemFree)
CComHeapPtr<WCHAR> pValue;
hr = pItem->GetString(PKEY_FileDescription, &pValue);
if (SUCCEEDED(hr))
desc = QString::fromWCharArray(pValue);
else
qDebug() << "No FileDescription, fallback to file name:" << desc;
CoUninitialize(); // 取消初始化 COM 库
return desc;
}
QPoint Util::getWindowPos(HWND hwnd)
{
RECT currentRect;
GetWindowRect(hwnd, ¤tRect);
return QPoint(currentRect.left, currentRect.top);
}
QString Util::getProcessDescription(HWND hwnd)
{
QString exePath = getProcessExePath(hwnd);
return getFileDescription(exePath);
}