有没有办法像动画一样移动控制台窗口?
Is there a way I can move my console window like animation?
我制作了一个程序来获取控制台句柄,然后像动画一样在屏幕上移动它,让它在屏幕上"飞",自动移动它。
我的程序采用屏幕的分辨率,获取控制台句柄,然后使用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;
}
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 按值 C++ 返回时进行双倍移动
- 有没有办法像动画一样移动控制台窗口?
- 在 c# 中将 int 添加到字符以将其 ascii 值向上移动(就像在 c++ 中一样)
- 如何使移动检测到的手和指尖像鼠标一样与Qt GUI交互