如何在 C++ 中绘制到屏幕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2981621/
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
How to draw to screen in c++?
提问by Kesarion
How would I draw something on the screen ? not the console window but the entire screen, preferably with the console minimised.
我如何在屏幕上画一些东西?不是控制台窗口而是整个屏幕,最好是控制台最小化。
Also, would it show up on a printscreen ? What I want to do is create something like a layer on top of the screen that only me and my aplication are aware of yet still be able to use aplications as usual.
另外,它会显示在打印屏幕上吗?我想要做的是在屏幕顶部创建一个类似层的东西,只有我和我的应用程序知道但仍然可以像往常一样使用应用程序。
Here's an example: Let's say I want 2 yellow squares 5 by 5 pixels in size appearing in the center of the screen on top of all the other applications, unclickable and invisible to a printscreen.
下面是一个示例:假设我想要 2 个大小为 5 x 5 像素的黄色方块出现在所有其他应用程序顶部的屏幕中央,不可点击且对打印屏幕不可见。
[Edit]
[编辑]
I forgot to mention that I'm using Visual Studio 2010 on Windows XP.
我忘了提到我在 Windows XP 上使用 Visual Studio 2010。
回答by OlimilOops
in windows you can use the GetDC-function. just a minimalistic example:
在 Windows 中,您可以使用 GetDC 函数。只是一个简约的例子:
#include <Windows.h>
#include <iostream>
void drawRect(){
HDC screenDC = ::GetDC(0);
::Rectangle(screenDC, 200, 200, 300, 300);
::ReleaseDC(0, screenDC);
}
int main(void){
char c;
std::cin >> c;
if (c == 'd') drawRect();
std::cin >> c;
return 0;
}
but since Windows Vista it is very slow
但由于 Windows Vista 速度很慢
回答by johannes
C++ has no notion of a "screen" and especially none of "graphics". The functionality needed is provided by your operating system. On many systems you will need a "Window" and draw on it. To do this portably, a library like Qt might help. A Windows solution was given by Oops. Maybe you want to use some OpenGL library, or Windows' DirectDraw/Direct3D from DirectX, in case you want to do some 3D stuff with your graphics.
C++ 没有“屏幕”的概念,尤其是没有“图形”的概念。所需的功能由您的操作系统提供。在许多系统上,您将需要一个“窗口”并在其上绘图。为了方便地做到这一点,像 Qt 这样的库可能会有所帮助。Oops 给出了一个 Windows 解决方案。也许你想使用一些 OpenGL 库,或者 Windows 的 DirectDraw/DirectX 的 Direct3D,以防你想用你的图形做一些 3D 的东西。
回答by Pete Kirkham
The (rather nice but not recently updated) graphics library anti-grain geometryhas very simple bindings to display its demos on a variety of windowing systems, you could look at those for examples. But for anything much more involved you're probably talking about operating system specific libraries.
(相当不错但最近没有更新)图形库反纹理几何具有非常简单的绑定,可以在各种窗口系统上显示其演示,您可以查看这些示例。但是对于更多涉及的内容,您可能正在谈论操作系统特定的库。
回答by Puppy
Windows offers GDI/+, WPF, and DirectX (including Direct2D on Vista+).
Windows 提供 GDI/+、WPF 和 DirectX(包括 Vista+ 上的 Direct2D)。