移动Windows窗口的小程序

下载请到:

https://github.com/danteliujie/smallTools/blob/master/MoveWindow.exe

基于FindWindow和SetWindowPos实现, 其中的flag设置请参考: 

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos

代码如下:

#include "Windows.h"
#include 
#include  
#include 

int Hex2Int(std::string s)
{
	return std::stoul(s, nullptr, 16);
}
void printBasicUsing() {
	printf("MoveWindow.exe tag [x y] [w h] [flag]\r\n");
	printf("Flags:\r\n");
	printf("\tSWP_ASYNCWINDOWPOS  0x4000\r\n");
	printf("\tSWP_DEFERERASE      0x2000\r\n");
	printf("\tSWP_NOSENDCHANGING  0x0400\r\n");
	printf("\tSWP_NOOWNERZORDER   0x0200\r\n");
	printf("\tSWP_NOCOPYBITS      0x0100\r\n");
	printf("\tSWP_HIDEWINDOW      0x0080\r\n");
	printf("\tSWP_SHOWWINDOW      0x0040\r\n");
	printf("\tSWP_FRAMECHANGED    0x0020\r\n");
	printf("\tSWP_NOACTIVATE      0x0010\r\n");
	printf("\tSWP_NOREDRAW        0x0008\r\n");
	printf("\tSWP_NOZORDER        0x0004\r\n");
	printf("\tSWP_NOMOVE          0x0002\r\n");
	printf("\tSWP_NOSIZE          0x0001\r\n");
	printf("More Info:\r\n");
    printf("\thttps://blog.csdn.net/danteLiujie/article/details/103550505\r\n");
}
int main(int argc, char* argv[])
{
	if(argc <= 3){
		printBasicUsing();
		return -1;
	}
	HWND hwndMyWindow = NULL;
	hwndMyWindow = FindWindowExA(NULL, NULL, argv[1], NULL);
	if (hwndMyWindow == NULL) {
		hwndMyWindow = FindWindowEx(NULL, NULL, MAKEINTATOM(Hex2Int(argv[1])), NULL);
	}
	printf("FindWindowEx return 0x%08x\r\n", hwndMyWindow);
	if (hwndMyWindow != NULL) {
		int x = 0;
		int y = 0;
		int w = 300;
		int h = 300;
		int flag = SWP_ASYNCWINDOWPOS | SWP_FRAMECHANGED | SWP_SHOWWINDOW;
		if (argc > 3) {
			x = std::stoul(argv[2]);
			y = std::stoul(argv[3]);
		}
		if (argc > 5) {
			w = std::stoul(argv[4]);
			h = std::stoul(argv[5]);
		}
		if (argc > 6) {
			flag = std::stoul(argv[6]);
		}
		SetWindowPos(hwndMyWindow, HWND_TOP, 
			x, y, w, h, flag);
	}
    return 0;
}

你可能感兴趣的:(杂项)