• coder walker
    了解作者
  • Visual C++
    开发工具
  • 350KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2019-12-02 09:32
    上传日期
包含透明色的位图的绘制方法有多种,最简单的方法是调用现成的函数:TransparentBlt,也可以通过自己的代码实现类似TransparentBlt的功能,实现过程也有两种形式,一种是事先做一张掩码位图,另一种是动态生成掩码位图。代码示例动态生成掩码位图绘制具有透明区域位图的方法。
TransparentBltTest.zip
  • TransparentBltTest
  • res
  • TransparentBltTest.rc2
    410B
  • bk.bmp
    416.3KB
  • football.bmp
    127.5KB
  • TransparentBltTest.ico
    1.1KB
  • TransparentBltTest.clw
    1.2KB
  • StdAfx.cpp
    220B
  • resource.h
    740B
  • TransparentBltTest.dsp
    4.4KB
  • TransparentBltTest.h
    1.4KB
  • TransparentBltTest.rc
    5.5KB
  • TransparentBltTestDlg.h
    1.5KB
  • TransparentBltTestDlg.cpp
    7.2KB
  • TransparentBltTest.cpp
    2.2KB
  • StdAfx.h
    1KB
  • TransparentBltTest.dsw
    561B
内容介绍
// TransparentBltTestDlg.cpp : implementation file // #include "stdafx.h" #include "TransparentBltTest.h" #include "TransparentBltTestDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #pragma comment( lib, "Msimg32.lib" ) ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTransparentBltTestDlg dialog CTransparentBltTestDlg::CTransparentBltTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTransparentBltTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTransparentBltTestDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTransparentBltTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTransparentBltTestDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTransparentBltTestDlg, CDialog) //{{AFX_MSG_MAP(CTransparentBltTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTransparentBltTestDlg message handlers BOOL CTransparentBltTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_bmpBackground.LoadBitmap(IDB_BKBMP); m_bmpFootball.LoadBitmap(IDB_FOOTBALLBMP); return TRUE; // return TRUE unless you set the focus to a control } void CTransparentBltTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } void TransparentBlt2( HDC hdcDest, // 目标DC int nXOriginDest, // 目标X偏移 int nYOriginDest, // 目标Y偏移 int nWidthDest, // 目标宽度 int nHeightDest, // 目标高度 HDC hdcSrc, // 源DC int nXOriginSrc, // 源X起点 int nYOriginSrc, // 源Y起点 int nWidthSrc, // 源宽度 int nHeightSrc, // 源高度 UINT crTransparent // 透明色,COLORREF类型 ) { HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest); // 创建兼容位图 HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL); // 创建单色掩码位图 HDC hImageDC = CreateCompatibleDC(hdcDest); HDC hMaskDC = CreateCompatibleDC(hdcDest); hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP); hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC, hMaskBMP); // 将源DC中的位图拷贝到临时DC中 if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc) BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY); else StretchBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY); // 设置透明色 SetBkColor(hImageDC, crTransparent); // 生成透明区域为白色,其它区域为黑色的掩码位图 BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY); // 生成透明区域为黑色,其它区域保持不变的位图 SetBkColor(hImageDC, RGB(0,0,0)); SetTextColor(hImageDC, RGB(255,255,255)); BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND); // 透明部分保持屏幕不变,其它部分变成黑色 SetBkColor(hdcDest,RGB(0xff,0xff,0xff)); SetTextColor(hdcDest,RGB(0,0,0)); BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND); // "或"运算,生成最终效果 BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT); SelectObject(hImageDC, hOldImageBMP); DeleteDC(hImageDC); SelectObject(hMaskDC, hOldMaskBMP); DeleteDC(hMaskDC); DeleteObject(hImageBMP); DeleteObject(hMaskBMP); } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CTransparentBltTestDlg::OnPaint() { CPaintDC dc(this); // device context for painting if (IsIconic()) { SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); COLORREF crMask = RGB(0x00,0x00,0xff); BITMAP bm; m_bmpFootball.GetBitmap(&bm); int nWidth = bm.bmWidth; int nHeight = bm.bmHeight; CDC ImageDC; ImageDC.CreateCompatibleDC(&dc); CBitmap *pOldImageBMP = ImageDC.SelectObject(&m_bmpFootball); //::TransparentBlt(dc.m_hDC,50,40,nWidth,nHeight,ImageDC.m_hDC,0,0,nWidth,nHeight,crMask); TransparentBlt2(dc.m_hDC,50,40,nWidth,nHeight,ImageDC.m_hDC,0,0,nWidth,nHeight,crMask); ImageDC.SelectObject(pOldImageBMP); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CTransparentBltTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CTransparentBltTestDlg::OnEraseBkgnd(CDC* pDC) { CRect rect; GetClientRect(&rect); CDC MemDC; MemDC.CreateCompatibleDC(pDC); CBitmap *pOldBMP = (CBitmap*)MemDC.SelectObject(&m_bmpBackground); pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY); if(pOldBMP) MemDC.SelectObject(pOldBMP); return TRUE;//CDialog::OnEraseBkgnd(pDC); }
评论
    相关推荐