一个透明的CStatic控件【原创】

如果我们自画了一个有背景的对话框,static控件在上面不透明是很不爽的。
改进方法很简单,从MFC的CStatic派生一个类,下面是相应的代码:
(使用的时候修改控件ID为其他值,不要是IDC_STATIC,否则不能添加用于ddx的变量)
 
 

//=========================================================
//
// Copyright (c) 2000-2004  iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Class: CTransparentStatic
//
// Product: iW988
// File   : TransparentStatic.h
// Author : 天衣有缝
// Created: 2004.12.29 23:40
// 
// Description:
//     ValueAdded main program for iW988.
//                   Contact:ShenZhen @ GuangDong
//                       [email protected]
//
//=========================================================
#pragma once
// CTransparentStatic

class CTransparentStatic : public CStatic
{
    DECLARE_DYNAMIC(CTransparentStatic)

public:
    CTransparentStatic();
    virtual ~CTransparentStatic();

protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnPaint();
};

//=========================================================
//
// Copyright (c) 2000-2004  iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Class: CTransparentStatic
//
// Product: iW988
// File   : TransparentStatic.cpp
// Author : 天衣有缝
// Created: 2004.12.29 23:40
// 
// Description:
//     ValueAdded main program for iW988.
//                   Contact:ShenZhen @ GuangDong
//                       [email protected]
//
//=========================================================
  
  



IMPLEMENT_DYNAMIC(CTransparentStatic, CStatic)
CTransparentStatic::CTransparentStatic()
{
}

CTransparentStatic::~CTransparentStatic()
{
}

BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
	ON_WM_PAINT()
END_MESSAGE_MAP()



 CTransparentStatic::OnPaint()
{
	CPaintDC dc(); 

	
	CRect client_rect;
	GetClientRect(client_rect);

	
	CString szText;
	GetWindowText(szText);

	
	CFont *pFont, *pOldFont;
	pFont  GetFont();
	pOldFont  dc.SelectObject(pFont);

	
	dc.SetBkMode(TRANSPARENT);

	
	dc.DrawText(szText, client_rect, );

	
	dc.SelectObject(pOldFont);

}


 
 
转载请注意: 下面的背景对话框是从CButtonST整理二来,
版权属于: Davide Calabro' [email protected]
 
   



//=========================================================
//
// Copyright (c) 2000-2004  iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Class: CBkDialogST
//
// Product: iW988
// File   : BkDialogST.h
// Author : 天衣有缝
// Created: 2004.12.29 23:50
// 
// Description:
//     ValueAdded main program for iW988.
//                   Contact:ShenZhen @ GuangDong
//                       [email protected]
//
//=========================================================
#pragma once class CBkDialogST : public CDialog { public: CBkDialogST(CWnd* pParent = NULL); // 标准构造函数 CBkDialogST(UINT uResourceID, CWnd* pParent = NULL); CBkDialogST(LPCTSTR pszResourceID, CWnd* pParent = NULL); virtual ~CBkDialogST(); // 析构 DWORD SetMode(BYTE byMode, BOOL bRepaint = TRUE); // 设置背景位图显示模式 DWORD SetBitmap(HBITMAP hBitmap); // 设置背景位图 DWORD SetBitmap( int nBitmap); // 设置背景位图 DWORD ActivateEasyMoveMode(BOOL bActivate); // 客户区可以随意拖动 DWORD ShrinkToFit(BOOL bRepaint = TRUE); // 使对话框只显示一个背景位图大小,左上角位置不变 enum { // 背景位图显示模式 BKDLGST_MODE_TILE , // 平铺 BKDLGST_MODE_CENTER , // 居中 BKDLGST_MODE_STRETCH , // 拉伸 BKDLGST_MODE_TILETOP , // 只平铺一行,top BKDLGST_MODE_TILEBOTTOM , // 只平铺一行,bottom BKDLGST_MODE_TILELEFT , // 只平铺一列,left BKDLGST_MODE_TILERIGHT , // 只平铺一列,right BKDLGST_MODE_TOPLEFT , // 只显示一图:top left BKDLGST_MODE_TOPRIGHT , // 只显示一图:top right BKDLGST_MODE_TOPCENTER , // 只显示一图:top center BKDLGST_MODE_BOTTOMLEFT , // 只显示一图:bottom left BKDLGST_MODE_BOTTOMRIGHT, // 只显示一图:bottom right BKDLGST_MODE_BOTTOMCENTER, // 只显示一图:bottom center BKDLGST_MAX_MODES }; enum { BKDLGST_OK , // 函数设置成功 BKDLGST_INVALIDRESOURCE , // 无效的位图资源 BKDLGST_INVALIDMODE          // 无效的显示模式 }; protected: //{{AFX_MSG(CBkDialogST) afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnSize(UINT nType, int cx, int cy); //}}AFX_MSG virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 virtual void OnPostEraseBkgnd(CDC* pDC); // 扩展功能 afx_msg void OnLButtonDown(UINT nFlags, CPoint point); // 支持鼠标拖动对话框 static short GetVersionI() { return 11;} // 程序版本 static LPCTSTR GetVersionC() { return (LPCTSTR)_T( "1.1");} // 程序版本 private: void Init(); void FreeResources(BOOL bCheckForNULL = TRUE); HBITMAP m_hBitmap ; // 位图句柄Handle to bitmap DWORD m_dwWidth ; // 位图宽度 DWORD m_dwHeight ; // 位图高度 BYTE m_byMode ; // 当前显示模式 BOOL m_bEasyMoveMode ; // 是否可用鼠标拖动对话框 DECLARE_MESSAGE_MAP() };
 
 
//=========================================================
//
// Copyright (c) 2000-2004  iWise Technologies,Co. Ltd.
// All Rights Reserved.
//
// Class: CBkDialogST
//
// Product: iW988
// File   : BkDialogST.cpp
// Author : 天衣有缝
// Created: 2004.12.29 23:50
// 
// Description:
//     ValueAdded main program for iW988.
//                   Contact:ShenZhen @ GuangDong
//                       [email protected]
//
//=========================================================
 
  
  

CBkDialogST::CBkDialogST(CWnd* pParent )
{
	Init();
}

CBkDialogST::CBkDialogST(UINT uResourceID, CWnd* pParent)
	: CDialog(uResourceID, pParent)
{
	Init();
}


CBkDialogST::CBkDialogST(LPCTSTR pszResourceID, CWnd* pParent)
	: CDialog(pszResourceID, pParent)
{
	Init();
}

CBkDialogST::~CBkDialogST()
{
	FreeResources();
}

 CBkDialogST::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CBkDialogST, CDialog)
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

 CBkDialogST::Init()
{
	FreeResources(FALSE);

	
	m_byMode  BKDLGST_MODE_TILE;

	
	m_bEasyMoveMode  TRUE;
}

 CBkDialogST::FreeResources(BOOL bCheckForNULL)
{
	
	 (bCheckForNULL  TRUE)
	{
		 (m_hBitmap)	::DeleteObject(m_hBitmap);
	}

	m_hBitmap  NULL;
	m_dwWidth  ;
	m_dwHeight  ;
}

 CBkDialogST::OnLButtonDown(UINT nFlags, CPoint point) 
{
	
	 (m_bEasyMoveMode  TRUE)
	    PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y));
	
	CDialog::OnLButtonDown(nFlags, point);
}

DWORD CBkDialogST::ActivateEasyMoveMode(BOOL bActivate)
{
	
	m_bEasyMoveMode  bActivate;
	 BKDLGST_OK;
}

DWORD CBkDialogST::SetBitmap( nBitmap)
{
	HBITMAP		hBitmap	 NULL;
	HINSTANCE	hInstResource	 NULL;

	
	hInstResource  AfxFindResourceHandle(MAKEINTRESOURCE(nBitmap), RT_BITMAP);

	
	hBitmap  (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmap), IMAGE_BITMAP, , , );

	 SetBitmap(hBitmap);	
}

DWORD CBkDialogST::SetBitmap(HBITMAP hBitmap)
{
		nRetValue;
	BITMAP	csBitmapSize;

	
	FreeResources();

	 (hBitmap)
	{
		m_hBitmap  hBitmap;

		
		nRetValue  ::GetObject(hBitmap, (csBitmapSize), &csBitmapSize);
		 (nRetValue  )
		{
			
			FreeResources();
			 BKDLGST_INVALIDRESOURCE;
		}
		m_dwWidth  (DWORD)csBitmapSize.bmWidth;	
		m_dwHeight  (DWORD)csBitmapSize.bmHeight;	
	}

	Invalidate();

	 BKDLGST_OK;
}

DWORD CBkDialogST::SetMode(BYTE byMode, BOOL bRepaint)
{
	 (byMode > BKDLGST_MAX_MODES)	 BKDLGST_INVALIDMODE;

	
	m_byMode  byMode;

	 (bRepaint  TRUE)	Invalidate();

	 BKDLGST_OK;
}

DWORD CBkDialogST::ShrinkToFit(BOOL bRepaint)
{
	
	CRect	rWnd;
	CRect	rClient;
	DWORD	dwDiffCX;
	DWORD	dwDiffCY;

	::GetWindowRect(m_hWnd, &rWnd);			
	::GetClientRect(m_hWnd, &rClient);		

	dwDiffCX  rWnd.Width()  rClient.Width();	
	dwDiffCY  rWnd.Height()  rClient.Height();	

	m_byMode  BKDLGST_MODE_CENTER;			

	
	MoveWindow(rWnd.left, rWnd.top, dwDiffCX  m_dwWidth, dwDiffCY  m_dwHeight, bRepaint);

	 BKDLGST_OK;
}

 CBkDialogST::OnSize(UINT nType,  cx,  cy) 
{
	CDialog::OnSize(nType, cx, cy);

	
	 (m_hBitmap  NULL)
	{
		Invalidate();
	}
}

 CBkDialogST::OnPostEraseBkgnd(CDC* pDC)
{
	
}

BOOL CBkDialogST::OnEraseBkgnd(CDC* pDC) 
{
	CRect		rWnd;
			nX			 ;
			nY			 ;

	BOOL	bRetValue  CDialog::OnEraseBkgnd(pDC);

	
	 (m_hBitmap)
	{
		GetClientRect(rWnd);		

		CDC		dcMemoryDC;	
		CBitmap		bmpMemoryBitmap;  
		CBitmap*	pbmpOldMemoryBitmap  NULL;

		dcMemoryDC.CreateCompatibleDC(pDC);			
		bmpMemoryBitmap.CreateCompatibleBitmap(pDC, rWnd.Width(), rWnd.Height());
								
		pbmpOldMemoryBitmap  (CBitmap*)dcMemoryDC.SelectObject(&bmpMemoryBitmap);
								
		dcMemoryDC.FillSolidRect(rWnd, pDC>GetBkColor());	


		CDC		dcTempDC;		
		HBITMAP		hbmpOldTempBitmap  NULL;
		dcTempDC.CreateCompatibleDC(pDC);
		hbmpOldTempBitmap  (HBITMAP)::SelectObject(dcTempDC.m_hDC, m_hBitmap);

		 (m_byMode)
		{
			
				// 平铺位图
				 (nY < rWnd.Height()) 
				{
					(nX < rWnd.Width()) 
					{
						dcMemoryDC.BitBlt(nX, nY, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
						nX  m_dwWidth;
					}
					nX  ;
					nY  m_dwHeight;
				}
				;
			
				nX  ((rWnd.Width()  m_dwWidth)/);
				nY  ((rWnd.Height()  m_dwHeight)/);
				dcMemoryDC.BitBlt(nX, nY, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				// 拉伸位图
				dcMemoryDC.StretchBlt(, , rWnd.Width(), rWnd.Height(), &dcTempDC, , , m_dwWidth, m_dwHeight, SRCCOPY);
				;
			
				(nX < rWnd.Width()) 
				{
					dcMemoryDC.BitBlt(nX, , m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
					nX  m_dwWidth;
				}
				;
			
				(nX < rWnd.Width()) 
				{
					dcMemoryDC.BitBlt(nX, rWnd.bottom  m_dwHeight, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
					nX  m_dwWidth;
				}
				;
			
				 (nY < rWnd.Height()) 
				{
					dcMemoryDC.BitBlt(, nY, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
					nY  m_dwHeight;
				}
				;
			
				 (nY < rWnd.Height()) 
				{
					dcMemoryDC.BitBlt(rWnd.right  m_dwWidth, nY, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
					nY  m_dwHeight;
				}
				;
			
				dcMemoryDC.BitBlt(, , m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				dcMemoryDC.BitBlt(rWnd.right  m_dwWidth, , m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				nX  ((rWnd.Width()  m_dwWidth)/);
				dcMemoryDC.BitBlt(nX, , m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				dcMemoryDC.BitBlt(, rWnd.bottom  m_dwHeight, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				dcMemoryDC.BitBlt(rWnd.right  m_dwWidth, rWnd.bottom  m_dwHeight, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
			
				nX  ((rWnd.Width()  m_dwWidth)/);
				dcMemoryDC.BitBlt(nX, rWnd.bottom  m_dwHeight, m_dwWidth, m_dwHeight, &dcTempDC, , , SRCCOPY);
				;
		}

		pDC>BitBlt(, , rWnd.Width(), rWnd.Height(), &dcMemoryDC, , , SRCCOPY);

		OnPostEraseBkgnd(&dcMemoryDC);

		::SelectObject(dcTempDC.m_hDC, hbmpOldTempBitmap);
		dcMemoryDC.SelectObject(pbmpOldMemoryBitmap);
	}

	 bRetValue;
}

你可能感兴趣的:(Win32/MFC)