OK, I've figured that one out for myself. Now I've got another problem with blitting:
I have a struct:
Code:
struct blitArea
{
HWND hostHWnd;
BITMAPINFO bmp;
DWORD* pBits;
HBITMAP hBmp;
HDC hdcMem;
PAINTSTRUCT ps;
};
Initialised by:
Code:
void blitInit (blitArea *area, int width, int height)
{
area->bmp.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
area->bmp.bmiHeader.biWidth = width;
area->bmp.bmiHeader.biHeight = height;
area->bmp.bmiHeader.biPlanes = 1;
area->bmp.bmiHeader.biBitCount = 32;
area->bmp.bmiHeader.biCompression = BI_RGB;
area->hBmp = CreateDIBSection (NULL, &area->bmp, DIB_RGB_COLORS, (void **) &(area->pBits),
NULL, 0xff0fffff);
area->hdcMem = CreateCompatibleDC (NULL);
SelectObject (area->hdcMem, area->hBmp);
}
In:
Code:
LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hdc = NULL;
static blitArea area;
static square *s;
switch (msg)
{
case WM_CREATE:
blitInit (&area, WIN_WIDTH, WIN_HEIGHT);
area.hostHWnd = hwnd;
s = new square (&area, 200, 200);
break;
case WM_PAINT:
hdc = GetDC (hwnd);
blit (&area, 0, 0, WIN_WIDTH, WIN_HEIGHT, 0, 0);
ReleaseDC (hwnd, hdc);
hdc = NULL;
break;
case WM_LBUTTONDOWN:
hdc = GetDC (hwnd);
if (hdc)
{
xp = LOWORD (lParam);
yp = HIWORD (lParam);
s->draw ();
ReleaseDC (hwnd, hdc);
hdc = NULL;
}
break;
I have a class called square that is supposed to draw a white rectangle to the screen with its own method called draw ():
Code:
class square
{
private:
blitArea *a;
int x, y;
public:
square (blitArea *ar, int newX, int newY);
void draw (void);
};
square::square (blitArea *ar, int newX, int newY)
{
a = ar;
x = newX;
y = newY;
};
void square::draw (void)
{
HDC hdc = NULL;
drawRect (a->pBits, x, y, 50, 50, 0xffffffff);
hdc = GetDC (a->hostHWnd);
blit (a, 0, 0, 700, 500, 0, 0);
ReleaseDC (a->hostHWnd, hdc);
hdc = NULL;
};
Please note, that this is simply a contrived example to illustrate the error I'm experiencing - the actual code is rather large, so I'm sparing you the hassle.
If I call the square's draw method in the WM_CREATE handler, the subsequent blit () call in the WM_PAINT handler will cause the square to be drawn (naturally).
But if I wanted to make the square only draw itself when you click the mouse (as seen in the WM_LBUTTONDOWN handler), the square will not appear straight away - you have to cause a WM_PAINT event (like moving the window out of the screen and back).
The square's draw method calls the blit function itself but it has no effect! Only blit () calls from MainWindowProc seem to have any effect.