Windows编程对话框背景图片
有没有什么好的教程或方法可以将背景图像添加到 Windows 对话框中?
与此类似的东西:
我的资源文件(稍微修剪一下):
#include <afxres.h>
#include "WindowSettings.h" // for IDC_?, IDD_DLG
// Dialog Box Template for IDD_DLG
//
IDD_DLG DIALOGEX 200, 100, 200, 350
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION DLG_NAME
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
LTEXT "Display Device", IDC_STATIC, 10, 5, 50, 10
COMBOBOX IDC_DIS, 10, 15, 180, 64, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
LTEXT "Resolution", IDC_STATIC, 10, 35, 50, 10
COMBOBOX IDC_RES, 10, 45, 180, 50, CBS_DROPDOWNLIST | WS_DISABLED | WS_VSCROLL | WS_TABSTOP
DEFPUSHBUTTON "Go", IDC_GO, 40, 205, 50, 15, WS_DISABLED
PUSHBUTTON "Cancel", IDCANCEL, 110, 205, 50, 15
END
Is there any good tutorial or way to add a background image into Windows DialogBox?
Something similar to this:
My Recourse file(trimmed a little):
#include <afxres.h>
#include "WindowSettings.h" // for IDC_?, IDD_DLG
// Dialog Box Template for IDD_DLG
//
IDD_DLG DIALOGEX 200, 100, 200, 350
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION DLG_NAME
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
LTEXT "Display Device", IDC_STATIC, 10, 5, 50, 10
COMBOBOX IDC_DIS, 10, 15, 180, 64, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
LTEXT "Resolution", IDC_STATIC, 10, 35, 50, 10
COMBOBOX IDC_RES, 10, 45, 180, 50, CBS_DROPDOWNLIST | WS_DISABLED | WS_VSCROLL | WS_TABSTOP
DEFPUSHBUTTON "Go", IDC_GO, 40, 205, 50, 15, WS_DISABLED
PUSHBUTTON "Cancel", IDCANCEL, 110, 205, 50, 15
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上只有一种实用的方法可以做到这一点。
在资源编辑器中添加静态图像控件是一件痛苦的事情,因为需要调整图像大小以覆盖整个对话框,这使得对话框几乎无法编辑,因为所有单击都将在图像控件上进行。
您可以将图像控件的创建推迟到 WM_INITDIALOG,但反对使用控件的第二个标志是窗口控件在重叠时不会优雅地绘制。
因此,您需要手动执行此操作,在 WM_INITDIALOG 中加载位图,存储 HBITMAP,并将其绘制在 WM_ERASEBKGND 消息上。
如果您另外将 HBITMAP 转换为 HBRUSH,则可以处理 WM_CTLCOLORSTATIC (和其他 WM_CTLCOLORxxx)消息并从消息中返回 HBRUSH,这将在任何非矩形控件下绘制位图,因此,根据您的“之后”屏幕截图,之后的文本将是位图背景上的黑色文本。
最后一个问题是,如果对话框可调整大小,它会闪烁。有一些方法可以尝试解决此问题,但没有一种方法 100% 成功:
将 WS_EX_COMPOSITED 样式添加到对话框中。 Windows 2000 中添加了这种样式,使对话框(实际上是任何窗口)一次性将所有子窗口渲染到后台缓冲区,然后将其渲染到屏幕上,从而消除闪烁。然而,实现 Vista 的 Aero Glass 效果的桌面窗口管理器不支持它,因此从 Windows 6.0 开始,此功能已被破坏。
将 WS_CLIPCHILDREN 样式添加到对话框中。使用此样式使得无法使用选项卡或组框等控件,因为它们依赖于未裁剪的绘制矩形来与其他控件组合。
There is really only one practical way to do this.
Adding a static image control in the resource editor is a pain as the image needs to be resized to cover the entire dialog, making the dialog all but impossible to edit as all clicks will be on the image control.
You can defer creation of the image control to WM_INITDIALOG, but the second mark against using a control is that window controls do not paint elegantly when overlapped.
So, you need to do this manually, load the bitmap in WM_INITDIALOG, store the HBITMAP, and paint it on your WM_ERASEBKGND messages.
If you additionally convert the HBITMAP to an HBRUSH, you can handle the WM_CTLCOLORSTATIC (and other WM_CTLCOLORxxx) messages and return the HBRUSH from the messages, this will paint the bitmap under any non-rectangular controls so, as per your "after" screenshot, the after text would be black text on a bitmap backdrop.
Your final problem is, if the dialog is resizable it will flicker. There are some ways to try and fix this, none are 100% successful:
Add the WS_EX_COMPOSITED style to your dialog. Added in Windows 2000, this style caused dialogs(any window actually) to render all the child windows to a backbuffer in one pass, and then render that onto the screen eliminating flicker. The Desktop Window Manager that implements Vista's Aero Glass effect however doesn't support it, so as of Windows 6.0 this feature was broken.
Add the WS_CLIPCHILDREN style to the dialog box. Using this style makes it impossible to use controls like tabs, or group boxes, as they rely on unclipped painting rects to composite with other controls.
这里有一个:
如何在vc++6.0中的对话框中设置背景图像
处理
WM_ERASEBKGND
消息。Have one here:
how to set background image in a dialog box in vc++6.0
Handle
WM_ERASEBKGND
message.