位图用法扩展库






4.67/5 (5投票s)
2001年1月16日

255973

6594
包含高级位图用法功能的 MFC 扩展 DLL
引言
不用说,位图是不同栅格图像格式与GDI图像表示之间的桥梁。设备无关位图(DIB)是重要的对象,允许我们保存、加载和绘制图像,而无需考虑设备上下文中的显示模式——以独立的方式。操作系统将尽其所能以尽可能接近原始图像的方式输出它们,因为DIB包含所有必要的颜色使用、颜色深度和图像分辨率信息。有很多,或者说,无数的类支持DIB操作,通过封装Windows API函数来实现。这个MFC扩展DLL包含这样的类,以及更多内容,以使位图的使用更轻松、更灵活。
包含的类
所有类声明都包含在bmpext.h头文件中。
设备相关位图(DDB)扩展类CDDBDrawEx
该类旨在在堆栈上分配和在单个范围内使用,因为它没有默认构造函数,也没有“真实”的数据成员,只有指向外部定义的指针。
它公开以下公共方法
// Inplace constructor. pDC - pointer to drawing context, pbmSrc - pointer to DDB to render // on pDC, pbmBack - optional pointer to background bitmap, that pbmSrc to blend with during transparent draw. CDDBDrawEx(CDC* pDC, CBitmap* pbmSrc, CBitmap* pbmBack = NULL); // Wraps around source DDB filling on pDC within rDest rectangle. void Fill(CRect& rDest); // Wrapper around BitBlt API function. Blitting source bitmap to pDC into rDest rectangle. void Draw(CRect& rDest, CPoint& pntSrc); // Perform transparent DDB draw, assuming crMask color stands for transparent pixels // in source DDB. It uses modified routine taken from flicker-free drawing example on // www.codeguru.com. It uses optional pbmBack if supplied in constructor to do this flicker - free drawing. void DrawTransparent(CRect& rDest, CPoint& pntSrc, COLORREF crMask); // Constructs complex region from source bitmap using cTransparentColor and cTolerance // for defining transparent (hollow) region areas. Internally, it uses modified region // builder code by Jean-Edouard Lachand-Robert http://www.geocities.com/Paris/LeftBank/1160/resume.htm). // It's extremely useful when playing with ::SetWindowRgn function to create windows with // non-rectangular shape. HRGN MakeRgn(COLORREF cTransparentColor = 0, COLORREF cTolerance = 0x101010);
DIB位图封装类CDib
这个类和弗兰肯斯坦怪物有很多共同之处 :) 它的部分代码来自MFC 4.0书籍附带的示例,部分来自CodeGuru和其他来源。我对其进行了修改,组合在一起,然后说:“它活了……活过来了!” :)
它公开以下公共方法
<CODE>// Default constructor. CDib(); // Several self-explanatory methods. DWORD Width(); DWORD Height(); WORD NumColors( BITMAPINFOHEADER& bmiHeader ); // Object validation checking method. BOOL IsValid(); // Frees internal DIB member data, making object invalid. (IsValid returns false) void Invalidate(); // Drawing method, automatically invokes DIB stretching. BOOL Draw(CDC*, CRect& rectDC, CRect& rectDIB); // Direct DIB pixel access methods. void SetPixel( int iX, int iY, RGBQUAD& rgbPixel ); RGBQUAD GetPixel(int iX, int iY); // Save DIB to disk file. DWORD Save(CFile& file); // Read DIB from file, support resource reading. If bFromResource = TRUE, // then it just skips DIB file marker read phase. DWORD Read(CFile& file, BOOL bFromResource = FALSE ); // Reads DIB from resource, basing on its nResID. Internally invokes previous Read method. DWORD ReadFromResource(UINT nResID); // Next two methods create DDB from DIB object. HBITMAP CreateDDBitmap(CDC* pDC); HBITMAP CreateDDBitmap( HDC hDC ); // Tries to compress DIB first making it DDB, then changing compression attribute and // converting it back to DIB. BOOL Compress(CDC* pDC, BOOL bCompress ); // Assignment operator CDib& operator = (CDib& dib);
扩展类用法
CDDBDrawEx类用法
<CODE>{ .... // CDC* pDC, CBitmap* bmImg, CRect rDest, and COLORREF crImgMask are declared and initialized elsewhere .... // inplace construction CDDBDrawEx ddbDrawEx(pDC, bmImg); // Do transparent drawing ddbDrawEx.DrawTransparent(rDest, CPoint(0), crImgMask ); // Do bitmapped fill ddbDrawEx.Fill( rDest ) } // Region creation. CBitmap bmSkin is declared and initialized elsewhere { .... CDDBDrawEx ddbDrawEx(NULL, &m_bmSkin ); return ddbDrawEx.MakeRgn( m_crTransparent, m_crTolerance ); }
CDib类用法
<CODE>CDib dibTemp;
CDC* pDC = CDC::FromHandle( ::GetDC( NULL ) );
....
// nResID is declared and initialized elsewhere
dibTemp.ReadFromResource( nResID );
....
}
实现这些扩展的演示项目还包含用于自定义对话框框架的类模板。我将在未来一周内发布一篇详细的评论来描述这个模板。它将涵盖窗口框架自定义技术,并辅以几个辅助模板类。目前为止就这些了,但我将在不久的将来提供几个有用的控件和动画类库,打包为MFC扩展DLL。 欢迎提出补充、更正和建议,因为我经常没有足够的时间来优化我的源代码或使其更优雅。 请通过以下方式与我联系:gromov@eti.co.ru。