Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

winapi - Window manipulation using window-handle from different process on MS Windows(XP)

Is it possible to do some operations with a window from other process having it's handle? What I would like to do is: -remove window decoration -move window to any position of the screen -let window stay on top without making it modal

A link to a command line tool, script or C/C++ code snippet be great.

Thank you very much!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I decided to take another shot, so I experimented with your code and added what was missing: a -naked option.

The key to remove the decoration came from here. Though it works, you will eventually find out that the application who got naked might start displaying a few bugs after it.

Enjoy:

#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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...