MFC实现的 picture control 类,对话框上显示图片

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

(文章原地址 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即可。

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

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

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值