Skip to content

Instantly share code, notes, and snippets.

@sergnechaev
Created October 11, 2016 06:01
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
C++ WinAPI, close window by title
#include <windows.h>
#include <iostream>
int main(int argc, char* argv[]) {
if (argc <= 1) {
std::cerr << "ERROR: Please, specify the window title to close" << std::endl;
return EXIT_FAILURE;
}
auto hwnd = FindWindow(nullptr, argv[1]);
if (hwnd != nullptr) {
std::cout << "Closing \"" << argv[1] << "\"" << std::endl;
PostMessage(hwnd, WM_CLOSE, 0, 0);
} else {
std::cerr << "ERROR: Can't find the window to close" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
@choi303
Copy link

choi303 commented Dec 12, 2020

It was very useful for me. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment