gpt4 book ai didi

c++ - 在 C++ 上的 Win32 控制台中绘图?

转载 作者:可可西里 更新时间:2023-11-01 18:41:49 25 4
gpt4 key购买 nike

使用 C++ 在 Win 32 平台的控制台窗口中绘制内容的最佳方法是什么?

我知道您可以使用符号绘制简单的艺术作品,但有没有一种方法可以绘制更复杂的东西,例如圆圈甚至位图?

最佳答案

是的,这是可能的。

使用GetConsoleWindow 获取控制台窗口的HWND然后画进去。

#define _WIN32_WINNT 0x601
#include <windows.h>
#include <stdio.h>

int main() {
// Get window handle to console, and device context
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);

//Here's a 5 pixels wide RED line [from initial 0,0] to 300,300
HPEN pen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
SelectObject(device_context, pen);
LineTo(device_context, 300, 300);

ReleaseDC(console_handle, device_context);

getchar();

return 0;
}

注意:GetConsoleWindow 是在 Windows 2000 中引入的。它在 _WIN32_WINNT 设置为 0x500 或更大时可用。

关于c++ - 在 C++ 上的 Win32 控制台中绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1937163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com