类头文件里添加如下成员变量:
GdiplusStartupInput m_gdiplusStartupInput;
//
gdi+用到的两个变量
ULONG_PTR m_pGdiToken;
U_CHAR m_cVerifyCodeImage[
2048
];
//
验证码图片数据
U_SHORT m_sVerifyCodeImageLen;
//
验证码图片数据长度
GdiplusStartupInput m_gdiplusStartupInput;
//
gdi+用到的两个变量
ULONG_PTR m_pGdiToken;
U_CHAR m_cVerifyCodeImage[
2048
];
//
验证码图片数据
U_SHORT m_sVerifyCodeImageLen;
//
验证码图片数据长度
类构造函数和析造函数里添加如下初始化和反初始化GDI
+
的代码:
//
类构造函数里添加这句:
GdiplusStartup(
&
m_pGdiToken,
&
m_gdiplusStartupInput,NULL);
//
装载gdi+
//
类析构函数里添加这句:
GdiplusShutdown(m_pGdiToken);
//
卸载gdi+
//
类构造函数里添加这句:
GdiplusStartup(
&
m_pGdiToken,
&
m_gdiplusStartupInput,NULL);
//
装载gdi+
//
类析构函数里添加这句:
GdiplusShutdown(m_pGdiToken);
//
卸载gdi+
类实现代码里添加如下函数代码:
//
显示验证码图片
int
CQQClient::ShowVerifyCodeImage(HWND hWnd,
int
x,
int
y)
{
if
(hWnd
==
NULL
||
m_sVerifyCodeImageLen
<=
0
||
m_sVerifyCodeImageLen
>=
sizeof
(m_cVerifyCodeImage))
return
-
1
;
HDC hDC
=
::GetDC(hWnd);
HGLOBAL hMem
=
::GlobalAlloc(GMEM_MOVEABLE,m_sVerifyCodeImageLen);
if
(hMem
==
NULL)
{
return
-
1
;
}
LPVOID lpMem
=
::GlobalLock(hMem);
if
(lpMem
==
NULL)
{
::GlobalFree(hMem);
return
-
1
;
}
memcpy(lpMem,m_cVerifyCodeImage,m_sVerifyCodeImageLen);
::GlobalUnlock(hMem);
IStream
*
lpStream
=
NULL;
::CreateStreamOnHGlobal(hMem,TRUE,
&
lpStream);
Image
*
lpImage
=
Gdiplus::Image::FromStream(lpStream);
Graphics clsGraph(hDC);
clsGraph.DrawImage(lpImage,x,y,
130
,
53
);
//
QQ验证码图片宽高固定为(130,53)
lpStream
->
Release();
::GlobalFree(hMem);
ReleaseDC(hWnd,hDC);
return
0
;
}
//
显示验证码图片
int
CQQClient::ShowVerifyCodeImage(HWND hWnd,
int
x,
int
y)
{
if
(hWnd
==
NULL
||
m_sVerifyCodeImageLen
<=
0
||
m_sVerifyCodeImageLen
>=
sizeof
(m_cVerifyCodeImage))
return
-
1
;
HDC hDC
=
::GetDC(hWnd);
HGLOBAL hMem
=
::GlobalAlloc(GMEM_MOVEABLE,m_sVerifyCodeImageLen);
if
(hMem
==
NULL)
{
return
-
1
;
}
LPVOID lpMem
=
::GlobalLock(hMem);
if
(lpMem
==
NULL)
{
::GlobalFree(hMem);
return
-
1
;
}
memcpy(lpMem,m_cVerifyCodeImage,m_sVerifyCodeImageLen);
::GlobalUnlock(hMem);
IStream
*
lpStream
=
NULL;
::CreateStreamOnHGlobal(hMem,TRUE,
&
lpStream);
Image
*
lpImage
=
Gdiplus::Image::FromStream(lpStream);
Graphics clsGraph(hDC);
clsGraph.DrawImage(lpImage,x,y,
130
,
53
);
//
QQ验证码图片宽高固定为(130,53)
lpStream
->
Release();
::GlobalFree(hMem);
ReleaseDC(hWnd,hDC);
return
0
;
}
以上代码使用到了GDI
+
,所以需要包含相应的GDI
+
头文件,和连接相应的GDI
+
库。类头文件之前添加如下代码:
#ifndef ULONG_PTR
#define
ULONG_PTR unsigned long *
#include
"
.\\GDIPlus\\Includes\\GdiPlus.h
"
using
namespace
Gdiplus;
#pragma
comment(lib, ".\\GDIPlus\\Lib\\gdiplus.lib")
#endif