65.9K
CodeProject 正在变化。 阅读更多。
Home

为任何窗口添加背景图像

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (15投票s)

1999年12月6日

viewsIcon

277425

downloadIcon

3186

一个 ATL 类, 允许您为任何窗口添加背景图像。

  • 下载源代码文件 - 2 Kb
  • 有时您可能需要更改对话框或控件的背景。 如果您使用 CPictureWindow 类,这会很容易。 此类使用 ATL 类之一,CWindowImpl。 这并不意味着 CPictureWindow 类不能在 MFC 应用程序中使用。 您需要做的就是在您的 stdafx.h 文件中插入两行新代码。

    #include < atlbase.h > ;
    extern CComModule _Module;
    

    该类通过子类化另一个窗口来使用。 因此,如果您想更改对话框或控件的背景,您所需要做的就是在对象中添加一个新的 CPictureWindow 成员,然后使用您的新成员对该对象进行子类化。

    例如,如果您想在对话框中设置背景图像,您将向对话框添加一个成员变量 m_PictureWindow,并在 OnInitDialog 调用中调用函数 m_PictureWindow.SubclassWindow(m_hWnd );,其中 m_hWnd 是对话框的窗口句柄。

    如果您想处理对话框中的 EraseBackGround 消息,您必须调用

    m_PictureWindow.nMessageHandler = CPictureWindow::BackGroundPaint. 
    
    完成此操作后,您需要调用 CPictureWindow 类的 Load 函数。 这样,您的对话框背景将显示图像。

    #include "PictureWindow.h"
    BOOL XXXX::OnInitDialog()
    {
    	...
    	m_PictureWindow.SubclassWindow( m_hWnd );
    	m_PictureWindow.m_nMessageHandler = CPictureWindow::BackGroundPaint;
    	m_PictureWindow.Load("image.bmp");
    };
    

    此外,如果您有一个对话框(窗口)的子控件,并且需要添加背景图片,您所需要做的就是

    #include "PictureWindow.h"
    BOOL XXXX::OnInitDialog()
    {
    	...
    
    	// The IDC_STATIC1 is the identifier of child window, and the m_hWnd is the
    	// parent window handle
    	m_PictureWindow.SubclassWindow( ::GetDlgItem( m_hWnd, IDC_STATIC1 ) ); 
    	m_PictureWindow.m_nMessageHandler = CPictureWindow::ClientPaint; // This is the default value
    	m_PictureWindow.Load("image.bmp");
    };
    
    © . All rights reserved.