GDI+ from C++ windows basic example
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
#pragma comment( lib, "gdiplus.lib" )
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
// attach a console
AllocConsole() ;
AttachConsole( GetCurrentProcessId() ) ;
freopen( "CON", "w", stdout ) ;
// Start up GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
WNDCLASS wndClass;
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("GDIPLUSWINDOW");
if(!RegisterClass(&wndClass)){ puts( "problem registering wndclass" ) ; }
HWND hWnd;
hWnd = CreateWindow(
TEXT("GDIPLUSWINDOW"), // window class name
TEXT("Welcome to GDI+!"), // window caption
WS_OVERLAPPEDWINDOW, // window style
20, // initial x position
20, // initial y position
640, // initial width
480, // initial height
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_PAINT:
{
// "Start painting"
// The HDC "handle to a device context"
// is the 'surface' on which you draw.
// You don't draw directly to a window.
// Rather you draw to a "device context".
// The reason its this way is it makes it so
// you can draw to ANY SURFACE
hdc = BeginPaint(hWnd, &ps);
#pragma region painting code
// Create a Graphics object for our window's hdc
Graphics g( hdc ) ;
// Create a pen.
Pen bluePen( Color( 128, 0, 0, 255 ) ) ;
bluePen.SetWidth( 8.0 ) ;
g.DrawLine( &bluePen, 0, 0, 100, 100 ) ;
g.DrawEllipse( &bluePen, 0, 0, 40, 40 ) ;
g.DrawEllipse( &bluePen, 0, 0, 50, 50 ) ;
SolidBrush blueBrush( Color( 120, 0, 0, 255 ) ) ;
g.FillRectangle( &blueBrush, 60, 60, 190, 180 ) ;
SolidBrush redBrush( Color( 123, 255, 0, 0 ) ) ;
Font arialFont( TEXT("Arial"), 12.0 ) ;
PointF pTextPos( 20, 20 );
g.DrawString( TEXT("This is GDI+!"),
strlen( "This is GDI+!" ),
&arialFont, pTextPos, &redBrush ) ;
g.FillRectangle( &redBrush, 30, 30, 100, 180 ) ;
#pragma endregion
// "Wrap it up"
EndPaint( hWnd, &ps );
printf( "Your window has been painted\n" ) ;
}
return 0;
case WM_LBUTTONDOWN:
{
UpdateWindow(hWnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc
Advertisement