MUILIB
让UI设计师的思想自由飞翔
::
首页
::
新随笔
:: ::
聚合
::
管理
::
33 随笔 :: 0 文章 :: 39 评论 :: 0 Trackbacks
<
2012年7月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
常用链接
我的随笔
我的评论
我参与的随笔
相册
ToolKit
模拟软件界面
界面开发
MUILIB界面开发库
专业的Win系列桌面软件界面开发库,技术讨论QQ群:41412011(已满) 183766467
最新评论
1. re: MUILIB题外话:Windows真的没落了吗
@Richard Wei
你的预言大多验证了,牛人啊
--龙酒
2. re: 从技术角度分析WIN10为何不全面支持窗口的玻璃特效
评论内容较长,点击标题查看
--fx
3. re: 从技术角度分析WIN10为何不全面支持窗口的玻璃特效
评论内容较长,点击标题查看
--bukebushuo
4. re: 从技术角度分析WIN10为何不全面支持窗口的玻璃特效
评论内容较长,点击标题查看
--溪流
5. re: 从技术角度分析WIN10为何不全面支持窗口的玻璃特效[未登录]
看起来有些道理
--jcily
评论排行榜
1. MUILIB题外话:Windows真的没落了吗(10)
2. MUILIB-为C++而坚守的高级界面库(8)
3. MUILIB-可视化界面编辑器功能测试版完成(8)
4. 从技术角度分析WIN10为何不全面支持窗口的玻璃特效(5)
5. MUILIB-UI开发库的简历(3)
IPicture使用代码
1
、IPicture接口对象的创建
方法1:直接通过文件创建
LPCSTR szFileUrl;
IPicture
*
pIPicture;
OleLoadPicturePath(CComBSTR(szFileUrl),
(LPUNKNOWN)NULL,
0
,
0
,
IID_IPicture,
(LPVOID
*
)
&
pIPicture))
方法2:通过IStream来创建
//
加载电子地图
LPCSTR szFileUrl;
IStream
*
pStream
=
NULL;
IPicture
*
pIPicture
=
NULL;;
CFileStatus fstatus;
CFile file;
LONG lFileSize;
HGLOBAL hGlobal;
if
(file.Open(szFileUrl, CFile::modeRead)
&&
file.GetStatus(szFileUrl, fstatus)
&&
((lFileSize
=
fstatus.m_size)
!=
-
1
))
{
hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, lFileSize);
//
开辟大内存
if
(hGlobal
!=
NULL)
{
LPVOID pvData
=
NULL;
pvData
=
GlobalLock(hGlobal);
if
(pvData
!=
NULL)
{
file.ReadHuge(pvData, lFileSize);
//
将图片数据保存在内存中
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
m_pIStream);
//
创建流
file.Close();
}
else
{
GlobalFree(hGlobal);
return
;
}
}
else
{
file.Close();
return
;
}
}
else
{
//
打开图片失败
return
;
}
OleLoadPicture(m_pIStream, fstatus.m_size, TRUE, IID_IPicture, (LPVOID
*
)
&
m_pIPicture);
GlobalFree(hGlobal);
2
、通过IPicture来获取图片的尺寸
OLE_XSIZE_HIMETRIC hmPicWidth;
OLE_YSIZE_HIMETRIC hmPicHeight;
pIPicture
->
get_Width(
&
hmPicWidth);
//
获取图像宽度
pIPicture
->
get_Height(
&
hmPicHeight);
//
获取图像高度
//
转化单位为象素
nPicWidth
=
MulDiv(hmPicWidth, GetDeviceCaps(GetDC()
->
m_hDC, LOGPIXELSX), HIMETRIC_PER_INCH);
nPicHeight
=
MulDiv(hmPicHeight, GetDeviceCaps(GetDC()
->
m_hDC, LOGPIXELSY), HIMETRIC_PER_INCH);
3
、通过IPicture来画图
void
ShowPicture(CDC
*
pDC)
{
OLE_XPOS_HIMETRIC xSrc;
//
图片中当前显示区域的x
OLE_YPOS_HIMETRIC ySrc;
//
图片中当前显示区域的y
OLE_XSIZE_HIMETRIC cxSrc;
//
图片中当前显示区域的宽度
OLE_YSIZE_HIMETRIC cySrc;
//
图片中当前显示区域的高度
m_pIPicture
->
Render(
*
pDC,
0
,
0
, m_rcBox.Width(), m_rcBox.Height(), xSrc, ySrc, cxSrc, cySrc, NULL);
}
4
、将IPicture中的图片保存起来
BOOL SaveToFileFromIPicture(LPCSTR szFileName, IPicture
*
pPic)
{
//
Create ILockBytes Buffer
ILockBytes
*
plkbyt
=
NULL;
CreateILockBytesOnHGlobal(NULL, TRUE,
&
plkbyt);
//
Create IStorage
IStorage
*
pStorage
=
NULL;
HRESULT hr
=
::StgCreateDocfileOnILockBytes(plkbyt,
STGM_SHARE_EXCLUSIVE
|
STGM_CREATE
|
STGM_READWRITE,
0
,
&
pStorage);
if
(FAILED(hr))
{
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
//
Create IStream
IStream
*
pStream
=
NULL;
hr
=
pStorage
->
CreateStream(L
"
PICTURE
"
,
STGM_SHARE_EXCLUSIVE
|
STGM_CREATE
|
STGM_READWRITE,
0
,
0
,
&
pStream);
if
(FAILED(hr))
{
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
//
Copy Data Stream
long
lSize;
hr
=
pPic
->
SaveAsFile(pStream, TRUE,
&
lSize);
if
(FAILED(hr))
{
pStream
->
Release();
pStream
=
NULL;
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
//
Get Statistics For Final Size Of Byte Array
STATSTG statStg;
hr
=
plkbyt
->
Stat(
&
statStg, STATFLAG_NONAME);
if
(FAILED(hr))
{
pStream
->
Release();
pStream
=
NULL;
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
//
calculate "Pure" Picture Data, Must Be In a 512 Blocks
double
dbSkipFloat
=
(
double
(lSize)
/
512
);
DWORD dwPicDataSize
=
0
;
if
(dbSkipFloat
>
DWORD(dbSkipFloat))
{
dwPicDataSize
=
(DWORD)dbSkipFloat
+
1
;
}
else
{
dwPicDataSize
=
(DWORD)dbSkipFloat;
}
dwPicDataSize
=
dwPicDataSize
*
512
;
//
Allocate Only The "Pure" Picture Data
BYTE
*
pPicDataBuffer
=
(BYTE
*
)malloc(dwPicDataSize);
if
(pPicDataBuffer
==
NULL)
{
pStream
->
Release();
pStream
=
NULL;
plkbyt
->
Release();
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
=
NULL;
return
FALSE;
}
//
Read "Pure" Picture Data to Buffer
_ULARGE_INTEGER ulOffset;
ulOffset.LowPart
=
0
;
ulOffset.HighPart
=
0
;
ulOffset.QuadPart
=
(DWORD)(statStg.cbSize.QuadPart
-
dwPicDataSize);
DWORD dwRealDataSize;
hr
=
plkbyt
->
ReadAt(ulOffset, pPicDataBuffer, dwPicDataSize,
&
dwRealDataSize);
if
(FAILED(hr))
{
free(pPicDataBuffer);
pPicDataBuffer
=
NULL;
pStream
->
Release();
pStream
=
NULL;
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
//
Save "Pure" Picture Data to file
CFile fBmp;
CFileException e;
if
(
!
fBmp.Open(szFileName, CFile::typeBinary
|
CFile::modeCreate
|
CFile::modeWrite,
&
e))
{
free(pPicDataBuffer);
pPicDataBuffer
=
NULL;
pStream
->
Release();
pStream
=
NULL;
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
FALSE;
}
fBmp.Write(pPicDataBuffer, dwRealDataSize);
fBmp.Close();
free(pPicDataBuffer);
pPicDataBuffer
=
NULL;
pStream
->
Release();
pStream
=
NULL;
pStorage
->
Release();
pStorage
=
NULL;
plkbyt
->
Release();
plkbyt
=
NULL;
return
TRUE;
}
5
、将IPicture中图片按规定的尺寸保存
BOOL CIPictureDlg::Save2File(LPCSTR szFileName, IPicture
*
pPic,
int
nWidth,
int
nHeight)
{
//
create a new IPicture
OLE_HANDLE hPic
=
NULL;
if
(FAILED(pPic
->
get_Handle(
&
hPic)))
{
return
FALSE;
}
HBITMAP hBmp
=
(HBITMAP)CopyImage((HANDLE)hPic,
IMAGE_BITMAP,
nWidth,
nWidth,
LR_CREATEDIBSECTION);
if
(hBmp
==
NULL)
{
return
FALSE;
}
PICTDESC picDesc;
picDesc.cbSizeofstruct
=
sizeof
(PICTDESC);
picDesc.picType
=
PICTYPE_BITMAP;
picDesc.bmp.hbitmap
=
hBmp;
IPicture
*
pNewPic
=
NULL;
if
(SUCCEEDED(OleCreatePictureIndirect(
&
picDesc, IID_IPicture, FALSE, (LPVOID
*
)
&
pNewPic)))
{
//
Save to file
Save2File(szFileName, pNewPic);
pNewPic
->
Release();
pNewPic
=
NULL;
DeleteObject(hBmp);
hBmp
=
NULL;
return
TRUE;
}
DeleteObject(hBmp);
hBmp
=
NULL;
return
FALSE;
}
posted on 2012-11-25 21:47
bukebushuo
阅读(577)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
Powered by:
C++博客
Copyright © bukebushuo