note : a demo test gdi operation on win32 project

来源:互联网 发布:ipad2更新不了软件 编辑:程序博客网 时间:2024/05/21 09:12
a demo test gdi operation on win32 project

test free gdi resource diffrent between gdi and gdi plus


vs2010 c++ project  GdiTestOnWin32-v2014-0203.rar download :

http://download.csdn.net/detail/lostspeed/6896189


Code snippets :


// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently//#pragma once#include "targetver.h"#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers// Windows Header Files:#include <windows.h>#include <atlbase.h>#include <gdiplus.h>#pragma comment(lib, "Gdiplus.lib")// C RunTime Header Files#include <stdlib.h>#include <malloc.h>#include <memory.h>#include <tchar.h>using namespace Gdiplus;// TODO: reference additional headers your program requires here

// GdiTestOnWin32.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "GdiTestOnWin32.h"#include "Function.h"#define MAX_LOADSTRING 100// Global Variables:HINSTANCE hInst;// current instanceTCHAR szTitle[MAX_LOADSTRING];// The title bar textTCHAR szWindowClass[MAX_LOADSTRING];// the main window class name// Forward declarations of functions included in this code module:ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);ULONG_PTR g_GdiplusToken = 0;int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here.MSG msg;HACCEL hAccelTable;Gdiplus::GdiplusStartupInput _GdiplusInput;Gdiplus::GdiplusStartup(&g_GdiplusToken, &_GdiplusInput, NULL);// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_GDITESTONWIN32, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// Perform application initialization:if (!InitInstance (hInstance, nCmdShow)){Gdiplus::GdiplusShutdown(g_GdiplusToken);return FALSE;}hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GDITESTONWIN32));// Main message loop:while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}Gdiplus::GdiplusShutdown(g_GdiplusToken);return (int) msg.wParam;}////  FUNCTION: MyRegisterClass()////  PURPOSE: Registers the window class.////  COMMENTS:////    This function and its usage are only necessary if you want this code//    to be compatible with Win32 systems prior to the 'RegisterClassEx'//    function that was added to Windows 95. It is important to call this function//    so that the application will get 'well formed' small icons associated//    with it.//ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GDITESTONWIN32));wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_GDITESTONWIN32);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));return RegisterClassEx(&wcex);}////   FUNCTION: InitInstance(HINSTANCE, int)////   PURPOSE: Saves instance handle and creates main window////   COMMENTS:////        In this function, we save the instance handle in a global variable and//        create and display the main program window.//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){   HWND hWnd;   hInst = hInstance; // Store instance handle in our global variable   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   if (!hWnd)   {      return FALSE;   }   ShowWindow(hWnd, nCmdShow);   UpdateWindow(hWnd);   return TRUE;}////  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)////  PURPOSE:  Processes messages for the main window.////  WM_COMMAND- process the application menu//  WM_PAINT- Paint the main window//  WM_DESTROY- post a quit message and return////LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message){case WM_COMMAND:wmId    = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_EXIT:DestroyWindow(hWnd);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:::ZeroMemory(&ps, sizeof(PAINTSTRUCT));hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...DoPaint(&hdc, &hWnd, &ps);EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}// Message handler for about box.INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE;}

/// @filefunction.cpp#include "stdafx.h"#include "Function.h"BOOL DoPaint(HDC * phdc, HWND * phWnd, PAINTSTRUCT * pPs){int iLeft = 50;int iTop = 50;int iRight = 100;int iButtom = 100;int iOffsetX = 50;int iOffsetY = 0;nsGdi::DoDrawLine(phWnd, iLeft, iTop, iRight, iButtom);nsGdiPlus::DoDrawLine(phWnd, iLeft + iOffsetX, iTop + iOffsetY, iRight + iOffsetX, iButtom + iOffsetY);DoDrawString(phWnd, L"Hello world !");return TRUE;}void DoDrawString(HWND * phWnd,WCHAR * pcText){HDChDc = ::GetDC(*phWnd);;Graphics Graph(hDc);Color colorFont = Color(255, 255, 0, 0);Color colorBrush = Color(255, 0, 0, 255);Pen pen(colorFont, 3);SolidBrush brush(colorBrush);/// on original WinXp sp3, font "微软雅黑" is invalidFontFamily family(L"宋体"); ///< if font invalid, string can't draw on phWndFont font(&family, 24, Gdiplus::FontStyleRegular, Gdiplus::UnitMillimeter);RECT rt;GetClientRect(*phWnd, &rt);PointF pt(rt.left + (rt.right - rt.left) / 2, rt.top + (rt.bottom - rt.top) / 2);Graph.DrawString(pcText, -1, &font, pt, &brush);/// free resource::ReleaseDC(*phWnd, hDc);}namespace nsGdi{void DoDrawLine(HWND * phWnd, int iLeft, int iTop, int iRight, int iBottom,int iLineWidth /*= 3*/, COLORREF color /*= RGB(255, 0, 0)*/){HPENhPenNew = NULL;HPENhPenOld = NULL;HDChDc = NULL;hPenNew = ::CreatePen(PS_SOLID, iLineWidth, color);hDc = ::GetDC(*phWnd);hPenOld = (HPEN)::SelectObject(hDc, (HGDIOBJ)hPenNew);::MoveToEx(hDc, iLeft, iTop, NULL);::LineTo(hDc, iRight, iBottom);::SelectObject(hDc, (HGDIOBJ)hPenOld);/// free resource::DeleteObject(hPenNew);::ReleaseDC(*phWnd, hDc);}};namespace nsGdiPlus{void DoDrawLine(HWND * phWnd, int iLeft, int iTop, int iRight, int iBottom, int iLineWidth /*= 3*/, Gdiplus::Color color /*= Gdiplus::Color(255, 255, 0, 0)*/){HDChDc = ::GetDC(*phWnd);;Gdiplus::Graphics Graph(hDc);Gdiplus::Pen pen(color, iLineWidth);Graph.DrawLine(&pen, iLeft, iTop, iRight, iBottom);/// free resource::ReleaseDC(*phWnd, hDc);}};


0 0
原创粉丝点击