在 MS Windows(XP) 上使用来自不同进程的窗口句柄进行窗口操作 [英] Window manipulation using window-handle from different process on MS Windows(XP)

查看:24
本文介绍了在 MS Windows(XP) 上使用来自不同进程的窗口句柄进行窗口操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对来自其他进程的窗口进行一些操作?我想做的是:- 去除窗户装饰- 将窗口移动到屏幕的任意位置- 让窗口保持在顶部而不使其模态

指向命令行工具、脚本或 C/C++ 代码片段的链接最好.

非常感谢!

解决方案

我决定再试一次,所以我尝试了你的代码并添加了缺少的东西:一个 -naked 选项.

去除装饰的关键从这里来.虽然它有效,但您最终会发现裸露的应用程序可能会在此之后开始显示一些错误.

享受:

#include "windows.h"#include #include <字符串>#include #include <算法>#include <限制>使用命名空间标准;#ifdef 分钟#undef 分钟#万一int main(int argc, char* argv[]){字符** 参数 = argv;unsigned int x = numeric_limits::min(), y=numeric_limits::min(), w=numeric_limits::min(), h=numeric_limits::min();HWND z = HWND_NOTOPMOST;无符号整数标志 = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);++参数;wstring winTitle;布尔关闭 = 假;布尔裸 = 假;而(*参数){字符串 sparam(*param);if (sparam == "-title"){++参数;if (!*param) 中断;参数 = *参数;winTitle.resize(sparam.size());复制(sparam.begin(),sparam.end(),winTitle.begin());}else if (sparam == "-move"){++参数;if (!*param) 中断;参数 =*参数;字符串流 sstr(sparam);字符 sep;sstr>>x >>sep >>y;if (x != numeric_limits::min() && y != numeric_limits::min()){标志 &= ~SWP_NOMOVE;}}else if (sparam == "-resize"){++参数;if (!*param) 中断;参数 = *参数;字符串流 sstr(sparam);字符 sep;sstr>>w>>sep>>H;if (h != numeric_limits::min() && w != numeric_limits::min() ){标志 &= ~SWP_NOSIZE;}}否则如果(sparam ==-top"){z = HWND_TOP;标志 &= ~SWP_NOZORDER;}否则如果(sparam ==-staytop"){z = HWND_TOPMOST;标志 &= ~SWP_NOZORDER;}else if (sparam == "-bottom"){z = HWND_BOTTOM;标志 &= ~SWP_NOZORDER;}else if (sparam == "-hide"){标志 |= SWP_HIDEWINDOW;}else if (sparam == "-close"){关闭 = 真;}else if (sparam == "-naked"){裸 = 真;}++参数;}如果 (winTitle.empty()){返回-1;}HWND win_handle = FindWindow(0, winTitle.c_str());如果(win_handle != 0){如果(关闭){TerminateProcess((HANDLE)GetProcessId(win_handle), 0);返回0;}SetWindowPos( win_handle, z, x, y, w, h, flags );如果(裸体){SetWindowLong(win_handle, GWL_STYLE, GetWindowLong(win_handle, GWL_EXSTYLE) | WS_EX_TOPMOST);ShowWindow(win_handle, SW_SHOW);}}别的{cout<<!!! FindWindow 失败" <<结束;}返回0;}

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!

解决方案

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

这篇关于在 MS Windows(XP) 上使用来自不同进程的窗口句柄进行窗口操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆