面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0

以上是源码及演示程序下载地址

(文章原地址 http://www.codeproject.com/Articles/24969/An-MFC-picture-control-to-dynamically-show-picture

Introduction 介绍

这篇文章描述的是一个可以用于在对话框上显示各种主流类型图片 (如 BMP, GIF, JPEG...) MFC控件

Background 背景

我花了一些时间去搜索可以用于显示图片的MFC控件, 但却没有发现合适的。 所以我决定自己做一个轻量级,灵活度高的图片控件(Picture control)去显示各种类型的图片。

Using the code 如何使用

这个控件内部使用的是GDI+库,所以请在使用时把GdiPlus.lib加入到你的工程中(include libraries)。

使用这个控件时,先用VC++对话框设计器创建一个静态文字控件(static text control 。之后用MFC向导为这个控件分配一个控件变量,类型定义为CPictureCtrl。

现在你可以用你的控件装载显示图片了,你只需要在这几个CPictureCtrl::LoadFrom... 函数, 选择合适你需要的的进行调用。装载后控件会自动更新并显示图片。

要清除掉控件中显示的图片,调用CPictureCtrl::FreeImage即可。

你的图片会被自动调整到控件的大小,这可能会改变图片原先的长宽比例。

 Collapse | Copy Code
class CPictureCtrl :     
public CStatic
{
public:
   //Constructor
   CPictureCtrl(void);
   //Destructor
   ~CPictureCtrl(void);

public:
   //Loads an image from a file
   BOOL LoadFromFile(CString &szFilePath);
   //Loads an image from an IStream interface
   BOOL LoadFromStream(IStream* piStream);
   //Loads an image from a byte stream;
   BOOL LoadFromStream(BYTE* pData, size_t nSize);
   //Loads an image from a Resource
   // BOOL LoadFromResource(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);
   //Overload - Single load function
   BOOL Load(CString &szFilePath);
   BOOL Load(IStream* piStream);
   BOOL Load(BYTE* pData, size_t nSize);
   // BOOL Load(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType);
   //Frees the image data
   void FreeData();

protected:
   virtual void PreSubclassWindow();
   //Draws the Control
   virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
   virtual BOOL OnEraseBkgnd(CDC* pDC);

private:
   //Internal image stream buffer
   IStream* m_pStream;
   //Control flag if a pic is loaded
   BOOL m_bIsPicLoaded;
   //GDI Plus Token
   ULONG_PTR m_gdiplusToken; };

Points of interest

这个控件是基于 CStatic control 设计的(基类使用的是CStatic)。所以你可以使用CStatic control的各种功能,但它并不会显示任何文字。对GDI+库的使用使其可以支持各种主流类型的图片。

History 历史

  • 1.0 - Initial release.
  • 1.1 - A bug when drawing the control without a loaded image was corrected.
  • 1.2 - A bug when drawing the control was corrected.

    Loading an image from a resource is disabled due to problems recognizing it correctly as an image.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TEiseler

Tester / Quality Assurance

Germany Germany

Member
 

本文转自:http://blog.csdn.net/cashey1991/article/details/7516996
posted on 2013-07-22 18:09 王海光 阅读(1976) 评论(0)  编辑 收藏 引用 所属分类: MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理