突然发现,会一点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.