2010-10-26 1 views
3

Est-il possible de faire certaines opérations avec une fenêtre d'un autre processus ayant son handle? Ce que je voudrais faire est: décoration de fenêtre -remove fenêtre vous suffit de placer à une position de l'écran fenêtre -LET séjour sur sans le rendre modalManipulation de fenêtre à l'aide d'une poignée de fenêtre à partir d'un processus différent sous MS Windows (XP)

Un lien vers un outil de ligne de commande, script ou C/L'extrait de code C++ est génial.

Merci beaucoup!

Répondre

3

j'ai décidé de prendre une autre photo, donc j'expérimenté avec votre code et a ajouté ce qui manquait: une option -naked. La clé pour enlever la décoration came from here. Bien que cela fonctionne, vous finirez par découvrir que l'application qui a été nu peut commencer à afficher quelques bugs après.

Profitez:

#include "windows.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <limits> 

using namespace std; 

#ifdef min 
    #undef min 
#endif 


int main(int argc, char* argv[]) 
{ 
    char** param = argv; 

    unsigned int x = numeric_limits<int>::min(), y=numeric_limits<int>::min(), w=numeric_limits<int>::min(), h=numeric_limits<int>::min(); 
    HWND z = HWND_NOTOPMOST; 
    unsigned int flags = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER); 
    ++param; 

    wstring winTitle; 
    bool close = false; 
    bool naked = false; 
    while (*param) 
    { 
    string sparam(*param); 
    if (sparam == "-title") 
    { 
     ++param; 
     if (!*param) break; 

     sparam = *param; 
     winTitle.resize(sparam.size()); 
     copy(sparam.begin(), sparam.end(), winTitle.begin()); 
    } 
    else if (sparam == "-move") 
    { 
     ++param; 
     if (!*param) break; 

     sparam =*param; 
     stringstream sstr(sparam); 
     char sep; 
     sstr >> x >>sep >> y; 
     if (x != numeric_limits<int>::min() && y != numeric_limits<int>::min()) 
     { 
     flags &= ~SWP_NOMOVE; 
     } 
    } 
    else if (sparam == "-resize") 
    { 
     ++param; 
     if (!*param) break; 

     sparam = *param; 
     stringstream sstr(sparam); 
     char sep; 
     sstr >> w >>sep >> h; 
     if (h != numeric_limits<int>::min() && w != numeric_limits<int>::min()) 
     { 
     flags &= ~SWP_NOSIZE; 
     } 
    } 
    else if (sparam == "-top") 
    { 
     z = HWND_TOP; 
     flags &= ~SWP_NOZORDER; 
    } 
    else if (sparam == "-staytop") 
    { 
     z = HWND_TOPMOST; 
     flags &= ~SWP_NOZORDER; 
    } 
    else if (sparam == "-bottom") 
    { 
     z = HWND_BOTTOM; 
     flags &= ~SWP_NOZORDER; 
    } 
    else if (sparam == "-hide") 
    { 
     flags |= SWP_HIDEWINDOW; 
    } 
    else if (sparam == "-close") 
    { 
     close = true; 
    } 
    else if (sparam == "-naked") 
    { 
     naked = true; 
    } 

    ++param;  
    } 

    if (winTitle.empty()) 
    { 
    return -1; 
    } 

    HWND win_handle = FindWindow(0, winTitle.c_str());  
    if (win_handle != 0) 
    { 
    if (close) 
    { 
     TerminateProcess((HANDLE)GetProcessId(win_handle), 0); 
     return 0; 
    } 

    SetWindowPos(win_handle, z, x, y, w, h, flags); 

    if (naked) 
    { 
     SetWindowLong(win_handle, GWL_STYLE, GetWindowLong(win_handle, GWL_EXSTYLE) | WS_EX_TOPMOST); 
     ShowWindow(win_handle, SW_SHOW); 
    } 
    } 
    else 
    { 
     cout << "!!! FindWindow failed" << endl; 
    } 

    return 0; 
} 
+0

@Valentin Faites-moi savoir quand vous l'avez testé. – karlphillip

+0

Cool! Juste ce dont j'avais besoin! Maintenant c'est complet! –

1

Supprimer la décoration de la fenêtre est un peu difficile. Je suppose que vous devrez probablement mettre en place une prime pour votre question.

Vérifiez les Windows API pour voir les fonctions que vous devriez appeler.

Quoi qu'il en soit, cet extrait devrait démarrer:

HWND win_handle = FindWindow (0, "Title Of The Window");  
if (win_handle != 0) 
{ 
    /* Do your magic */ 

    // MoveWindow params: handle, x=100, y=200, width=400, height=450, repaint 
    MoveWindow(win_handle, 100, 200, 400, 450, TRUE); 

} 
else 
{ 
    std::cout << "!!! FindWindow failed" << std::endl; 
} 
+0

Merci beaucoup pour l'aide getting-started! Cela semble être plus facile que ce à quoi je m'attendais. –

0

Enfin, avec l'aide de karlphillip j'ai pu écrire un outil qui se déplace, redimensionne et colle une fenêtre sur le dessus. Ce qui me manque, c'est comment enlever la décoration et la fermer. Voici le code:

#include "windows.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <limits> 
using namespace std; 

#ifdef min 
    #undef min 
#endif 

int main(int argc, char* argv[]) 
{ 
    char **param= argv; 
    wstring winTitle; 
    bool close = false; 
    unsigned int x=numeric_limits<int>::min(), y=numeric_limits<int>::min(), w=numeric_limits<int>::min(), h=numeric_limits<int>::min(); 
    HWND z=HWND_NOTOPMOST; 
    unsigned int flags=SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER; 
    ++param; 
    while (*param) 
    { 
    string sparam(*param); 
    if (sparam == "-title") 
    { 
     ++param; if (!*param) break; 
     sparam =*param; 
     winTitle.resize(sparam.size()); 
     copy (sparam.begin(), sparam.end(), winTitle.begin()); 
    } 
    else if (sparam == "-move") 
    { 
     ++param; if (!*param) break; 
     sparam =*param; 
     stringstream sstr(sparam); 
     char sep; 
     sstr >> x >>sep >> y; 
     if (x != numeric_limits<int>::min() && y != numeric_limits<int>::min()) 
     { 
     flags &= ~SWP_NOMOVE; 
     } 
    } 

    else if (sparam == "-resize") 
    { 
     ++param; if (!*param) break; 
     sparam =*param; 
     stringstream sstr(sparam); 
     char sep; 
     sstr >> w >>sep >> h; 
     if (h != numeric_limits<int>::min() && w != numeric_limits<int>::min()) 
     { 
     flags &= ~SWP_NOSIZE; 
     } 
    } 

    else if (sparam == "-top") 
    { 
     z = HWND_TOP; 
     flags &= ~SWP_NOZORDER; 
    } 

    else if (sparam == "-staytop") 
    { 
     z = HWND_TOPMOST; 
     flags &= ~SWP_NOZORDER; 
    } 

    else if (sparam == "-bottom") 
    { 
     z = HWND_BOTTOM; 
     flags &= ~SWP_NOZORDER; 
    } 

    else if (sparam == "-hide") 
    { 
     flags |= SWP_HIDEWINDOW; 
    } 

    else if (sparam == "-close") 
    { 
     close=true; 
    } 

    ++param;  
    } 
    if (winTitle.empty()) 
    { 
    return 1; 
    } 

    HWND win_handle = FindWindow (0, winTitle.c_str());  
    if (win_handle != 0) 
    { 

    if(close) 
    { 
     TerminateProcess((HANDLE)GetProcessId(win_handle), 0); 
     return 0; 
    } 

    SetWindowPos(win_handle, z, x, y, w, h, flags); 
    } 
    else 
    { 
     cout << "!!! FindWindow failed" << endl; 
    } 

    //system("pause"); 
    return 0; 
} 
Questions connexes