gdany
gdany

Reputation: 104

How do I interact with other applications in C++?

I want to know if there is some way to change the positioning of other applications is C++. What I want to do is move the active window on the screen. For example I want to move Firefox in a circular move. At 2:17 http://www.youtube.com/watch?v=TLqPepLhDTY&list=WL8D6E1A188FBFE181 the browser moves, how do I do that?

Upvotes: 3

Views: 1438

Answers (4)

gdany
gdany

Reputation: 104

Wow, thank you guys, I wasn't expecting to answer so fast. I will try and see which code would be best to use. It finally works the way I want it. The code looks like this:

#include <windows.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#pragma comment(lib, "user32.lib")

HWND windowHandle;

BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR title[500];
    ZeroMemory(title, sizeof(title));

    //string strTitle;

    GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));

    //_tprintf(_T("Found window: %s\n"), title);

    //strTitle += title; // Convert to std::string
    if(_tcsstr(title, _T("Firefox")))
    {
        windowHandle = hWnd;
        return FALSE;
    }
    return TRUE;
}
int xLoc = 10;
int yLoc = 50;

int getXLoc(){
    if(xLoc<70&&yLoc<=30){
        xLoc += 1;
        Sleep(10);
    }else if(yLoc>30){
        xLoc -= 1;
        Sleep(10);
    }
    return xLoc;
}
int getYLoc(){
    if(yLoc>10&&xLoc<30){
        yLoc -= 1;
        Sleep(10);
    }else if(xLoc>=30&&yLoc<70){
        yLoc += 1;
        Sleep(10);
    }
    return yLoc;
}

int main()
{
    windowHandle = NULL;
    EnumWindows(MyEnumProc, 0);
    while(true){
        MoveWindow(windowHandle, getXLoc(), getYLoc(), 1220, 930, false);
        Sleep(5);
    }
    return 0;
}

Upvotes: 0

marcinj
marcinj

Reputation: 49976

You should first use FindWindow() function get HWND of your window. Then you can simply use SetWindowPos() or MoveWindow() with found HWND to change position.

Upvotes: 4

Proxy
Proxy

Reputation: 1854

Actually using FindWindow on a window you didn't make is difficult because you need the class name used on that window. Use EnumWindows instead. Here's a code sample that searches for and closes any window with "Firefox" in it's name. Continually send WM_MOVE instead of WM_CLOSE to move the window.

#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "user32.lib")

HWND windowHandle;

BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR title[500];
    ZeroMemory(title, sizeof(title));

    //string strTitle;

    GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));

    //_tprintf(_T("Found window: %s\n"), title);

    //strTitle += title; // Convert to std::string
    if(_tcsstr(title, _T("Firefox")))
    {
        windowHandle = hWnd;
        return FALSE;
    }
    return TRUE;
}

int main()
{
    while (true)
    {
        windowHandle = NULL;
        EnumWindows(MyEnumProc, 0);

        cout << endl << endl << "Desired window handle: " << windowHandle << endl << "Sending WM_CLOSE message..." << endl;
        SendMessage(windowHandle, WM_CLOSE, NULL, NULL);
    }
    return 0;
}

Upvotes: 0

sehe
sehe

Reputation: 392833

On linux, mac and FreeBSD: Here's a quick script that uses xdotool to slide the chrome browser along some trajectory:

#!/bin/bash

while read x y
do 
    xdotool search --class google-chrome windowmove $x $y
    sleep 0.001
done <<TRAJECTORY
624 624
634 614
644 624
654 614
664 624
674 614
684 624
694 614
704 624
TRAJECTORY

Notes

  • I've made the trajectory pretty boring
    You can use some elementary trigs to make the motion circular though. The sample shows you can even feed shapes from a list
  • The example hardcodes the target window (but you could use e.g. selectwindow to have the user click any window to operate on)
    Again your imagination is the limit

Upvotes: 0

Related Questions