ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```c++ #ifndef __MEM_DC__H__ #define __MEM_DC__H__ //防止重复编译 #include "stdafx.h" class CMemDCZ : public CDC { CSize m_size; public: ~CMemDCZ() { DeleteDC(); } CMemDCZ() { m_size.cx = m_size.cy=0; } CMemDCZ(UINT nBitmap,CDC* pDC=NULL) { LoadBitmap(nBitmap,pDC); } CMemDCZ(LPCWSTR szFile,CDC* pDC=NULL) { LoadBitmap(szFile,pDC); } CMemDCZ(int cx,int cy,CDC* pDC=NULL) { Create(cx,cy,pDC); } BOOL DeleteDC() { if(!GetSafeHdc()) return FALSE; CBitmap* pBitmap = GetCurrentBitmap(); if(pBitmap) pBitmap ->DeleteObject(); return CDC::DeleteDC(); } int GetWidth(){return m_size.cx;} int GetHeight(){return m_size.cy;} BOOL Create(int cx,int cy,CDC* pDC=NULL) {//创建空白位图 CBitmap bmp; if(!bmp.CreateCompatibleBitmap(pDC,cx,cy)) return FALSE; m_size.cx = cx; m_size.cy = cy; CreateCompatibleDC(pDC); SelectObject(&bmp); return TRUE; } BOOL LoadBitmap(UINT nBitmap,CDC* pDC=NULL) {//进程内位图资源加载 CBitmap bmp; if(!bmp.LoadBitmap(nBitmap)) return FALSE; BITMAP bm; bmp.GetBitmap(&bm); m_size.cx = bm.bmWidth; m_size.cy = bm.bmHeight; CreateCompatibleDC(pDC); SelectObject(&bmp); return TRUE; } BOOL LoadBitmap(LPCWSTR szFile,CDC* pDC=NULL) {//进程外位图图片文件加载 HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,szFile,IMAGE_BITMAP, 0,0,LR_LOADFROMFILE); if(!hBitmap) return FALSE; BITMAP bm; GetObject(hBitmap,sizeof(bm),&bm); m_size.cx = bm.bmWidth; m_size.cy = bm.bmHeight; CreateCompatibleDC(pDC); SelectObject(hBitmap); return TRUE; } void BitTrans( int nXDest, // 目标起点X int nYDest, // 目标起点Y int nWidthDest, // 目标宽度 int nHeightDest,// 目标高度 CDC* pDC, // 目标DC int nXSrc, // 来源起点X int nYSrc, // 来源起点Y COLORREF crTrans// 透明色 ) { CMemDCZ dcImage(nWidthDest, nHeightDest,pDC);//临时DC CBitmap bmpMask; bmpMask.CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL); // 创建单色掩码位图 CDC dcMask;//掩码DC dcMask.CreateCompatibleDC(pDC); dcMask.SelectObject(bmpMask); //将载入位图的内存DC中的位图,拷贝到临时DC中 dcImage.BitBlt( 0, 0, nWidthDest, nHeightDest, this, nXSrc, nYSrc, SRCCOPY); // 设置临时DC的透明色 dcImage.SetBkColor(crTrans); //掩码DC的透明区域为白色其它区域为黑色 dcMask.BitBlt(0, 0, nWidthDest, nHeightDest, &dcImage, 0, 0, SRCCOPY); //临时DC透明区域为黑色,其它区域保持不变 dcImage.SetBkColor(RGB(0,0,0)); dcImage.SetTextColor(RGB(255,255,255)); dcImage.BitBlt( 0, 0, nWidthDest, nHeightDest, &dcMask, 0, 0, SRCAND); // 目标DC透明部分保持屏幕不变,其它部分变成黑色 pDC ->SetBkColor(RGB(255,255,255)); pDC ->SetTextColor(RGB(0,0,0)); pDC ->BitBlt(nXDest, nYDest, nWidthDest, nHeightDest, &dcMask, 0, 0, SRCAND); pDC ->BitBlt(nXDest, nYDest, nWidthDest, nHeightDest, &dcImage, 0, 0, SRCPAINT); } void StretchTrans( int nXDest, // 目标起点X int nYDest, // 目标起点Y int nWidthDest, // 目标宽度 int nHeightDest, // 目标高度 CDC* pDC, // 目标DC int nXSrc, // 来源起点X int nYSrc, // 来源起点Y int nWidthSrc, // 来源宽度 int nHeightSrc, // 来源高度 COLORREF crTrans // 透明色 ) { CMemDCZ dcImage(nWidthDest, nHeightDest,pDC);//临时DC CBitmap bmpMask; // 创建单色掩码位图 bmpMask.CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL); CDC dcMask; dcMask.CreateCompatibleDC(pDC); dcMask.SelectObject(bmpMask); // 将载入位图的内存DC中的位图,拷贝到临时DC中 if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc) dcImage.BitBlt(0, 0, nWidthDest, nHeightDest, this, nXSrc, nYSrc, SRCCOPY); else dcImage.StretchBlt(0, 0, nWidthDest, nHeightDest, this, nXSrc, nYSrc, nWidthSrc, nHeightSrc, SRCCOPY); // 设置临时DC的透明色 dcImage.SetBkColor( crTrans); //掩码DC的透明区域为白色其它区域为黑色 dcMask.BitBlt(0,0,nWidthDest, nHeightDest,&dcImage,0,0,SRCCOPY); //临时DC透明区域为黑色,其它区域保持不变 dcImage.SetBkColor(RGB(0,0,0)); dcImage.SetTextColor(RGB(255,255,255)); dcImage.BitBlt(0, 0, nWidthDest, nHeightDest, &dcMask, 0, 0, SRCAND); // 目标DC透明部分保持屏幕不变,其它部分变成黑色 pDC ->SetBkColor(RGB(255,255,255)); pDC ->SetTextColor(RGB(0,0,0)); pDC ->BitBlt(nXDest, nYDest, nWidthDest, nHeightDest, &dcMask, 0, 0, SRCAND); pDC ->BitBlt(nXDest, nYDest, nWidthDest, nHeightDest, &dcImage, 0, 0, SRCPAINT); } }; #endif ```