Code:
//Filename: src.cpp
//Simple GDI+ example. Ken Fitlike
#include <windows.h> //include all the basics
#include <gdiplus.h> //requires linking with GdiPlus.lib
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR lpCmdLine,int nCmdShow)
{
HWND hwnd;
MSG Msg;
TCHAR chClassName[]=TEXT("SIMPLEWND");
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW|CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC)WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInst;
wcx.hIcon =(HICON)LoadImage(0, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
wcx.hCursor =(HCURSOR)LoadImage(0, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wcx.hbrBackground =(HBRUSH)(COLOR_BTNFACE+1);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = chClassName;
wcx.hIconSm = NULL;
//Register wnd
if (!RegisterClassEx(&wcx))
{
//Registration failure
MessageBox( NULL,
TEXT("Failed to register wnd class"),
TEXT("ERROR"),
MB_OK|MB_ICONERROR);
return FALSE;
}
hwnd=CreateWindowEx(0,
chClassName,
TEXT("Simple GDI+ Example"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
GetSystemMetrics(SM_CXSCREEN)/4,
GetSystemMetrics(SM_CYSCREEN)/4,
GetSystemMetrics(SM_CXSCREEN)/2,
GetSystemMetrics(SM_CYSCREEN)/2,
NULL,
NULL,
hInst,
NULL);
if (!hwnd)
{
//wnd creation failure
MessageBox( NULL,
TEXT("Failed to create wnd"),
TEXT("ERROR"),
MB_OK|MB_ICONERROR);
return FALSE;
}
Gdiplus::GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL); //init gdi+
//start message loop
while (GetMessage(&Msg,NULL,0,0)>0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Gdiplus::GdiplusShutdown(gdiplusToken); //release gdi+
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
WCHAR wcTxt[]=L"Simple GDI+";
BeginPaint(hwnd,&ps);
Gdiplus::Graphics *gs=new Gdiplus::Graphics(ps.hdc);
//create an underlined font
Gdiplus::Font myFont(L"system",14,Gdiplus::FontStyleUnderline);
Gdiplus::Font fntBlack(L"system",12);
Gdiplus::PointF origin(100.0f, 100.0f);
Gdiplus::SolidBrush blackBrush(Gdiplus::Color(255,0,0,255));
gs->DrawString(wcTxt,lstrlenW(wcTxt),&myFont,origin,&blackBrush);
delete gs;
EndPaint(hwnd,&ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
If you can't get the the above code to work then your include/lib paths are screwed or you haven't added the gdiplus.lib to 'project->settings->link'.