Why cant I compile a simple GDI+ example with Visual Studio 2008 Express C++?
I copied the following simple GDI+ example into Visual Studio 2008 Express C++
//#define UNICODE
#include
"stdafx.h"#include
<gdiplus.h>using
namespace Gdiplus;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 nameTEXT(
"Getting Started"), // window captionWS_OVERLAPPEDWINDOW,
// window styleCW_USEDEFAULT,
// initial x positionCW_USEDEFAULT,
// initial y positionCW_USEDEFAULT,
// initial x sizeCW_USEDEFAULT,
// initial y sizeNULL,
// parent window handleNULL,
// window menu handlehInstance,
// program instance handleNULL);
// creation parametersShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;}
// WinMainLRESULT 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);}
}
// WndProcAnd when I compile I get a bunch of errors starting with:
1>------ Build started: Project: GDITest, Configuration: Debug Win32 ------
1>Compiling...
1>GDITest.cpp
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2440: 'initializing' : cannot convert from 'const char [37]' to 'int'
1> There is no context in which this conversion is possible
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2146: syntax error : missing ';' before identifier 'IImageBytes'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2470: 'IImageBytes' : looks like a function definition, but there is no parameter list; skipping apparent body
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2059: syntax error : 'public'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(280) : error C2146: syntax error : missing ';' before identifier 'id'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(280) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(280) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(384) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(395) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(405) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(443) : error C2061: syntax error : identifier 'PROPID'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(444) : error C2061: syntax error : identifier 'PROPID'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(445) : error C2061: syntax error : identifier 'PROPID'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(453) : error C2061: syntax error : identifier 'PROPID'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(471) : error C2535: 'Gdiplus::Image::Image(void)' : member function already defined or declared
1> c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(384) : see declaration of 'Gdiplus::Image::Image'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(505) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(516) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(758) : error C2061: syntax error : identifier 'IStream'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusheaders.h(813) : error C2061: syntax error : identifier 'IStream'
Am I missing something really obvious here?
Thanks,
Answers
- Edit stdafx.h and add this line:
#include <windows.h>
All Replies
- Edit stdafx.h and add this line:
#include <windows.h> #include
<objbase.h>This header is included by a lot of stuff, such as the DirectX libs, and eventually includes the necessary stuff. This should be considered a bug I think. The <gdiplus.h> lib should include it itself.
- No. Any Windows project should #include <windows.h> first. Proper include order is important.
- comment this line in stdafx.h
#define WIN32_LEAN_AND_MEAN
will resovle this problem - Thx!!!
really helpful, it happen with some many program ported to VS2008

