在 C++ 上的 Win32 控制台中绘图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1937163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing in a Win32 Console on C++?
提问by Kristina Brooks
What is the best way to draw things in the Console Window on the Win 32 platform using C++?
使用 C++ 在 Win 32 平台上的控制台窗口中绘制东西的最佳方法是什么?
I know that you can draw simple art using symbols but is there a way of doing something more complex like circles or even bitmaps?
我知道你可以使用符号绘制简单的艺术,但有没有办法做更复杂的东西,比如圆形甚至位图?
回答by Tapani
#include <windows.h>
#include <iostream.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);
cin.ignore();
return 0;
}
回答by Kristina Brooks
No you can't just do that because Win32 console doesn't support those methods. You can however use GDI to draw on the console window.
不,您不能这样做,因为 Win32 控制台不支持这些方法。但是,您可以使用 GDI 在控制台窗口上绘图。
This is a great example of drawing a bitmap on a console by creating a child window on it: http://www.daniweb.com/code/snippet216431.html
这是通过在控制台上创建子窗口来在控制台上绘制位图的一个很好的示例:http: //www.daniweb.com/code/snippet216431.html
And this tells you how to draw lines and circles:
http://www.daniweb.com/code/snippet216430.html
这告诉你如何画线和圆:http:
//www.daniweb.com/code/snippet216430.html
This isn't really drawing in the console though. This is sort of drawing "over" the console but it still does the trick pretty well.
但这并不是在控制台中真正绘制的。这有点像“超过”控制台,但它仍然可以很好地完成这个技巧。
回答by Koro
It is possible, albeit totally undocumented, to create a console screen buffer that uses an HBITMAP
that is shared between the console window process and the calling process. This is the approach that NTVDM takes to display graphics once a DOS application switches to graphics mode.
尽管完全没有记录,但可以创建一个控制台屏幕缓冲区,该缓冲区使用HBITMAP
在控制台窗口进程和调用进程之间共享的 。一旦 DOS 应用程序切换到图形模式,NTVDM 就会采用这种方法来显示图形。
回答by Sam Overton
Perhaps you are talking about DOS programs, using VGA mode. A quick google search shows up a C tutorial.
回答by Clifford
As Nick Brooks has pointed out, you can use GDI calls in console apps, but the graphics cannot appear in the same window as the text console I/O. This may not matter since you can draw text elements in GDI.
正如 Nick Brooks 所指出的,您可以在控制台应用程序中使用 GDI 调用,但图形不能与文本控制台 I/O 出现在同一窗口中。这可能无关紧要,因为您可以在 GDI 中绘制文本元素。
A simplified interface to GDI calls in console apps is provided by WinBGIm. It is a clone of Borland's DOS BGI API, but with extensions to handle resizable windows, mouse input, and 24bit colour models. Since it is available as source code, it also serves a good demonstration of using GDI in this way.
WinBGIm提供了控制台应用程序中 GDI 调用的简化接口。它是 Borland 的 DOS BGI API 的克隆,但具有处理可调整大小的窗口、鼠标输入和 24 位颜色模型的扩展。由于它作为源代码提供,因此它也很好地演示了以这种方式使用 GDI。
It is possible to either have both a console and the GDI window, or you can suppress the console window by specifying that the application is a GUI app (the -mwindows linker option in GNU toolchain) - note that specifying a GUI app really only suppresses the console, it is only really a GUI app if it has a message loop. Having the console is good for debugging, since it is where stdout and stderr are output to by default.
可以同时拥有控制台和 GDI 窗口,或者您可以通过指定应用程序是 GUI 应用程序(GNU 工具链中的 -mwindows 链接器选项)来抑制控制台窗口 - 请注意,指定 GUI 应用程序实际上只会抑制控制台,如果它有消息循环,它只是一个真正的 GUI 应用程序。拥有控制台有利于调试,因为它是默认输出 stdout 和 stderr 的地方。
回答by Goz
Not without usng ASCII art. Back in the days of DOS it was "fairly" easy to do by redesigning the character bitmaps. It might only be possible in windows by creating your own font, but im really not sure thats possible
并非没有使用 ASCII 艺术。回到 DOS 时代,通过重新设计字符位图“相当”容易做到。它可能只能通过创建自己的字体在 Windows 中实现,但我真的不确定那是可能的