C++/MFC-Picture Control

Picture Control属于Cstatic类

能显示三种类型的图片:

Bitmap:位图(.bmp)
Cursor:光标(.cur)
Icon:图标(.icon)

一、Picture Control 控件类

CStatic::

SetBitmap

Specifies a bitmap to be displayed in the static control.

GetBitmap

Retrieves the handle of the bitmap previously set with SetBitmap.

SetIcon

Specifies an icon to be displayed in the static control.

GetIcon

Retrieves the handle of the icon previously set with SetIcon.

SetCursor

Specifies a cursor image to be displayed in the static control.

GetCursor

Retrieves the handle of the cursor image previously set with SetCursor.

SetEnhMetaFile

Specifies an enhanced metafile to be displayed in the static control.

GetEnhMetaFile

Retrieves the handle of the enhanced metafile previously set with SetEnhMetaFile


二、加载图片

注意:加载不同类型的图片时,picture control 控件的type属性中应改为相应的类型

Type:Frame //框架
Type:Etched Horz水平蚀刻线条
Type:Etched Vert垂真蚀刻线条
Type:Rectangle实心矩形
Type:Bitmap位图
Type:Icon  图标
Type:Enhanced Metafile 增强图元 支持wmf格式图片
Type:Owner Draw   自绘图
Color:颜色
    Black:黑
    Gray:灰色
    White:白色
    Etched:蚀刻

2.1LoadImage函数

HANDLE LoadImage(
 HINSTANCE hinst,   // handle of the instance containing the image
 LPCTSTR lpszName,  // name or identifier of image
 UINT uType,         // type of image
 int cxDesired,        // desired width
 int cyDesired,        // desired height
 UINT fuLoad        // load flags
);

2.2从文件中载入

//载入文件里(即通过CFileDialog打开)的图片   文件中的第一个参数一般为空,路径,类型,宽,高, 默认大小
 HBITMAP  hbp=(HBITMAP)LoadImage(    NULL,   fileName,  IMAGE_BITMAP,   0,  0,  LR_DEFAULTSIZE|LR_LOADFROMFILE) ;

2.3从资源中载入

具体的可以看:

VS2010/MFC编程入门之二十七(常用控件:图片控件Picture Control)

//载入资源里的图片
 hbp=(HBITMAP)LoadImage(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2),IMAGE_BITMAP,0,0,LR_DEFAULTSIZE) ;

2.4图片缩放

//图片大小缩放
 hbp=(HBITMAP)LoadImage(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2),IMAGE_BITMAP,33,221,0) ;

三、释放资源

Bitmap

DeleteObject

Cursor

DestroyCursor

Icon

DestroyIcon


例:

HCURSOR  hbp=(HCURSOR)LoadImage(    NULL,   fileName,  IMAGE_CURSOR,   0,  0,  LR_DEFAULTSIZE|LR_LOADFROMFILE) ;
DestroyCursor(hbp);



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC 的图片控件中绘制图形时,可以在控件的`OnPaint()`函数中绘制。当窗口大小改变后,控件的大小可能会发生变化,导致图形消失。 为了解决这个问题,可以通过在控件类中添加一个成员变量来保存绘制的图形,然后在`OnPaint()`函数中绘制保存的图形,并根据控件的大小进行缩放。 具体实现步骤如下: 1. 在控件类中添加一个成员变量来保存绘制的图形,例如: ``` class CMyPictureCtrl : public CStatic { private: CBitmap m_bmp; // 保存绘制的图形 // ... }; ``` 2. 在控件的`OnPaint()`函数中绘制保存的图形,并根据控件的大小进行缩放,例如: ``` void CMyPictureCtrl::OnPaint() { CPaintDC dc(this); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap* pOldBmp = memDC.SelectObject(&m_bmp); CRect rect; GetClientRect(&rect); dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, m_bmp.GetBitmapDimension().cx, m_bmp.GetBitmapDimension().cy, SRCCOPY); memDC.SelectObject(pOldBmp); } ``` 3. 在绘制图形时,将绘制的结果保存到成员变量中,并根据控件的大小进行缩放,例如: ``` void CMyPictureCtrl::DrawShape() { CDC* pDC = GetDC(); CDC memDC; memDC.CreateCompatibleDC(pDC); m_bmp.CreateCompatibleBitmap(pDC, cx, cy); CBitmap* pOldBmp = memDC.SelectObject(&m_bmp); // 在 memDC 中绘制图形 // ... memDC.SelectObject(pOldBmp); ReleaseDC(pDC); Invalidate(); // 重绘控件 } ``` 这样,在窗口大小改变后,绘制的图形就不会消失了,而且会根据控件的大小进行缩放。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值