VC++6.0 通过HTTP方式获取网页 OpenURL
1
//
头部包含afxinet.h
2
#include
<
afxinet.h
>
3
CInternetSession::OpenURL函数原型为:
4
CStdioFile
*
OpenURL( LPCTSTR pstrURL,
//
文件URL地址
5
DWORD dwContext
=
1
,
//
上下文ID
6
DWORD dwFlags
=
INTERNET_FLAG_TRANSFER_ASCII,
//
标记
7
LPCTSTR pstrHeaders
=
NULL,
//
发送到服务器的数据头
8
DWORD dwHeadersLength
=
0
);
//
发送到服务器的数据头长度
9
dwFlags可以为:
10
INTERNET_FLAG_RELOAD 强制重读数据
11
INTERNET_FLAG_DONT_CACHE 不保存到缓存
12
INTERNET_FLAG_TRANSFER_ASCII 使用文本数据
13
INTERNET_FLAG_TRANSFER_BINARY 使用二进制数据
14
//
获取网页的方法代码如下
15
CString getURLContext(LPCTSTR pstrURL)
16
{
17
//
自定义http头信息
18
char
*
headers
=
"
Accept:*/*\r\n
"
19
"
Accept-Language:zh-cn\r\n
"
20
"
User-Agent:VCTestClient\r\n
"
;
21
CString m_strHTML;
22
try
23
{
24
CInternetSession sess;
//
建立会话
25
CHttpFile
*
pF
=
(CHttpFile
*
)sess.OpenURL(pstrURL,
1
,INTERNET_FLAG_TRANSFER_ASCII
||
INTERNET_FLAG_RELOAD,headers,strlen(headers));
//
打开文件
26
注意:OpenURL 方法的dwFlags参数 必须选择传输模式为INTERNET_FLAG_TRANSFER_ASCII 或者 INTERNET_FLAG_TRANSFER_BINARY
27
CString szData,szAllData;
28
while
(pF
->
ReadString(szData))
29
{
30
//
读取文件
31
szAllData
+=
"
\r\n
"
;
32
szAllData
+=
szData;
33
}
34
pF
->
Close();
35
sess.Close();
36
m_strHTML
=
szAllData;
37
}
38
catch
(CException
*
e)
39
{
40
AfxMessageBox(
"
获取数据失败
"
);
41
}
42
return
m_strHTML;
43
}
-------------------------------------------------------------------------------------------------------------------------------------
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT CBMPDlg::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
::GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
char
*
headers
=
"
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*
"
"
Accept-Encoding: gzip, deflate\r\n
"
"
Accept-Language: zh-CN\r\n
"
"
Content-Type: application/x-www-form-urlencoded\r\n
"
"
Cache-Control: no-cache\r\n
"
;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
DWORD dwFlags
=
INTERNET_FLAG_TRANSFER_BINARY
|
INTERNET_FLAG_RELOAD
|
INTERNET_FLAG_DONT_CACHE;
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl,
1
,dwFlags,headers,strlen(headers));
//
1,dwFlags
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
char
achQueryBuf[
32
];
DWORD dwQueryBufLen
=
sizeof
(achQueryBuf);
CHttpFile
*
pHF
=
(CHttpFile
*
)httpFile;
pHF
->
QueryInfo(HTTP_QUERY_CONTENT_LENGTH, achQueryBuf,
&
dwQueryBufLen, NULL);
dwFileSize
=
(DWORD)atol(achQueryBuf);
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
1
//
头部包含afxinet.h
2 #include < afxinet.h >
3 CInternetSession::OpenURL函数原型为:
4 CStdioFile * OpenURL( LPCTSTR pstrURL, // 文件URL地址
5 DWORD dwContext = 1 , // 上下文ID
6 DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII, // 标记
7 LPCTSTR pstrHeaders = NULL, // 发送到服务器的数据头
8 DWORD dwHeadersLength = 0 ); // 发送到服务器的数据头长度
9 dwFlags可以为:
10 INTERNET_FLAG_RELOAD 强制重读数据
11 INTERNET_FLAG_DONT_CACHE 不保存到缓存
12 INTERNET_FLAG_TRANSFER_ASCII 使用文本数据
13 INTERNET_FLAG_TRANSFER_BINARY 使用二进制数据
14 // 获取网页的方法代码如下
15 CString getURLContext(LPCTSTR pstrURL)
16 {
17 // 自定义http头信息
18 char * headers = " Accept:*/*\r\n "
19 " Accept-Language:zh-cn\r\n "
20 " User-Agent:VCTestClient\r\n " ;
21 CString m_strHTML;
22 try
23 {
24 CInternetSession sess; // 建立会话
25 CHttpFile * pF = (CHttpFile * )sess.OpenURL(pstrURL, 1 ,INTERNET_FLAG_TRANSFER_ASCII || INTERNET_FLAG_RELOAD,headers,strlen(headers)); // 打开文件
26 注意:OpenURL 方法的dwFlags参数 必须选择传输模式为INTERNET_FLAG_TRANSFER_ASCII 或者 INTERNET_FLAG_TRANSFER_BINARY
27 CString szData,szAllData;
28 while (pF -> ReadString(szData))
29 {
30 // 读取文件
31 szAllData += " \r\n " ;
32 szAllData += szData;
33 }
34 pF -> Close();
35 sess.Close();
36 m_strHTML = szAllData;
37 }
38 catch (CException * e)
39 {
40 AfxMessageBox( " 获取数据失败 " );
41 }
42 return m_strHTML;
43 }
2 #include < afxinet.h >
3 CInternetSession::OpenURL函数原型为:
4 CStdioFile * OpenURL( LPCTSTR pstrURL, // 文件URL地址
5 DWORD dwContext = 1 , // 上下文ID
6 DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII, // 标记
7 LPCTSTR pstrHeaders = NULL, // 发送到服务器的数据头
8 DWORD dwHeadersLength = 0 ); // 发送到服务器的数据头长度
9 dwFlags可以为:
10 INTERNET_FLAG_RELOAD 强制重读数据
11 INTERNET_FLAG_DONT_CACHE 不保存到缓存
12 INTERNET_FLAG_TRANSFER_ASCII 使用文本数据
13 INTERNET_FLAG_TRANSFER_BINARY 使用二进制数据
14 // 获取网页的方法代码如下
15 CString getURLContext(LPCTSTR pstrURL)
16 {
17 // 自定义http头信息
18 char * headers = " Accept:*/*\r\n "
19 " Accept-Language:zh-cn\r\n "
20 " User-Agent:VCTestClient\r\n " ;
21 CString m_strHTML;
22 try
23 {
24 CInternetSession sess; // 建立会话
25 CHttpFile * pF = (CHttpFile * )sess.OpenURL(pstrURL, 1 ,INTERNET_FLAG_TRANSFER_ASCII || INTERNET_FLAG_RELOAD,headers,strlen(headers)); // 打开文件
26 注意:OpenURL 方法的dwFlags参数 必须选择传输模式为INTERNET_FLAG_TRANSFER_ASCII 或者 INTERNET_FLAG_TRANSFER_BINARY
27 CString szData,szAllData;
28 while (pF -> ReadString(szData))
29 {
30 // 读取文件
31 szAllData += " \r\n " ;
32 szAllData += szData;
33 }
34 pF -> Close();
35 sess.Close();
36 m_strHTML = szAllData;
37 }
38 catch (CException * e)
39 {
40 AfxMessageBox( " 获取数据失败 " );
41 }
42 return m_strHTML;
43 }
-------------------------------------------------------------------------------------------------------------------------------------
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT CBMPDlg::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
::GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
char
*
headers
=
"
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*
"
"
Accept-Encoding: gzip, deflate\r\n
"
"
Accept-Language: zh-CN\r\n
"
"
Content-Type: application/x-www-form-urlencoded\r\n
"
"
Cache-Control: no-cache\r\n
"
;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
DWORD dwFlags
=
INTERNET_FLAG_TRANSFER_BINARY
|
INTERNET_FLAG_RELOAD
|
INTERNET_FLAG_DONT_CACHE;
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl,
1
,dwFlags,headers,strlen(headers));
//
1,dwFlags
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
char
achQueryBuf[
32
];
DWORD dwQueryBufLen
=
sizeof
(achQueryBuf);
CHttpFile
*
pHF
=
(CHttpFile
*
)httpFile;
pHF
->
QueryInfo(HTTP_QUERY_CONTENT_LENGTH, achQueryBuf,
&
dwQueryBufLen, NULL);
dwFileSize
=
(DWORD)atol(achQueryBuf);
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
//
显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic(
char
*
lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp
=
GetDC(hWnd);
IPicture
*
pPic;
IStream
*
pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
//
读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session(
"
HttpClient
"
);
CFile
*
httpFile
=
(CFile
*
)session.OpenURL(lpstrImgUrl);
if
(httpFile
!=
INVALID_HANDLE_VALUE)
{
dwFileSize
=
httpFile
->
GetLength();
//
获取文件字节数
if
(dwFileSize
==
0xFFFFFFFF
)
return
E_FAIL;
}
else
{
return
E_FAIL;
}
//
分配全局存储空间
HGLOBAL hGlobal
=
GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData
=
NULL;
if
(hGlobal
==
NULL)
return
E_FAIL;
if
((pvData
=
GlobalLock(hGlobal))
==
NULL)
//
锁定分配内存块
return
E_FAIL;
//
把文件读入内存缓冲区
dwByteRead
=
httpFile
->
Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE,
&
pStm);
//
装入图形文件
bResult
=
OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID
*
)
&
pPic);
if
(FAILED(bResult))
return
E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth;
//
图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight;
//
图片的真实高度, 单位为英寸
pPic
->
get_Width(
&
hmWidth);
pPic
->
get_Height(
&
hmHeight);
//
转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int
nWidth
=
MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),
2540
);
int
nHeight
=
MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),
2540
);
//
将图形输出到屏幕上(有点像BitBlt)
bResult
=
pPic
->
Render(hDC_Temp,
0
,
0
,nWidth,nHeight,
0
,hmHeight,hmWidth,
-
hmHeight,NULL);
pPic
->
Release();
httpFile
->
Close();
//
关闭打开的文件
if
(SUCCEEDED(bResult))
{
return
S_OK;
}
else
{
return
E_FAIL;
}
}
HRESULT CBMPDlg::ShowPic( char * lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp = ::GetDC(hWnd);
IPicture * pPic;
IStream * pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
char * headers = " Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */* "
" Accept-Encoding: gzip, deflate\r\n "
" Accept-Language: zh-CN\r\n "
" Content-Type: application/x-www-form-urlencoded\r\n "
" Cache-Control: no-cache\r\n " ;
// 读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session( " HttpClient " );
DWORD dwFlags = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE;
CFile * httpFile = (CFile * )session.OpenURL(lpstrImgUrl, 1 ,dwFlags,headers,strlen(headers)); // 1,dwFlags
if (httpFile != INVALID_HANDLE_VALUE)
{
char achQueryBuf[ 32 ];
DWORD dwQueryBufLen = sizeof (achQueryBuf);
CHttpFile * pHF = (CHttpFile * )httpFile;
pHF -> QueryInfo(HTTP_QUERY_CONTENT_LENGTH, achQueryBuf, & dwQueryBufLen, NULL);
dwFileSize = (DWORD)atol(achQueryBuf);
if (dwFileSize == 0xFFFFFFFF )
return E_FAIL;
}
else
{
return E_FAIL;
}
// 分配全局存储空间
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData = NULL;
if (hGlobal == NULL)
return E_FAIL;
if ((pvData = GlobalLock(hGlobal)) == NULL) // 锁定分配内存块
return E_FAIL;
// 把文件读入内存缓冲区
dwByteRead = httpFile -> Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, & pStm);
// 装入图形文件
bResult = OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID * ) & pPic);
if (FAILED(bResult))
return E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth; // 图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight; // 图片的真实高度, 单位为英寸
pPic -> get_Width( & hmWidth);
pPic -> get_Height( & hmHeight);
// 转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX), 2540 );
int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY), 2540 );
// 将图形输出到屏幕上(有点像BitBlt)
bResult = pPic -> Render(hDC_Temp, 0 , 0 ,nWidth,nHeight, 0 ,hmHeight,hmWidth, - hmHeight,NULL);
pPic -> Release();
httpFile -> Close(); // 关闭打开的文件
if (SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}
HRESULT Utils::ShowPic( char * lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp = GetDC(hWnd);
IPicture * pPic;
IStream * pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
// 读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session( " HttpClient " );
CFile * httpFile = (CFile * )session.OpenURL(lpstrImgUrl);
if (httpFile != INVALID_HANDLE_VALUE)
{
dwFileSize = httpFile -> GetLength(); // 获取文件字节数
if (dwFileSize == 0xFFFFFFFF )
return E_FAIL;
}
else
{
return E_FAIL;
}
// 分配全局存储空间
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData = NULL;
if (hGlobal == NULL)
return E_FAIL;
if ((pvData = GlobalLock(hGlobal)) == NULL) // 锁定分配内存块
return E_FAIL;
// 把文件读入内存缓冲区
dwByteRead = httpFile -> Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, & pStm);
// 装入图形文件
bResult = OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID * ) & pPic);
if (FAILED(bResult))
return E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth; // 图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight; // 图片的真实高度, 单位为英寸
pPic -> get_Width( & hmWidth);
pPic -> get_Height( & hmHeight);
// 转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX), 2540 );
int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY), 2540 );
// 将图形输出到屏幕上(有点像BitBlt)
bResult = pPic -> Render(hDC_Temp, 0 , 0 ,nWidth,nHeight,
0 ,hmHeight,hmWidth, - hmHeight,NULL);
pPic -> Release();
httpFile -> Close(); // 关闭打开的文件
if (SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}
// 显示图片, lpstrImgUrl 为图片URL地址,hWnd 为窗口句柄
HRESULT Utils::ShowPic( char * lpstrImgUrl,HWND hWnd)
{
HDC hDC_Temp = GetDC(hWnd);
IPicture * pPic;
IStream * pStm;
BOOL bResult;
DWORD dwFileSize,dwByteRead;
// 读取网页上图片文件,实际是个CHttpFile指针
CInternetSession session( " HttpClient " );
CFile * httpFile = (CFile * )session.OpenURL(lpstrImgUrl);
if (httpFile != INVALID_HANDLE_VALUE)
{
dwFileSize = httpFile -> GetLength(); // 获取文件字节数
if (dwFileSize == 0xFFFFFFFF )
return E_FAIL;
}
else
{
return E_FAIL;
}
// 分配全局存储空间
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
LPVOID pvData = NULL;
if (hGlobal == NULL)
return E_FAIL;
if ((pvData = GlobalLock(hGlobal)) == NULL) // 锁定分配内存块
return E_FAIL;
// 把文件读入内存缓冲区
dwByteRead = httpFile -> Read(pvData,dwFileSize);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, & pStm);
// 装入图形文件
bResult = OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID * ) & pPic);
if (FAILED(bResult))
return E_FAIL;
OLE_XSIZE_HIMETRIC hmWidth; // 图片的真实宽度, 单位为英寸
OLE_YSIZE_HIMETRIC hmHeight; // 图片的真实高度, 单位为英寸
pPic -> get_Width( & hmWidth);
pPic -> get_Height( & hmHeight);
// 转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX), 2540 );
int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY), 2540 );
// 将图形输出到屏幕上(有点像BitBlt)
bResult = pPic -> Render(hDC_Temp, 0 , 0 ,nWidth,nHeight,
0 ,hmHeight,hmWidth, - hmHeight,NULL);
pPic -> Release();
httpFile -> Close(); // 关闭打开的文件
if (SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}