GDI+ (Graphics Device Interface Plus) is a graphics programming interface in Windows that provides a wide array of functions for drawing shapes, text, and images. Here's a basic guide to help you get started with GDI+ in C++.
Setting Up Your Environment
- Compiler/IDE: Use Microsoft Visual Studio for a smooth experience with GDI+. It's well-integrated for Windows development.
- Linking Libraries: Ensure that you have linked the required GDI+ libraries. You generally need to include
Gdiplus.lib.
Basic GDI+ Example
Below is a simple example demonstrating how to set up GDI+ and draw a basic shape (a rectangle) and some text.
-
Include Necessary Headers:
Make sure to include the GDI+ header.
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
-
Initialize GDI+:
You need to initialize GDI+ before using its functions.
-
Create a Window:
Create a standard Windows application that will serve as the rendering surface.
-
Drawing with GDI+:
Use GDI+ to perform drawing operations.
Here's a complete example in C++:
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
using namespace Gdiplus;
// Global Variables
HINSTANCE hInst;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_PAINT: {
// Create a graphics object
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Graphics graphics(hdc);
// Draw a rectangle
Pen pen(Color(255, 0, 0, 255)); // Red pen
graphics.DrawRectangle(&pen, 10, 10, 200, 100);
// Draw some text
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
SolidBrush brush(Color(255, 0, 0, 0)); // Black brush
graphics.DrawString(L"Hello, GDI+!", -1, &font, PointF(10, 120), &brush);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
GdiplusShutdown(gdiplusToken);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// WinMain Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
hInst = hInstance;
// Initialize GDI+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Register Window Class
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.lpszClassName = L"GDIPlusExample";
RegisterClass(&wc);
// Create Window
HWND hwnd = CreateWindowEx(
0,
L"GDIPlusExample",
L"GDI+ Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInst, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Message Loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Explanation of the Example
-
Headers and Library: The example starts with including windows.h and gdiplus.h, followed by linking to Gdiplus.lib.
-
Window Procedure: The WndProc function processes messages sent to the window. In the WM_PAINT case, a rectangle and string are drawn.
-
WinMain: Initializes GDI+, registers a window class, creates a window, and runs a message loop.
Compiling the Code
Make sure to configure your project settings in Visual Studio:
- Go to Project Properties -> Linker -> Input -> Additional Dependencies, and add
Gdiplus.lib.
- Ensure you're targeting a Windows subsystem in your project settings.
Getting More Familiar
Explore the GDI+ documentation and try drawing various geometrical shapes, working with images, and applying transformations (like rotation and scaling) to increase your familiarity with the API.
Feel free to ask if you have specific questions or need further details!