C++ 中的屏幕保护程序图像褪色

发布于 2024-08-04 12:13:37 字数 258 浏览 8 评论 0原文

如何用 C++ 制作一个屏幕保护程序,在屏幕上的随机位置淡入和淡出图像,并在淡出时指定指定的时间延迟?

多显示器支持会很棒。

如果您有有效的代码或知道我可以在哪里获得它,那就太好了。否则请为我指明正确的方向。我正在寻找一种平滑且不滞后或闪烁的淡入淡出方法。该屏幕保护程序适用于 Windows XP。

我不懂 C++,但我懂 AS3、Javascript 和 PHP。所以我希望将其中一些知识与 C++ 联系起来。

我应该用什么来编译它?

How can I make a screensaver in C++ that fades an image in and out at random places on the screen with a specified time delay on the fade out?

Multimonitor support would be awesome.

If you have a working code or know where I can get it, it would be great. Otherwise point me in the right direction. I'm looking for a method that has a smooth and not laggy og flickery fade. The screensaver is for Windows XP.

I dont know C++, but I do know AS3, Javascript, and PHP. So I was hoping to relate some of that knowledge to C++.

What should I use to compile it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在梵高的星空下 2024-08-11 12:13:37

首先,如果您开始使用 C++,请不要从特定于 Windows 的编译器(如 Visual C++)开始。获取一个不错的跨平台 IDE,它带有 eclipse 或 code::blocks 等编译器。与任何项目一样,您需要将其分成可以分阶段完成的较小任务。听起来你有几个障碍。

  1. 缺乏 C++ 知识(我们都来过一次)
  2. 缺乏图像知识(非常常见的烦恼)
  3. 缺乏经验(我们会努力解决这个问题)

不要让别人阻止你。你可以做到这一点,而且可能比你想象的要快。一定要读一两本关于 C++ 的书,你可以在不了解太多的情况下完成一个项目,但你会经常感到沮丧。让我们将您的项目分解为一组小目标。当您完成每个目标时,您对 C++ 的信心就会增强。

  1. 图像混合
  2. 具有多显示器支持的 Windows 屏幕保护程序
  3. 屏幕画布(directx、opengl、位图?)
  4. 计时器

首先,让我们看看图像混合的问题。我假设您希望将相关图像“淡入”到您激活的任何窗口背景中。如果您想要纯色背景,则可以通过在画布刷新之间更改相关图像的 Alpha 透明度来实现。本质上,您需要根据刷新计时器对两个图像中每个像素的颜色值进行平均。更直接地说,找到任何结果像素的红色、绿色和蓝色像素元素 (P3)

N = 每个间隔的计时器滴答声(秒/毫秒/等)
T = 此时间间隔内发生的价格变动
P1r = 图像 1 中的红色像素元素
P2r = 图像 2 中的红色像素元素
P3r = 混合图像的合成红色像素元素
P1g = 图像 1 中的绿色像素元素
P2g = 图像 2 中的绿色像素元素
P3g = 混合图像的合成绿色像素元素
P1b = 图像 1 中的蓝色像素元素
P2b = 图像 2 中的蓝色像素元素
P3b = 混合图像的蓝色像素元素

P3r = ((T/N * P1r) + ((N-T)/N * P2r))/2
P3g = ((T/N * P1g) + ((N-T)/N * P2g))/2
P3b = ((T/N * P1b) + ((N-T)/N * P2b))/2

现在让我们看看 Windows 屏幕保护程序和多显示器支持的问题。这实际上是两个不同的问题。 Windows 屏幕保护程序实际上只是具有特定回调函数的常规 .exe 编译文件。您可以在网上找到许多有关设置屏幕保护程序功能的教程。当您设置屏幕画布时,多显示器支持实际上是一个问题,我们将在接下来讨论。

当我提到屏幕画布时,我指的是程序将输出可视数据的区域。所有图像渲染应用程序或教程基本上都会引用相同的概念。如果您觉得这特别有趣,请考虑计算机视觉方面的研究生课程或学习课程。相信我你不会后悔的。无论如何,考虑到应用程序的基本性质,我建议不要使用 openGL 或 DirectX,因为它们都有自己的应用程序特定知识层,您需要在它们有用之前获取它们。另一方面,如果您想要内置的 3D 细节,例如双缓冲和 GPU 卸载,我会选择 openGL(与平台无关)。许多有趣的图像库也以赠品的形式出现。

至于多显示器支持,这是一个棘手但并不复杂的问题。您基本上只需设置画布边界(屏幕边界)以匹配多个显示器的几何形状。具有相同分辨率的多个显示器不应该成为问题,但显示器分辨率不匹配可能会变得棘手(可能画布区域未显示在屏幕上等)。针对这些问题有众所周知的算法和解决方法,但这可能超出了范围 使用

最后,关于所需的计时器,这应该是最简单的部分,Windows 和 Linux 以它们自己奇怪的方式处理时间(为什么 MS 不实现 STRPTIME),所以如果您对可移植性感兴趣,我会 第 3 方库。否则,只需使用 Windows settimer() 基本功能并在回调函数中渲染图像:

嘿,如果您仍在阅读,您可以对此程序进行许多有趣的算法改进。例如,当你的计时器每秒关闭一个设定的量子时,你可以缓存前几个混合图像并节省一些处理时间(我们的眼睛不太擅长注意到颜色渐变变化之间的区别)。如果您使用的是 openGL,您可以将混合图像集缓存为显示列表(必须使用所有显卡内存,对吗?)。有趣的事情,祝你好运!

First off if you're starting out in C++, don't start with a windows specific compiler like visual c++. Grab a nice cross-platform IDE that comes with a compiler like eclipse or code::blocks. Like any project, you are going to want to split it up into smaller tasks you can complete in stages. Sounds like you have a couple of hurdles.

  1. Lack of C++ Knowledge (we were all here once)
  2. Lack of knowledge about images (very common affliction)
  3. Lack of experience (we'll work on this)

DO NOT let others discourage you. You CAN do this, and probably faster than you think possible. DO read a book or two about C++, you can get by for one project without knowing much but you WILL get frustrated often. Let's break up your project into a set of small goals. As you complete each goal your confidence in C++ will rise.

  1. Image blending
  2. Windows Screen Saver w/ multi-monitor support
  3. Screen Canvas (directx, opengl, bitmaps?)
  4. Timers

First, let's look at the problem of image blending. I assume that you'll want to "fade" the image in question into whatever windows background you have active. If you're going to have a solid-color background, you can just do it by changing the alpha transparency of the image in question between canvas refreshes. Essentially you'll want to average the color values of each pixel in the two images based on your refresh timer. In more direct terms to find the red, green, and blue pixel elements for any resultant pixel (P3)

N = timer ticks per interval (seconds/milliseconds/etc)
T = ticks that have occurred this interval
P1r = red pixel element from image 1
P2r = red pixel element from image 2
P3r = resultant red pixel element for blended image
P1g = green pixel element from image 1
P2g = green pixel element from image 2
P3g = resultant green pixel element for blended image
P1b = blue pixel element from image 1
P2b = blue pixel element from image 2
P3b = resultant blue pixel element for blended image

P3r = ((T/N * P1r) + ((N-T)/N * P2r))/2
P3g = ((T/N * P1g) + ((N-T)/N * P2g))/2
P3b = ((T/N * P1b) + ((N-T)/N * P2b))/2

Now let's look at the problem of windows screen savers and multi-monitor support. These are really two separate problems. Windows screensavers are really only regular .exe compiled files with a certain callback function. You can find many tutorials on setting up your screensaver functions on the net. Multi-monitor support will actually be a concern when you set up your Screen Canvas, which we'll discuss next.

When I refer to the Screen Canvas, I am referring to the area upon which your program will output visual data. All image rendering apps or tutorials will basically refer to this same concept. If you find this particularly interesting, please consider a graduate program or learning course in Computer Vision. Trust me you will not regret it. Anyway, considering the basic nature of your app I would reccommend against using openGL or DirectX, just because each have their own layer of app-specific knowledge you'll need to acquire before they are useful. On the other hand if you want built-in 3d niceties like double buffering and gpu offloading, I'd go with openGL (more platform agnostic). Lots of fun image libraries come as gimmes as well.

As for multi monitor support, this is a tricky but not complicated issue. You're basically just going to set your canvas bounds (screen boundary) to match the geometry of your multiple monitors. Shouldn't be an issue with multiple monitors of the same resolution, may get tricky with mismatched monitor resolutions (might have canvas areas not displayed on screen etc. There are well known algorithms and workarounds for these issues, but perhaps this is beyond the scope of this question.

Finally as to the timers required, this should be the easiest part. Windows and Linux handle time in their own strange ways (WHY doesn't MS implement STRPTIME), so If you are interested in portability I'd use a 3rd party library. Otherwise just use the windows settimer() basic functionality and have your image rendered in the callback function.

Advanced Topic: Hey if you're still reading there are a number of interesting algorithmic improvements you can make to this program. For instance, with your timer going off a set quanta each second, you could cache the first few blended images and save some processing time (our eyes are not terribly good at noticing differentiating between changing color gradients). If you are using openGL, you could cache sets of blended images as display lists (gotta use all that graphics card memory for something right?). Fun stuff, good luck!

放手` 2024-08-11 12:13:37

“我不懂 C++,但我了解 AS3、Javascript 和 PHP。所以我希望将其中一些知识与 C++ 联系起来。”

哦,孩子,我想你会感到惊讶。要学习 C++:

  • 至少购买一两本非常好的书籍(请参阅此处)。不要买不太好的书。他们会教给你一些习惯,你必须在以后忘记这些习惯才能取得进一步的进步。
  • 预计必须进行大量的实践代码编写,并在其间进行大量的阅读。在最初的几周内,编译器会向你吐出大量难以理解的错误消息,你的代码会不断崩溃,经验丰富的 C++ 程序员看到你的代码会厌恶地举手。这总是都是你的错。
  • 计划大量的时间来学习。我的意思是很多。无论你投入多少时间,从“傻瓜”升级到“新手”至少需要几个月的时间。
  • 想象一下,为了成为一名真正的专业人士,必须苦苦钻研几年,不断地阅读书籍和文章,投入大量时间参加新闻组和论坛,向他人学习,用头撞办公桌周围的每一面墙。
  • 即使在以 C++ 编程为生十年之后,也要准备好每周至少学习一次以前不知道的东西。
  • 暂时想象一下我可能不会夸大这一点。

编辑澄清:我对 Hunter Davis 和 Elemental 的答案都投了赞成票,因为他们非常好,几乎切中要点,总的来说,尽管我咆哮,但我还是支持鼓励那里。事实上,我毫不怀疑许多人在给出一个框架示例时能够用 C/C++ 来组合某些东西,即使他们实际上对 C++ 不太了解。继续尝试,您很有可能在合理的时间内想出一个屏幕保护程序。但。能够“用 C/C++ 一起破解一些东西”与“学会 C++”相去甚远。 (我的意思是非常。)

C++ 是一个令人难以置信的复杂、卑鄙和顽固的野兽。我还认为它几乎令人惊叹的美丽,但因为它可能反映了从很高的山上看到的景色:很难到达能够欣赏美丽的程度。

"I dont know C++, but I do know AS3, Javascript, and PHP. So I was hoping to relate some of that knowledge to C++."

Oh boy, I suppose you're going to be surprised. To learn C++:

  • Buy at least one or two very good books (see here). Do not buy books that aren't very good. They'll teach you habits that you would have to unlearn later in order to make further progress.
  • Expect having to do plenty of hands-on code writing with a lot of reading in between. In the first few weeks, the compiler will spit legions of incomprehensible error messages at you, your code will keep crashing, and seasoned C++ programmers looking at your code will throw up their hands in disgust. And it's always your fault.
  • Plan a lot of time to learn. And I mean a real lot. No matter how much time you devote, it will take at least a couple of months until you upgrade from "dummy" to "novice".
  • Imagine having to hammer at it for a couple of years in order to become a real professional, constantly reading books and articles, devoting plenty of time to newsgroups and discussion forums, learning from others, banging your head against every wall surrounding your desk.
  • Be prepared to learn something you haven't known before at least once every week even after a decade of programming in C++ for a living.
  • Just for a moment, imagine I might not overstate this.

Edit for clarification: I have up-voted both Hunter Davis' and Elemental's answers, because they're very good, pretty much to the point, and in general the encouragement is supported by me despite my rant up there. In fact, I do not doubt that many are able to hack something together in C/C++ when given a skeleton example even if actually they don't know much of C++. Go ahead and try, there's a good chance you'll come up with a screen saver within reasonable time. But. Being able to "hack something together in C/C++" is far from "having learned C++". (And I mean very far.)

C++ is an incredible complex, mean, and stubborn beast. I also consider it almost breathtakingly beautiful, but in that it probably mirrors the view from a very high mountain: It's pretty hard to get to the point where you're able to appreciate the beauty.

笨死的猪 2024-08-11 12:13:37

C++ 是一种复杂的语言,sbi 所指出的一切可能都是正确的(当然,经过 10 年的商业 C++ 编程,仍然有一些东西需要学习),但我认为,如果您对另一种语言有信心,那么只需要几天的时间即可:
a) 安装这里提到的编译器之一(我建议使用 VC 作为 Windows 编程的快速入门工具)
b) 找到一些使用 Windows GDI 执行简单操作的屏幕保护程序示例代码(MS 文档中有关屏幕保护程序的一些代码)
c) 修改此代码以执行您想要的操作。

就您的有限要求而言,我认为您会发现窗口 GDI++ 库有足够的能力来顺利完成您需要的 alpha 淡入淡出。

C++ is a complex language and all that sbi indicates is probably true (certainly after 10 years of commercial C++ programming there is still some to learn) BUT I think that if you are confident in another language it should only take a couple of days to:
a) Install one of the compilers mentioned here (I would suggest VC as a quick in to windows programming)
b) Find some sample code of a screen saver that does something simple using the windows GDI (there is some code within the MS documentation on screen savers)
c) Modify this code to do what you want.

In terms of your limited requirement I think you will find that the window GDI++ libraries have sufficient power to do the alpha fades you require smoothly.

野の 2024-08-11 12:13:37

我会看看 microsoft c++express。它是免费的并且功能非常强大。您可能需要破解它或获取旧版本才能生成非托管可执行文件。

有很多教程可用于其余的部分

I'd look at microsoft c++ express. It's free and pretty capable. You may need to hack it, or get an older version, to produce unmanaged executables.

There are a lot of tutorials available for the rest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文