// Create a pattern brush
HRESULT DemoApp::CreateGridPatternBrush(
	ID2D1RenderTarget *pRenderTarget,
	ID2D1BitmapBrush **ppBitmapBrush)
{
	HRESULT hr = S_OK;

	// Create a compaitble render target
	ID2D1BitmapRenderTarget *pCompatibleRenderTarget = NULL;
	hr = pRenderTarget->CreateCompatibleRenderTarget(D2D1::SizeF(10.f, 10.f), &pCompatibleRenderTarget);
	if (SUCCEEDED(hr))
	{
		// Draw a pattern
		ID2D1SolidColorBrush *pGridBrush = NULL;
		hr = pCompatibleRenderTarget->CreateSolidColorBrush(
			D2D1::ColorF(D2D1::ColorF(0.93f, 0.94f, 0.96f, 1.0f)),
			&pGridBrush);
		if (SUCCEEDED(hr))
		{
			pCompatibleRenderTarget->BeginDraw();
			pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.0f, 10.0f, 1.0f), pGridBrush);
			pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.1f, 1.0f, 10.0f), pGridBrush);
			pCompatibleRenderTarget->EndDraw();

			// Retrieve the bitmap from the render target
			ID2D1Bitmap *pGridBitmap = NULL;
			hr = pCompatibleRenderTarget->GetBitmap(&pGridBitmap);
			if (SUCCEEDED(hr))
			{
				// Choose the filling mode for the bitmap brush
				D2D1_BITMAP_BRUSH_PROPERTIES brushProperties =
					D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP);

				// Create the bitmap brush
				hr = m_pRenderTarget->CreateBitmapBrush(pGridBitmap, brushProperties, ppBitmapBrush);

				pGridBitmap->Release();
			}

			pGridBrush->Release();
		}

		pCompatibleRenderTarget->Release();
	}

	return hr;
}
Пример #2
0
// this imitates http://blogs.msdn.com/b/wpfsdk/archive/2006/10/26/uncommon-dialogs--font-chooser-and-color-picker-dialogs.aspx
static void drawGrid(ID2D1RenderTarget *rt, D2D1_RECT_F *fillRect)
{
	D2D1_SIZE_F size;
	D2D1_PIXEL_FORMAT pformat;
	ID2D1BitmapRenderTarget *brt;
	D2D1_COLOR_F color;
	D2D1_BRUSH_PROPERTIES bprop;
	ID2D1SolidColorBrush *brush;
	D2D1_RECT_F rect;
	ID2D1Bitmap *bitmap;
	D2D1_BITMAP_BRUSH_PROPERTIES bbp;
	ID2D1BitmapBrush *bb;
	HRESULT hr;

	// mind the divisions; they represent the fact the original uses a viewport
	size.width = 100 / 10;
	size.height = 100 / 10;
	// yay more ABI bugs

	pformat = rt->GetPixelFormat();

	hr = rt->CreateCompatibleRenderTarget(&size, NULL,
		&pformat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
		&brt);
	if (hr != S_OK)
		logHRESULT(L"error creating render target for grid", hr);

	brt->BeginDraw();

	color.r = 1.0;
	color.g = 1.0;
	color.b = 1.0;
	color.a = 1.0;
	brt->Clear(&color);

	color = D2D1::ColorF(D2D1::ColorF::LightGray, 1.0);
	ZeroMemory(&bprop, sizeof (D2D1_BRUSH_PROPERTIES));
	bprop.opacity = 1.0;
	bprop.transform._11 = 1;
	bprop.transform._22 = 1;
	hr = brt->CreateSolidColorBrush(&color, &bprop, &brush);
	if (hr != S_OK)
		logHRESULT(L"error creating brush for grid", hr);
	rect.left = 0;
	rect.top = 0;
	rect.right = 50 / 10;
	rect.bottom = 50 / 10;
	brt->FillRectangle(&rect, brush);
	rect.left = 50 / 10;
	rect.top = 50 / 10;
	rect.right = 100 / 10;
	rect.bottom = 100 / 10;
	brt->FillRectangle(&rect, brush);
	brush->Release();

	hr = brt->EndDraw(NULL, NULL);
	if (hr != S_OK)
		logHRESULT(L"error finalizing render target for grid", hr);
	hr = brt->GetBitmap(&bitmap);
	if (hr != S_OK)
		logHRESULT(L"error getting bitmap for grid", hr);
	brt->Release();

	ZeroMemory(&bbp, sizeof (D2D1_BITMAP_BRUSH_PROPERTIES));
	bbp.extendModeX = D2D1_EXTEND_MODE_WRAP;
	bbp.extendModeY = D2D1_EXTEND_MODE_WRAP;
	bbp.interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
	hr = rt->CreateBitmapBrush(bitmap, &bbp, &bprop, &bb);
	if (hr != S_OK)
		logHRESULT(L"error creating bitmap brush for grid", hr);
	rt->FillRectangle(fillRect, bb);
	bb->Release();
	bitmap->Release();
}