GDI+ 的初始化,GDI初始化
昨天看見同學做的畫圖軟體,有圖層的效果,於是乎自己也來配置GDI+的環境,明天貼上自己學習GDI的常見操作
環境VS2013 updata3
建立的是win32 視窗項目
#include <gdiplus.h>using namespace Gdiplus;#pragma comment (lib, "Gdiplus.lib")
連結庫裡添加
gdiplus.lib
申明全域變數
GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;
初始化操作
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Q:編譯出現了錯誤
W:需要去掉stdafx.h標頭檔裡“WIN32_LEAN_AND_MEAN”
// Init.cpp : 定義應用程式的進入點。// #include "stdafx.h"#include "Init.h"<span style="color:#ff0000;">#include <gdiplus.h>using namespace Gdiplus;#pragma comment (lib, "Gdiplus.lib")</span>#define MAX_LOADSTRING 100 // 全域變數: HINSTANCE hInst;// 當前執行個體TCHAR szTitle[MAX_LOADSTRING];// 標題列文本TCHAR szWindowClass[MAX_LOADSTRING];// 主視窗類名<span style="color:#ff0000;">GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;</span>// 此代碼模組中包含的函數的前向聲明: ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);VOID OnPaint(HDC hdc){Graphics graphics(hdc);//畫線Pen pen(Color(255, 0, 0, 255));graphics.DrawLine(&pen, 0, 0, 200, 100);}int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代碼。MSG msg;HACCEL hAccelTable; // 初始化全域字串LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_INIT, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)){return FALSE;} hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_INIT)); // 主訊息迴圈: while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}} return (int) msg.wParam;} //// 函數: MyRegisterClass()//// 目的: 註冊視窗類別。//ATOM MyRegisterClass(HINSTANCE hInstance){<span style="color:#ff0000;">GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);</span>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_INIT));wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_INIT);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex);} //// 函數: InitInstance(HINSTANCE, int)//// 目的: 儲存執行個體控制代碼並建立主視窗//// 注釋: //// 在此函數中,我們在全域變數中儲存執行個體控制代碼並// 建立和顯示主程式視窗。//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ HWND hWnd; hInst = hInstance; // 將執行個體控制代碼儲存在全域變數中 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;} //// 函數: WndProc(HWND, UINT, WPARAM, LPARAM)//// 目的: 處理主視窗的訊息。//// WM_COMMAND- 處理應用程式菜單// WM_PAINT- 繪製主視窗// WM_DESTROY- 發送退出訊息並返回////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);// 分析菜單選擇: 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:hdc = BeginPaint(hWnd, &ps);// TODO: 在此添加任意繪圖代碼...OnPaint(hdc);EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;} // “關於”框的訊息處理常式。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;}