简体   繁体   English

实现 C++ Win32 启动画面的最快方法

[英]Quickest way to implement a C++ Win32 Splash Screen

What's a simple way to implement a c++ Win32 program to...实现 C++ Win32 程序的简单方法是什么...
- display an 800x600x24 uncompressed bitmap image - 显示一个 800x600x24 的未压缩位图图像
- in a window without borders (the only thing visible is the image) - 在没有边框的窗口中(唯一可见的是图像)
- that closes after ten seconds - 十秒后关闭
- and doesn't use MFC - 并且不使用 MFC

If you're targeting modern versions of Windows (Windows 2000) and above, you can use the UpdateLayeredWindow function to display any bitmap (including one with an alpha channel, if so desired).如果您的目标是现代版本的 Windows (Windows 2000) 及更高版本,您可以使用UpdateLayeredWindow函数来显示任何位图(包括带有 alpha 通道的位图,如果需要的话)。

I blogged a four-part series on how to write a C++ Win32 app that does this.在博客上写了一个关于如何编写 C++ Win32 应用程序来执行此操作的四部分系列文章。 If you need to wait for exactly ten seconds to close the splash screen (instead of until the main window is ready), you would need to use Dan Cristoloveanu's suggested technique of a timer that calls DestroyWindow.如果您需要等待十秒钟来关闭启动画面(而不是直到主窗口准备好),您将需要使用 Dan Cristoloveanu 建议的调用 DestroyWindow 的计时器技术。

Register a class for the splash window and create a window using these styles:为启动窗口注册一个类并使用这些样式创建一个窗口:

  • WS _ POPUPWINDOW: will make sure your window has no caption/sysmenu WS _ POPUPWINDOW:将确保您的窗口没有标题/系统菜单
  • WS _ EX _ TOPMOST: will keep the splash screen on top of everything. WS _ EX _ TOPMOST:将启动画面保持在一切之上。 Note that this is a bit intrusive.请注意,这有点侵入性。 It might be better to just make the splash window a child of your main window.最好让启动窗口成为主窗口的子窗口。 You may have to manipulate the z-order, though, to keep any other popup windows (if you create any) below the splash screen.但是,您可能必须操纵 z 顺序,以将任何其他弹出窗口(如果您创建)保留在启动屏幕下方。

Use CreateDIBSection to load the bitmap.使用 CreateDIBSection 加载位图。 It should be easy, since BMP files are essentially dumps of DIB structures.这应该很容易,因为 BMP 文件本质上是 DIB 结构的转储。 Or do what Ken said and use LoadImage.或者按照 Ken 说的去做并使用 LoadImage。

Handle the WM _ PAINT or WM _ ERASEBKGND message to draw the bitmap on the window.处理 WM _ PAINT 或 WM _ ERASEBKGND 消息以在窗口上绘制位图。

On WM _ CREATE set a timer of 10 seconds and when Windows sends the WM _ TIMER message, have the window destroy itself.在 WM _ CREATE 上设置一个 10 秒的计时器,当 Windows 发送 WM _ TIMER 消息时,让窗口自行销毁。

The key point here is to use layered window .这里的关键是使用分层窗口

You can start with a win32 wizard generated project and change CreateWindow call to CreateWindowEx and set WS_EX_LAYERED as extended window style and combination of WS_POPUP and WS_SYSMENU as window style.您可以从 win32 向导生成的项目开始,将 CreateWindow 调用更改为CreateWindowEx ,并将 WS_EX_LAYERED 设置为扩展窗口样式,并将 WS_POPUP 和 WS_SYSMENU 的组合设置为窗口样式。 When you do that launch your application it will be invisible.当您这样做时,启动您的应用程序它将是不可见的。 Then you should use UpdateLayeredWindow to paint your image.然后你应该使用UpdateLayeredWindow来绘制你的图像。 You may also need AlphaBlend function if you want use PNG image with alpha layer.如果您想使用带有 alpha 层的 PNG 图像,您可能还需要AlphaBlend函数。

Hope this helps!希望这可以帮助!

  • Use LoadImage to load the bitmap使用 LoadImage 加载位图
  • Use CreateWindowEx to create the window.使用 CreateWindowEx 创建窗口。
  • In the window proc capture the WM_PAINT.在窗口 proc 中捕获 WM_PAINT。 Use BitBlt to paint the bitmap.使用 BitBlt 绘制位图。

You can:你可以:

  • Create a dialog in your resource file在您的资源文件中创建一个对话框
  • Have it contain a Picture control让它包含图片控件
  • Set the picture control type to Bitmap设置图片控件类型为Bitmap
  • Create/import your bitmap in the resource file and set that bitmap ID to the picture control in your dialog在资源文件中创建/导入位图并将该位图 ID 设置为对话框中的图片控件
  • Create the window by using CreateDialogParam使用 CreateDialogParam 创建窗口
  • Handle the WM_INITDIALOG in order to set a timer for 10 seconds (use SetTimer)处理 WM_INITDIALOG 以将计时器设置为 10 秒(使用 SetTimer)
  • Handle WM_TIMER to catch your timer event and to destroy the window (use DestroyWindow)处理 WM_TIMER 以捕获您的计时器事件并销毁窗口(使用 DestroyWindow)

It's a Win32 api FAQ这是一个 Win32 api 常见问题解答

See professional Win32api forum news://194.177.96.26/comp.os.ms-windows.programmer.win32 where it has been answered hundreds of times for 20 years..见专业的Win32api论坛新闻://194.177.96.26/comp.os.ms-windows.programmer.win32 20年来已回答数百次..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM