VC6.0显示透明PNG图片

用VC做界面难免会遇到要用图片做界面,但VC6显示透明PNG还是非常费劲的,本文向大家介绍如何用CxImage类实现Static控件显示透明PNG

1.创建一基于对话框的MFC工程

2.新建类CMyStatic继承CStatic,源码如下:

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
{
// Construction
public:
	CMyStatic();


// Attributes
public:


// Operations
public:
	void SetImage(CxImage *);
// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMyStatic)
	//}}AFX_VIRTUAL


// Implementation
public:
	virtual ~CMyStatic();


	// Generated message map functions
protected:
	//{{AFX_MSG(CMyStatic)
	afx_msg void OnPaint();
	//}}AFX_MSG


	DECLARE_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_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyStatic

CMyStatic::CMyStatic()
{
}

CMyStatic::~CMyStatic()
{
}


BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
	//{{AFX_MSG_MAP(CMyStatic)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyStatic message handlers

void CMyStatic::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	RECT 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.在工程中添加PNG资源

4.在头文件中

CMyStatic m_sImg;(对应要显示PNG的Static控件)
CxImage m_ximage;

5.在OnInitDialog中增加

m_sImg.SetImage(&m_ximage);
m_ximage.LoadResource(FindResource(NULL, MAKEINTRESOURCE(IDR_PNG_LOGO), "PNG"), CXIMAGE_FORMAT_PNG);
m_sImg.Invalidate();

这样就可以显示PNG图片了。



猜你喜欢

转载自blog.csdn.net/jinhill/article/details/8434102
今日推荐