#define GDIPVER 0x0110 //定义高版本的GDI+(1.1) #include <windows.h> #include <ObjIdl.h> #include <stdio.h> #include <GdiPlus.h> #pragma comment(lib,"GdiPlus.lib") using namespace Gdiplus; VOID OnPaint(HDC hDC) { //在此处添加重绘代码 } LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_PAINT: { PAINTSTRUCT PaintStruct; HDC hDC = BeginPaint(hWnd,&PaintStruct); OnPaint(hDC); EndPaint(hWnd,&PaintStruct); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam, lParam); break; } return LRESULT(0); } INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT) { //初使化GDI+ ---------------------------------------- ULONG_PTR GdiplusToken; GdiplusStartupInput GdiplusStartupInput; Status sResult = GdiplusStartup(&GdiplusToken, &GdiplusStartupInput, NULL); if(sResult != Ok)return 0; WNDCLASSEX WndClassEx = { sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WinProc, 0L, 0L, GetModuleHandle(NULL), LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL,IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), NULL, _T("SimpleWindowClass"), LoadIcon(NULL, IDI_APPLICATION) }; RegisterClassEx(&WndClassEx); HWND hWnd = CreateWindow( _T("SimpleWindowClass"), _T("Beacon Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, GetDesktopWindow(), NULL, WndClassEx.hInstance, NULL); ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG Msg; do { GetMessage(&Msg, NULL, 0U, 0U); TranslateMessage(&Msg); DispatchMessage(&Msg); }while(Msg.message != WM_QUIT); UnregisterClass( _T("SimpleWindowClass"), WndClassEx.hInstance); //关闭GDI+ ---------------------------------------- GdiplusShutdown(GdiplusToken); return 0; } VOID OnPaint(HDC hDC) { Graphics g(hDC); /* //加载和显示图元文件 Image i(L"Image.wmf"); g.DrawImage(&i,60,60); */ //记录图元文件 Metafile m(L"SampleMetafile.emf"); { Graphics gm(&m); //在括号中使程序离开括号即销毁gm Pen p(Color(255,0,255,0)); SolidBrush sb(Color(255,0,0,255)); //绘制矩形和椭圆 gm.DrawRectangle(&p,Rect(50,10,25,75)); gm.DrawEllipse(&p,Rect(100,10,25,75)); //以指定的平滑模式绘制椭圆 gm.SetSmoothingMode(SmoothingModeHighQuality); //设置滤波模式(抗锯齿) gm.DrawEllipse(&p,Rect(150,10,25,75)); //绘制字符串 FontFamily ff(L"Arial"); Font f(&ff,24,FontStyleRegular,UnitPixel); gm.SetTextRenderingHint(TextRenderingHintAntiAlias); //设置字体渲染模式 gm.RotateTransform(30.0f); //旋转坐标系 gm.DrawString(L"Smooth Text",-1,&f,PointF(50.0f,50.0f),&sb); } g.DrawImage(&m,200,100); } /* enum QualityMode { QualityModeInvalid = -1, QualityModeDefault = 0, QualityModeLow = 1, QualityModeHigh = 2 }; enum SmoothingMode //滤波模式 { SmoothingModeInvalid = QualityModeInvalid, //保留 SmoothingModeDefault = QualityModeDefault, //不使用 SmoothingModeHighSpeed = QualityModeLow, //低 SmoothingModeHighQuality = QualityModeHigh, //高 SmoothingModeNone, //不使用 SmoothingModeAntiAlias, //8x4抗锯齿 #if (GDIPVER >= 0x0110) SmoothingModeAntiAlias8x4 = SmoothingModeAntiAlias, //8x4抗锯齿 SmoothingModeAntiAlias8x8 //8x8抗锯齿 #endif //(GDIPVER >= 0x0110) }; enum TextRenderingHint //字体渲染微调 { TextRenderingHintSystemDefault = 0, //使用默认微调 TextRenderingHintSingleBitPerPixelGridFit, //使用微调 TextRenderingHintSingleBitPerPixel, //不使微调 TextRenderingHintAntiAliasGridFit, //使用抗锯齿且使用微调 TextRenderingHintAntiAlias, //使用抗锯齿且不使用微调 TextRenderingHintClearTypeGridFit //使用ClearType模式 }; #include <afxwin.h> class CMFCApp: public CWinApp { public: virtual BOOL InitInstance(); }g_theApp; class CMainFrame: public CFrameWnd { public: CMainFrame(); private: DECLARE_MESSAGE_MAP() public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); private: CDC m_memDC; CBitmap m_memBitmap; public: afx_msg void OnTimer(UINT_PTR nIDEvent); }; BOOL CMFCApp::InitInstance() { m_pMainWnd = new CMainFrame(); //创建一个新的框架 m_pMainWnd->ShowWindow(SW_SHOW); //显示窗口 m_pMainWnd->UpdateWindow(); //刷新窗口 return TRUE; } CMainFrame::CMainFrame() { //创建窗口 Create( NULL, //窗口类名 _T("Simple MFC Window"), //窗口标题 WS_OVERLAPPEDWINDOW, //窗口样式 CRect(0,0,640,480)); //窗口矩形 } BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE() ON_WM_TIMER() END_MESSAGE_MAP() int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; CRect rcClient; GetClientRect(rcClient); CDC * pDC = this->GetDC(); if( !m_memDC.CreateCompatibleDC(pDC) ) ::PostQuitMessage( 0 ); m_memBitmap.CreateCompatibleBitmap( pDC, rcClient.Width(), rcClient.Height() ); m_memDC.SelectObject( &m_memBitmap ); SetTimer( 0, 50, NULL ); ReleaseDC( pDC ); return 0; } void CMainFrame::OnTimer(UINT_PTR nIDEvent) { static int nRadius = 0; ++nRadius; if( nRadius > 15 ) nRadius = 0; CDC * pPaintDC = GetDC(); CRect rcClient; GetClientRect(rcClient); m_memDC.FillSolidRect( &rcClient, RGB(0xFF,0xFF,0xFF) ); for(int x(0); x < rcClient.Width(); x += 16 ) { for(int y(0); y < rcClient.Height(); y += 16 ) { m_memDC.Ellipse( x, y, x + nRadius, y + nRadius ); } } pPaintDC->BitBlt( 0, 0, rcClient.Width(), rcClient.Height(), &m_memDC, 0, 0, SRCCOPY ); ReleaseDC( pPaintDC ); CFrameWnd::OnTimer(nIDEvent); }