Disegno di una linea
In questo argomento viene illustrato come disegnare una linea usando GDI Plus.
Per disegnare una linea in Windows GDI+ è necessario un oggetto Graphics, un oggetto Pen e un oggetto Color. L'oggetto Graphics fornisce il metodo DrawLine e l'oggetto Pen contiene attributi della linea, ad esempio colore e larghezza. L'indirizzo dell'oggetto Penna viene passato come argomento al metodo DrawLine.
Il programma seguente, che disegna una linea da (0, 0) a (200, 100), è costituito da tre funzioni: WinMain, WndProce OnPaint. Le funzioni di WinMain e WndProc forniscono il codice fondamentale comune alla maggior parte delle applicazioni Windows. Nessun codice GDI+ nella funzione WndProc. La funzione WinMain ha una piccola quantità di codice GDI+, ovvero le chiamate necessarie a GdiplusStartup e GdiplusShutdown. Il codice GDI+ che effettivamente crea un oggetto Graphics e disegna una linea si trova nella funzione OnPaint.
La funzione OnPaint riceve un handle a un contesto di dispositivo e passa tale handle a un costruttore Graphics. L'argomento passato al costruttore Penna è un riferimento a un oggetto Color. I quattro numeri passati al costruttore di colori rappresentano i componenti alfa, rosso, verde e blu del colore. Il componente alfa determina la trasparenza del colore; 0 è completamente trasparente e 255 è completamente opaco. I quattro numeri passati al metodo DrawLine rappresentano il punto iniziale (0, 0) e il punto finale (200, 100) della linea.
#include <stdafx.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 200, 100);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT("GettingStarted");
RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("GettingStarted"), // window class name
TEXT("Getting Started"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;
} // WinMain
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc
Si noti la chiamata a GdiplusStartup nella funzione WinMain. Il primo parametro della funzione GdiplusStartup è l'indirizzo di un ULONG_PTR. GdiplusStartup riempie tale variabile con un token che viene successivamente passato alla funzione GdiplusShutdown. Il secondo parametro della funzione GdiplusStartup è l'indirizzo di una struttura GdiplusStartupInput. Il codice precedente si basa sul costruttore predefinito GdiplusStartupInput per impostare i membri della struttura in modo appropriato.