NO

Author Topic: Why GDIplus dont work?  (Read 2305 times)

Offline bitcoin

  • Member
  • *
  • Posts: 179
Why GDIplus dont work?
« on: April 15, 2021, 01:52:30 AM »
Hello, I try to code gdi plus, and this no draw anything! And no errors..why?

Code: [Select]
#include <windows.h>
#include <fgdiplusflat.h>

#pragma comment (lib,"Gdiplus.lib")


VOID OnPaint(HDC hdc)
{
GpStatus g_ok;
Graphics graphics;
Pen pen;

TextOut(hdc, 0, 0, "Hello, Windows!", 15);

g_ok = GdipCreateFromHDC(hdc,&graphics);

if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipCreateFromHDC",MB_OK);
return;
}

g_ok = GdipCreatePen1(MakeARGB(255, 0, 0, 255),1.0,UnitInch,&pen);

if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipCreatePen1",MB_OK);
return;
}
g_ok = GdipDrawLine(graphics,pen, 20, 20, 200, 100);
if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipDrawLine",MB_OK);
return;
}
GdipDrawRectangle(graphics, pen, 20, 20, 120, 120);
GdipDeletePen(pen);
     GdipDeleteGraphics(graphics);


}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE h, PSTR ss, INT iCmdShow)
{
HWND                hWnd;
MSG                 msg;
WNDCLASS            wndClass;
GdiplusStartupInput gdiplusStartupInput = { 1, NULL, 0, 0 };
ULONG_PTR           gdiplusToken;

// Initialize GDI+.
if(GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL))
MessageBoxW(0,L"Err GdiplusStartup",0,0);

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);
if(!hdc)
MessageBoxW(0,L"Err",NULL,0);
OnPaint(hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Why GDIplus dont work?
« Reply #1 on: April 15, 2021, 07:02:16 AM »
Try UnitPixel instead
May the source be with you

Offline bitcoin

  • Member
  • *
  • Posts: 179
Re: Why GDIplus dont work?
« Reply #2 on: April 15, 2021, 10:39:20 AM »
Yes, it works. Thanks Timo!

But why? When I type 'Inch', it draw in another place?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Why GDIplus dont work?
« Reply #3 on: April 15, 2021, 04:24:38 PM »
GDI+ is tricky. You may want read this first.

To draw something using a different unit you must set the page transformation, using GdipSetPageUnit(), then print using world units, or pixel units.
See the sample.

Code: [Select]
VOID OnPaint(HDC hdc)
{
GpStatus g_ok;
Graphics graphics;
Pen pen;

TextOut(hdc, 0, 0, "Hello, Windows!", 15);

g_ok = GdipCreateFromHDC(hdc,&graphics);

if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipCreateFromHDC",MB_OK);
return;
}

g_ok = GdipSetPageUnit(graphics, UnitInch);
if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipSetPageUnit",MB_OK);
return;
}

g_ok = GdipCreatePen1(MakeARGB(255, 0, 0, 255), 1.0, UnitWorld,&pen);
if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipCreatePen1",MB_OK);
return;
}

g_ok = GdipDrawLine(graphics,pen, 2, 2, 4, 4);
if(g_ok != Ok)
{
WCHAR errCode[4];
wsprintfW(errCode,L"%d",g_ok);
MessageBoxW(0,errCode,L"GdipDrawLine",MB_OK);
return;
}
GdipDrawRectangle(graphics, pen, 2, 2, 4, 4);
GdipDeletePen(pen);
     GdipDeleteGraphics(graphics);
}
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide