http://blog.csdn.net/cashey1991/article/details/7407425
http://blog.csdn.net/zhongbin104/article/details/8730935
CXXXApp 头文件中添加:
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
private:
ULONG_PTR m_gdiplusToken;
CXXXApp 源文件构造函数中添加:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
CXXXApp 源文件析构函数中添加:
Gdiplus::GdiplusShutdown(m_gdiplusToken);
整个工程都可以使用GDI++了,不需要每个类中都添加一遍
画png资源:
BOOL ImageFromIDResource(UINT nID, LPCTSTR sTR, Gdiplus::Image *&pImg) {
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nID), sTR); // type
if(!hRsrc) return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if(!lpRsrc) return FALSE;
// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, lpRsrc, len);
GlobalUnlock(m_hMem);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
// load from stream
pImg = Gdiplus::Image::FromStream(pstm);
// free/release stuff
pstm->Release();
FreeResource(lpRsrc);
GlobalFree(m_hMem);
return TRUE;
}
绘制中添加:
1.画png
Button, Picture Control 改为 Owner Draw 类型,
Button 不能实现半透效果,但是能显示图形,可以使用 SolidBrush 来填充背景,实现假透明
Picture 可以实现半透,但是,默认不响应鼠标点击事件,可以添加 PretranslateMessage 或者 OnLButtonDown 之类的方法来处理点击事件
点击事件中添加
CPoint ptCursor;
GetCursorPos(&ptCursor);
CRect rc;
GetDlgItem(IDC_???)->GetWindowRect(&rc);
if(rc.PtInRect(ptCursor)) {
TRACE(L"LButtonDown\n");
}
OnDrawItem 中添加
CRect rect = lpDrawItemStruct->rcItem;
Graphics g(lpDrawItemStruct->hDC);
Image *pimage; // Construct an image
ImageFromIDResource(IDB_PNG_CLOSE, _T("PNG"), pimage);
g.DrawImage(pimage, 0, 0, rect.Width(), rect.Height());
2.图形或文字
Gdiplus::SolidBrush brush(0);
Graphics g(lpDrawItemStruct->hDC);
Gdiplus::GraphicsPath path;
g.SetSmoothingMode(SmoothingModeAntiAlias);
brush.SetColor(Gdiplus::Color(0xff, 0xff, 0x00, 0x00));//背景圆
path.Reset();
path.AddPie(0, 0, 20, 20, 0, 360);
g.FillPath(&brush, &path);
brush.SetColor(Gdiplus::Color(0x00, 0xbc, 0xd4));
Gdiplus::FontFamily fontFamily(L"Arial");
Gdiplus::Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
StringFormat format;//设置水平垂直居中
format.SetAlignment(StringAlignmentCenter);
format.SetLineAlignment(StringAlignmentCenter);
Gdiplus::PointF origin(20, 20);
g.DrawString(L"hao", -1, &font, origin, &format, &brush);