在MS Windows(XP)上使用来自不同进程的窗口句柄进行窗口操作

时间:2010-10-26 13:07:18

标签: winapi

是否可以使用其他进程的窗口进行某些操作?我想做的是: - 删除窗户装饰 - 将窗口移动到屏幕的任何位置 -let窗口保持在顶部而不是模态

指向命令行工具,脚本或C / C ++代码段的链接非常棒。

非常感谢!

3 个答案:

答案 0 :(得分:3)

我决定再试一次,所以我尝试了你的代码,并添加了遗漏的内容:-naked选项。

删除装饰came from here的关键。虽然它有效,但你最终会发现裸体应用程序可能会在它之后显示一些错误。

享受:

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

答案 1 :(得分:1)

删除窗口装饰有点棘手。我猜你可能要为你的问题设置赏金。

检查Windows API以查看您应该拨打的功能。

无论如何,这个片段应该让你开始:

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

答案 2 :(得分:0)

最后,在karlphillip的帮助下,我能够编写一个移动,调整大小并将窗口固定在顶部的工具。我缺少的是如何去除装饰和关闭。这是代码:

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