Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C++ be used to interact with running applications?

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.

like image 648
Eric Smith Avatar asked Aug 02 '13 18:08

Eric Smith


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.

like image 115
Kirk Backus Avatar answered Sep 27 '22 19:09

Kirk Backus


On Windows you can use the SendMessage function to active windows or processes.

like image 20
Thomas Denney Avatar answered Sep 27 '22 19:09

Thomas Denney


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.

like image 26
πάντα ῥεῖ Avatar answered Sep 27 '22 20:09

πάντα ῥεῖ