我制作了一个获取控制台手柄的程序,然后将其像动画一样在屏幕上移动,使其在屏幕上“飞行”,然后自动移动。
我的程序获取屏幕分辨率,获取控制台手柄,然后使用该MoveWindow
功能移动它。
我的问题是窗口根本不会移动,并且我也没有任何错误。
我的代码:
#include <iostream>
#include <Windows.h>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
POINT current_position;
while (true) {
int offset = rand() % 2;
int x_direction = rand() % 2 == 1 ? 1 : -1;
int y_direction = rand() % 2 == 1 ? 1 : -1;
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
HWND hwndConsole = GetWindow(GetConsoleWindow(), (UINT)¤t_position);
MoveWindow(hwndConsole, current_position.x + (offset * x_direction), current_position.y + (offset * y_direction), width, height, TRUE);
Sleep(10);
}
return 0;
}
编辑:
在您写给我的评论的帮助下,我更改了代码。
但是现在控制台的大小正在改变,并且控制台根本没有移动。
#include <iostream>
#include <Windows.h>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
POINT current_position{};
while (true) {
HWND hwndConsole = GetConsoleWindow();
int offset = rand() % 2;
int x_direction = rand() % 2 == 1 ? 1 : -1;
int y_direction = rand() % 2 == 1 ? 1 : -1;
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
BOOL hwndMove = MoveWindow(hwndConsole, current_position.x + (offset * x_direction), current_position.y + (offset * y_direction), width, height, TRUE);
if (hwndMove == FALSE) {
cout << "Failed! & Error Code: " << GetLastError();
}
Sleep(10);
}
return 0;
}
第二编辑:
的width
和height
是控制台的大小,所以我删除它,并且改变的参数为100,100。
这是您要执行的操作的简化版本,可将窗口向下和向右滑动。我删除了随机性,因为很难从示例中看出意图。我还对程序终止前窗口移动了多长时间进行了限制。
使用此作为您要完成的目标的起点。
#include <Windows.h>
int main()
{
HWND hwndConsole = GetConsoleWindow();
for (int i = 0; i < 200; i++)
{
RECT position;
GetWindowRect(hwndConsole, &position);
MoveWindow(hwndConsole, position.left + 3, position.top + 3, 300, 300, TRUE);
Sleep(10);
}
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句