How to get name of active window?

有些话、适合烂在心里 提交于 2019-12-08 05:42:08

问题


I've confronted with problem of getting active window's name.

When I use this code:

HWND currentWindowHWND = GetForegroundWindow();
char title[100];
GetWindowTextA(currentWindowHWND, title, 100);

I get something like: "How to get active window's name? - Stack Overflow - Google Chrome".

But I want to get "Google Chrome", which WINAPI function should I use?


回答1:


in a c code use the following winapi functions:

DWORD WINAPI GetModuleFileName(
  _In_opt_  HMODULE hModule,
  _Out_     LPTSTR lpFilename,
  _In_      DWORD nSize
);

or

DWORD WINAPI GetModuleBaseName(
  _In_      HANDLE hProcess,
  _In_opt_  HMODULE hModule,
  _Out_     LPTSTR lpBaseName,
  _In_      DWORD nSize
);

How to get the process name in C++


In c#:

Int32 pid = win32.GetWindowProcessID(hwnd);
Process p = Process.GetProcessById(pid);
string appName = p.ProcessName;

You should get the process name and not the window's title.



来源:https://stackoverflow.com/questions/14540118/how-to-get-name-of-active-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!