#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <string>
using namespace std;
HWND previousForegroundWindow = nullptr;
HWND 自身窗口句柄;
bool GetProcessNameFromHWND(HWND hwnd, string& processName) {
DWORD processId;
if (GetWindowThreadProcessId(hwnd, &processId)) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (hProcess) {
char moduleName[MAX_PATH];
if (GetModuleFileNameExA(hProcess, NULL, moduleName, MAX_PATH)) {
processName = moduleName;
CloseHandle(hProcess);
return true;
}
CloseHandle(hProcess);
}
}
return false;
}
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
HWND currentForegroundWindow = GetForegroundWindow();
string characters;
if (currentForegroundWindow != previousForegroundWindow && currentForegroundWindow !=自身窗口句柄 && currentForegroundWindow != 00000000) {
char title[256];
GetWindowTextA(currentForegroundWindow, title, sizeof(title));
char className[256];
GetClassNameA(currentForegroundWindow, className, sizeof(className));
string processName;
GetProcessNameFromHWND(currentForegroundWindow, processName);
std::cout << "窗口标题:" << title << std::endl;
std::cout << "窗口类名:" << className << std::endl;
std::cout << "进程路径:" << processName << std::endl;
for (int i = 0; i < processName.length() +10; ++i) {
characters += '-';
}
std::cout << characters << std::endl;
}
previousForegroundWindow = currentForegroundWindow;
}
int main() {
std::cout << "www.52pojie.cn" << std::endl;
自身窗口句柄 = GetConsoleWindow();
SetTimer(NULL, 0, 1, TimerProc);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
评论好像发不了附件,如果你有C++的话,编译一下,这个只有十几kb |