突然发现,会一点Windows API,似乎可以写一些莫名其妙的鬼东西,比如xx小病毒啥的。

接上一篇。

再说一遍需求:写一个程序A,去启动一个指定的程序B,设置它的窗口大小并把它放到指定位置。

Review:Windows程序设计(4):根据PID,获取句柄Handle

既然得到了handle,直接调用SetWindowPos这个函数,就好了。

#include <windows.h>
#include <stdio.h>int main (int argc,char* argv[])
{char szCommandLine[]="notepad.exe";STARTUPINFO si={sizeof(si)};PROCESS_INFORMATION pi;si.dwY = 0;si.dwX = 0;si.dwXSize = 200;si.dwYSize = 100;si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USEPOSITION | STARTF_USESIZE; //制定wShowWindow成员si.wShowWindow=TRUE; //为真,显示进程的主窗口BOOL bRet=::CreateProcess(NULL,//不在此指定可执行文件的文件名szCommandLine, //命令行参数NULL,//默认进程的安全性NULL,//默认线程的安全性FALSE,//指定当前进程内的句柄不可以被子进程继承CREATE_NEW_CONSOLE,//为新进程创建一个新的控制台窗口NULL,//使用本进程的环境变量NULL,//使用本进程的驱动器和目录&si,&pi);if(bRet){                 WaitForInputIdle(pi.hProcess,INFINITE);Sleep(1000);unsigned long find_pid = 0;HWND hwnd = GetForegroundWindow();while (hwnd)  {   GetWindowThreadProcessId(hwnd, &find_pid);if (find_pid != 0) {if (find_pid == pi.dwProcessId) {printf("Done\n");printf("0x%x, %d\t",hwnd, pi.dwProcessId);SetWindowPos(hwnd, NULL, 40, 40, 200, 200, SWP_SHOWWINDOW);break;}}hwnd = GetNextWindow(hwnd, GW_HWNDPREV);}//既然我们不使用两个句柄,最好是立刻将他们关闭::CloseHandle(pi.hThread);::CloseHandle(pi.hProcess);printf("新的进程的进程ID号:%d\n",pi.dwProcessId);printf("新进程的主线程ID号:%d\n",pi.dwThreadId);}return 0;
}

对于那种希望窗口在后台,也可以被设置大小和位置的需求,可能可以试一下SwitchToThisWindow这个函数!先把窗口从最小化的状态弹起来,然后再设置就好了。

如果是C++要用这个函数的话,好像要手动加载动态链接库,才可以用。好像是要load一个什么 user32.dll。

ShowWindow(hwnd, SW_RESTORE ) // 窗口最大化后的回复状态(相当于非最大化、还原按钮)

SetWindowPos

The SetWindowPos function changes the size, position, and Z order of a child, pop-up, or top-level window. Child, pop-up, and top-level windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.

BOOL SetWindowPos(HWND hWnd,             // handle to window
  HWND hWndInsertAfter,  // placement-order handle
  int X,                 // horizontal position
  int Y,                 // vertical position
  int cx,                // width
  int cy,                // height
  UINT uFlags            // window-positioning flags
);

Parameters

hWnd
Handle to the window.
hWndInsertAfter
Handle to the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values:

Value Meaning
HWND_BOTTOM Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
HWND_NOTOPMOST Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
HWND_TOP Places the window at the top of the Z order.
HWND_TOPMOST Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.

For more information about how this parameter is used, see the following Remarks section.

X
Specifies the new position of the left side of the window, in client coordinates.
Y
Specifies the new position of the top of the window, in client coordinates.
cx
Specifies the new width of the window, in pixels.
cy
Specifies the new height of the window, in pixels.
uFlags
Specifies the window sizing and positioning flags. This parameter can be a combination of the following values:

Value Meaning
SWP_ASYNCWINDOWPOS If the calling thread does not own the window, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME Draws a frame (defined in the window's class description) around the window.
SWP_FRAMECHANGED Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
SWP_HIDEWINDOW Hides the window.
SWP_NOACTIVATE Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of thehWndInsertAfter parameter).
SWP_NOCOPYBITS Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE Retains the current position (ignores the X and Y parameters).
SWP_NOOWNERZORDER Does not change the owner window's position in the Z order.
SWP_NOREDRAW Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
SWP_NOREPOSITION Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE Retains the current size (ignores the cx and cy parameters).
SWP_NOZORDER Retains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOW Displays the window.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks

If the SWP_SHOWWINDOW or SWP_HIDEWINDOW flag is set, the window cannot be moved or sized.

Windows程序设计(5):移动窗口、调整窗口大小相关推荐

  1. 中年人学C语言Windows程序设计,9 窗口绘图:直线的画法

    标题中年人学C语言Windows程序设计,9 窗口绘画:直线 MoveToEx函数 函数功能: MoveToEx 函数将当前绘图位置移动到某个具体的点,同时也可获得之前位置的坐标. API 函数原型: ...

  2. Windows程序设计之创建窗口示例

    Windows程序设计书本上的创建窗口示例,手敲代码,拿出了show,记录一下而已~~~ 一.代码如下: #include <tchar.h> #include <windows.h ...

  3. 中年人学C语言Windows程序设计,3 窗口中文本的输出TextOut

    中年人学C语言Windows程序设计,3 文本的输出TextOut 函数功能 TextOut 函数使用当前选择的字体.背景颜色和文本颜色,将一个字符串绘制于窗口的指定位置. API 函数原型: 注释: ...

  4. windows调整窗口大小_175 Windows 7调整,提示和操作方法文章

    windows调整窗口大小 Windows 7 is being officially released on October 22nd, which also happens to be today ...

  5. C语言Windows程序设计 - 【第一个属于自己的窗口】!

    ☺创建属于自己的窗口 ================== ۞创建窗口前的准备 在创建窗口前我们先来熟悉几个名词, 这些名词现在可以暂时不去透彻的进行理解, 只需要印象中知道有这么回事就行. 1> ...

  6. c语言lnk1168无法打开exe,跪求大佬帮帮看看LINK : fatal error LNK1168: 无法打开 F:\windows程序设计\窗口\......

    马上注册,结交更多好友,享用更多功能^_^ 您需要 登录 才可以下载或查看,没有帐号?立即注册 x 运行的结果显示是这样: 1>------ 已启动生成: 项目: 窗口, 配置: Debug W ...

  7. 《Windows 程序设计(第3版)》——6.7 【实例】窗口查看器

    本节书摘来自异步社区<Windows 程序设计(第3版)>一书中的第6章,第6.7节,作者:王艳平 , 张铮著,更多章节内容可以访问云栖社区"异步社区"公众号查看 6. ...

  8. Windows程序设计设计第一个窗口

    窗口调用的函数的简短解释: 来自小甲鱼的窗口模板: /* -------------------------------------------------------------------MyWi ...

  9. PyQT5 禁用窗口最大化,禁止调整窗口大小

    1.禁用窗口最大化,禁止调整窗口大小 self.setFixedSize(self.width(), self.height()); 2.禁止窗口最大化,禁止窗口关闭按钮 self.setWindow ...

  10. MFC Windows 程序设计[六十]之窗口分屏(附源码)

    MFC Windows 程序设计[六十]之窗口分屏 程序之美 前言 主体 运行效果 核心代码 逻辑分析 结束语 程序之美 前言 MFC是微软公司提供的一个类库(class libraries),以C+ ...

最新文章

  1. Java 过一下基础
  2. js把文字中的空格替换为横线
  3. MIT人工智能独立设系!拆分EECS为EE、CS、AI+决策三部分,直接归学院管理
  4. [UWP]本地化入门
  5. android webview卡顿检测_Android webview隐藏后跳转新页面input输入卡顿与白屏渲染慢的问题说明及修复方案...
  6. qt弹框输入密码_Android仿支付宝密码输入框
  7. 时间是把杀猪刀...分享我10年的水深火热:软件测试员!
  8. c#中,如何获取日期型字段里的年、月、日?
  9. be服务器未正常运行5.4.15,RHEL5.4 DNS服务器配置详解(一)
  10. org.apache.hadoop.hbase.mapreduce.Driver 导入数据到HBASE table
  11. 【时间序列分析】02.线性平稳序列
  12. ajax data=text,jQuery ajax dataType值为text json探索分享
  13. uni-app项目Android离线打包UrlSchemes设置
  14. Android实现开屏广告(广点通SDK)
  15. 台式计算机怎么加一个硬盘,台式电脑硬盘怎么多安装一个?电脑安装多加一块硬盘的方法...
  16. 再高贵的打工人都得在体检报告前低下高贵的头颅
  17. 【华为云技术分享】如何快速实现鲲鹏弹性云服务器Node.js部署和高可用性?
  18. 关于SQL Server numeric数据类型介绍
  19. 【区块链开发指南】区块链基础之区块和交易
  20. uniapp发布h5和小程序样式或者一些其他问题

热门文章

  1. 天涯明月刀开发_天涯明月刀手游公测上线,斗鱼暗地操作,打造第二个PDD
  2. linux断点续传程序,Linux中实现断点续传的原理
  3. java -cp 配置文件目录_java – 使用可执行JAR时指定Log4j2配置文件
  4. NO.3 寻找数组主要元素
  5. Java NIO学习与记录(七): Reactor单线程模型的实现
  6. Cordova--打包问题
  7. PHP学习笔记:利用gd库给图片打图片水印
  8. NYOJ题目1045看美女
  9. lzw编码过程详解_【手打】LZW编码的C/C++实现
  10. Matlab 图像像素点在RGB空间的显示