views:

323

answers:

1

I'm trying to create a Gradient Brush in windows mobile as follows:

HBITMAP hBitmap = CreateBitmap(16, 16, 1, 16, NULL);
HDC hDC = CreateCompatibleDC(NULL);
HBITMAP hPrevious = SelectObject(hDC, hBitmap);
TRIVERTEX vert[2];
GRADIENT_RECT gRect;
//... fill in vert and gRect
GradientFill(hDC, vert, 2, &gRect, 1, GRADIENT_FILL_RECT_V);
SelectObject(hDC, hPrevious);
Delete(hDC);

HBRUSH hPatternBrush = CreatePatternBrush(hBitmap);
HDC hDC = BeginPaint(hWnd, &ps);
SelectObject(hDC, hPatternBrush);
RoundRect(hDC, ...);
EndPaint(hWND, &ps);

Draw the bitmap without pattern brush

HDC hdcSrc = CreateCompatibleDC(NULL);
HGDIOBJ hbmOld = SelectObject(hdcSrc, hBitmap);
BitBlt(hDC, ..., hdcSrc, ..., SRCCOPY);
SelectObject(hdcSrc, hbmOld);
DeleteDC(hdcSrc);

This code create a round rect with a black background, not the pattern brush. I can draw the hBitmap which is used to create the brush and it draws the gradient. Anyone got a solution?

A: 

Create a DIB.

Replace

HBITMAP hBitmap = CreateBitmap(16, 16, 1, 16, NULL);
HDC hDC = CreateCompatibleDC(NULL);

with

HDC hDC = CreateCompatibleDC(NULL);
BITMAPINFO BitmapInfo;
memset(&BitmapInfo, 0, sizeof(BITMAPINFOHEADER));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = 16;
BitmapInfo.bmiHeader.biHeight = 16;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 16;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP hBitmap = CreateDIBSection(hDC, &BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
Bill
That works now but I now get the gradient starting half way into the rect I'm drawing, not from the top of the rect as I would of thought.
mtopley
@mtopley What you need is SetBrushOrgEx(). Check http://msdn.microsoft.com/en-us/library/dd162967%28VS.85%29.aspx
Bill
The documentation says the default origin is top left, which is what I want. Tried using that function to center the brush and made no difference, I've decided to use images now instead of gradient brush.
mtopley