When using VC as the interface, you will inevitably encounter the need to use images as the interface, but it is still very difficult to use vc6 to display transparent PNG. This article will introduce how to use the cximage class to implement static controls to display transparent PNG.
1. Create a dialog box-based MFC project
2. The new cmystatic class inherits cstatic. The source code is as follows:
MyStatic.h
#if !defined(AFX_MYSTATIC_H__384ED2FD_4979_4F49_A362_EF7AEFA836B4__INCLUDED_)#define AFX_MYSTATIC_H__384ED2FD_4979_4F49_A362_EF7AEFA836B4__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// MyStatic.h : header file///////////////////////////////////////////////////////////////////////////////// CMyStatic window#include ".\\include\\ximage.h"class CMyStatic : public CStatic{// Constructionpublic:CMyStatic();// Attributespublic:// Operationspublic:void SetImage(CxImage *);// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CMyStatic)//}}AFX_VIRTUAL// Implementationpublic:virtual ~CMyStatic();// Generated message map functionsprotected://{{AFX_MSG(CMyStatic)afx_msg void OnPaint();//}}AFX_MSGDECLARE_MESSAGE_MAP()private:CxImage *m_pImage;};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYSTATIC_H__384ED2FD_4979_4F49_A362_EF7AEFA836B4__INCLUDED_)
MyStatic.cpp
// MyStatic.cpp : implementation file//#include "stdafx.h"#include "MyStatic.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMyStaticCMyStatic::CMyStatic(){}CMyStatic::~CMyStatic(){}BEGIN_MESSAGE_MAP(CMyStatic, CStatic)//{{AFX_MSG_MAP(CMyStatic)ON_WM_PAINT()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyStatic message handlersvoid CMyStatic::OnPaint() {CPaintDC dc(this); // device context for paintingRECT rect;this->GetClientRect(&rect); RGBQUAD rgb=m_pImage->GetPixelColor(1,1); m_pImage->IncreaseBpp(24); m_pImage->SetTransIndex(24); m_pImage->SetTransColor(rgb);m_pImage->Draw2(dc.m_hDC, rect);}void CMyStatic::SetImage(CxImage *pImage) {m_pImage = pImage;}
3. Add PNG resources to the project
4. In the header file
Cmystaticm_simg; (corresponding to the static control to display PNG)
Cximage m_ximage;
5. Add in oninitdialog
M_simg.setimage (& m_ximage );
M_ximage.loadresource (findresource (null, makeintresource (idr_png_logo), "PNG"), cximage_format_png );
M_simg.invalidate ();
In this way, the PNG image can be displayed.