1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
using std::wostringstream;
using std::ostringstream;
#ifndef HINST_THISCOMPONENT
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#endif
// The SafeRelease Pattern
HRESULT CreateGraphics(HWND hWnd);
void DestroyGraphics(void);
// Programmer defined macro to make using the known colors easier.
#define D2DColor(clr) D2D1::ColorF(D2D1::ColorF::clr)
template <class T> void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
// DirectX Interfaces:
ID2D1Factory* pD2DFactory = NULL;
ID2D1HwndRenderTarget* pRT = NULL;
ID2D1SolidColorBrush *pBrush = NULL;
ID2D1LinearGradientBrush *pGradient = NULL;
ID2D1Bitmap *pBitmap = NULL;
IWICImagingFactory *pWIC = NULL;
D2D1_COLOR_F custom = D2DColor(CornflowerBlue); // Color used when clearing the background.
D2D1_POINT_2F mouse = D2D1::Point2F(0,0); // Position used to demonstrate clicking.
IDWriteFactory* pDWFactory = NULL;
IDWriteTextFormat* pTF = NULL;
// Forward Declarations:
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
HRESULT LoadBitmapFromFile(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
);
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
WNDCLASSEX wcex = {};
wcex.cbSize = sizeof(wcex);
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hInstance = hInstance;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WindowProc; // identify which function will process messages
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = MAINWINCLASS;
RegisterClassEx(&wcex); // register the window
HWND hWnd = CreateWindow(wcex.lpszClassName, // handle to the window
MAINWINTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
916,
800,
HWND_DESKTOP,
NULL,
hInstance,
NULL);
if(!hWnd) // ensure the window was created successfully
{
MessageBox(HWND_DESKTOP,
ERRORMESSAGE1,
MAINWINTITLE,
MB_OK | MB_ICONERROR);
return -1;
}
CreateGraphics(hWnd); // causes an access right violation
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg = { };
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CoUninitialize();
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
case WM_CREATE:
{
HRESULT hr = CreateGraphics(hWnd);
if (FAILED(hr))
{
return -1;
}
}
break;
case WM_DESTROY:
DestroyGraphics();
PostQuitMessage(0);
break;
case WM_PAINT:
Paint(hWnd);
break;
case WM_LBUTTONDOWN:
LButtonClick(hWnd, wParam, lParam);
break;
case WM_COMMAND:
OnCommand(hWnd, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
HRESULT CreateGraphics(HWND hWnd)
{
// Initialize the Direct2D Factory.
HRESULT hr;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
if (FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the Direct2D Factory."),
_T("Direct2D Error"), MB_OK | MB_ICONERROR);
return hr;
}
// Get the dimensions of the client.
RECT rc;
GetClientRect(hWnd, &rc);
// Initialize a Direct2D Size Structure.
D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);
// Create the Direct2D Render Target.
hr = pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hWnd, size), &pRT);
if (FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the Direct2D Render Target."),
_T("Direct2D Error"), MB_OK | MB_ICONERROR);
return hr;
}
pRT->SetDpi(96.0f, 96.0f);
// Create the Direct2D Solid Color Brush.
hr = pRT->CreateSolidColorBrush(D2D1::ColorF(0x0), &pBrush);
if (FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the Direct2D Solid Color Brush."),
_T("Direct2D Error"), MB_OK | MB_ICONERROR);
return hr;
}
ID2D1GradientStopCollection *pGradientStops = NULL;
D2D1_GRADIENT_STOP gradientStops[2];
gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Red, 1);
gradientStops[0].position = 0.0f;
gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::Black, 1);
gradientStops[1].position = 1.0f;
// Create the ID2D1GradientStopCollection from a previously
// declared array of D2D1_GRADIENT_STOP structs.
hr = pRT->CreateGradientStopCollection(
gradientStops,
ARRAYSIZE(gradientStops),
D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE_CLAMP,
&pGradientStops
);
if (SUCCEEDED(hr))
{
hr = pRT->CreateLinearGradientBrush(
D2D1::LinearGradientBrushProperties(
D2D1::Point2F(0, 600),
D2D1::Point2F(0, 800)),
D2D1::BrushProperties(),
pGradientStops,
&pGradient
);
}
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_LOCAL_SERVER,
IID_PPV_ARGS(&pWIC)
);
// Create a bitmap by loading it from a file.
hr = LoadBitmapFromFile(
pRT,
pWIC,
L"space.bmp",
0,
0,
&pBitmap
);
// Initialize the DirectWrite Factory.
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
(IUnknown**)&pDWFactory);
if (FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the DirectWrite Factory."),
_T("DirectWrite Error"), MB_OK | MB_ICONERROR);
return hr;
}
// Create the DirectWrite Text Format.
hr = pDWFactory->CreateTextFormat(_T("Veranda"), NULL,
DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
24, _T(""), &pTF);
if (FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the DirectWrite Text Format."),
_T("DirectWrite Error"), MB_OK | MB_ICONERROR);
return hr;
}
return S_OK;
}
HRESULT LoadBitmapFromFile(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if(FAILED(hr))
{
MessageBox(HWND_DESKTOP, _T("ERROR: Failed to Create the iWIC factory."),
_T("DirectWrite Error"), MB_OK | MB_ICONERROR);
}
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);}
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}
SafeRelease(&pDecoder);
SafeRelease(&pSource);
SafeRelease(&pStream);
SafeRelease(&pConverter);
SafeRelease(&pScaler);
return hr;
}
void DestroyGraphics(void)
{
SafeRelease(&pTF);
SafeRelease(&pBitmap);
SafeRelease(&pWIC);
SafeRelease(&pGradient);
SafeRelease(&pBrush);
SafeRelease(&pDWFactory);
SafeRelease(&pRT);
SafeRelease(&pD2DFactory);
}
|