I'm looking to mess around with C++ and build some kind of desktop application that can interact with other windows. Something like a window tiling manager (e.g. minimizing current windows, snapping windows into a grid etc). Is this possible to do in C++? I've only ever worked with command line stuff, so is there a good framework for this kind of work? Anything in the right direction or how I can accomplish something like this would be awesome.
-
It most definitely can. – chris Aug 02 '13 at 18:23
-
2Depends if the operating system and window manager will let you (as in, it provides the necessary functions). – Drew McGowen Aug 02 '13 at 18:23
-
Typically, yes. But as Drew said, it depends on the operating system and window manager, so let us know that and we may be able to help you further. Some have APIs that will allow you to do this easily, while some may be a bit more difficult. – Daniel Underwood Aug 02 '13 at 18:25
-
1Take a look at Qt (http://qt-project.org). It's a multi platform c++ gui application framework with windows/desktop management. – Werner Erasmus Aug 02 '13 at 18:41
3 Answers
In Windows
You can use EnumWindows
to iterate through each of the windows. It starts from the topmost window and works its way down. The following code will loop through each visible window and print out the winow's text.
#include <windows.h>
#include <stdio.h>
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
char buff[255];
if (IsWindowVisible(hWnd)) {
GetWindowText(hWnd, (LPSTR) buff, 254);
printf("%s\n", buff);
}
return TRUE;
}
int main() {
EnumWindows(EnumWindowsProc, 0);
return 0;
}
Since you have the handle of each window, you can do further manipulations by sending messages to them.
I created a tool to play with windows and shapes called Desktop Playground that uses this very method.
I spawn off a thread and store each window in a hash table with their coordinates and size. Then I iterate through and compare their current position and size with their previous one and execute callbacks to let my main thread know whether a window was Created
, Moved
, Reiszed
, or Destroyed
.

- 1
- 1

- 4,776
- 4
- 32
- 52
On Windows you can use the SendMessage function to active windows or processes.

- 1,578
- 2
- 15
- 25
This question is highly related on which OS, or GUI framework is used for such application you want to create.
If the OS supports interfaces for such interaction, it certainly can be used with C++ if there's a certain language binding to the GUI control API (C/C++) provided. Usually it's not a good idea to use these API's natively from your code, but through a C++ library that abstracts the low level bits and operations out.
At a next level there are libraries available, that even support abstractions for diverse OS specific GUI and System Control APIs. If you're looking for OS portable code, check out the Qt framework for example.

- 1
- 13
- 116
- 190