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

突然发现,会一点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:
ValueMeaning
HWND_BOTTOMPlaces 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_NOTOPMOSTPlaces 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_TOPPlaces the window at the top of the Z order.
HWND_TOPMOSTPlaces 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:
ValueMeaning
SWP_ASYNCWINDOWPOSIf 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_DEFERERASEPrevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAMEDraws a frame (defined in the window's class description) around the window.
SWP_FRAMECHANGEDSends 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_HIDEWINDOWHides the window.
SWP_NOACTIVATEDoes 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_NOCOPYBITSDiscards 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_NOMOVERetains the current position (ignores the X and Y parameters).
SWP_NOOWNERZORDERDoes not change the owner window's position in the Z order.
SWP_NOREDRAWDoes 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_NOREPOSITIONSame as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGINGPrevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZERetains the current size (ignores the cx and cy parameters).
SWP_NOZORDERRetains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOWDisplays 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. 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qcyfred

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值