Thread: [Dev-C++] brush gets modified
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Carlos d. M. <cg...@wo...> - 2002-08-17 22:31:24
|
I am writing a program which displays a series of bitmaps, placed such
that white is transparent, and it shall be displayed over a red
background.
The bitmap mask is generated by the program in a way specified at the
Forger's win32 programming tutorial, so I select the color bitmap in a
first memory device context (created with createcompatiblebitmap [I omit
the upper case only in the messages, not in the code]) to which I select
the color bitmap, I create a second bitmap with the color bitmap
dimensions and select it in a second memory DC, the bitmap is monochrome
with 1 depth and correct, then I bitblt from second to first with
SRCCOPY, then from first to second with SRCINVERT, then I bitblt from
second to the window, and bitblt from a previous memory device in which
I select the color image (I select it now) to the window, so the image
is set with white as transparent color to the window. And befora all
this I selected a brush.
I have an object with a member which is HBRUSH hbrush, in its
constructor I set it to red with createsolidbrush(RGB(255, 0, 0));,
then I have a function which is HBRUSH getBrush(){return hbrush} (It is
not inline but I write it this way in the message, so I don't spend
writing the code exactly, but I spend this explanation :)),
so before all that, I write brush = object.getBrush(); (object is a
static variable in WindowProcedure).
So, the image shall be displayed over a red background, and it is true
for the first three (perhaps one, two or four) bitmaps (the full amount
of bitmaps is sixteen, divided in two sets of eight), but after that the
background gets white. It doesn't happen if I substitute in windowproc
brush = object.getBrush(); with brush = createsolidbrush(RGB(255, 0,
0));.
Here is the code of the WM_TIMER: (where all these things happen)
The comments are added in the message.
case WM_TIMER:
{
RECT memRct;
SHORT l, r, t, b, f;
UINT pel;
UINT lucas;
HBRUSH hbrush, holdb;
SetLastError(0);
DWORD err;
long pol;
char *buffer, *point;
SetLastError(0);
if((err = GetLastError()) != 0){
fstream outf;
outf.open("D:/Devpro/NewF/Fail.txt", ios::out);
outf << err;
outf.close();
MessageBox(hwnd, "Init error", "ERROR", MB_OK |
MB_ICONEXCLAMATION);
}
HDC temp1, temp2;
BITMAP bm, ms, cr;
HBITMAP hdef, con2;
GetClientRect(hwnd, &rct);
InvalidateRect(hwnd, &rct, TRUE);
hdc = GetDC(hwnd); // HDC FOR THE WINDOW
col += 5;
if(col > 255)
col = 0;
l = GetAsyncKeyState(VK_LEFT);
r = GetAsyncKeyState(VK_RIGHT);
l >>= (sizeof(SHORT) - 1);
r >>= (sizeof(SHORT) - 1); //SELECT SERIES OF BITMAPS
AND POSITION
t = GetAsyncKeyState(VK_UP);
b = GetAsyncKeyState(VK_DOWN);
t >>= (sizeof(SHORT) - 1);
b >>= (sizeof(SHORT) - 1);
f = GetAsyncKeyState(VK_CONTROL);
f >>= (sizeof(SHORT) - 1);
if(l != 0){
cord.x-=10;
curb.move(0);
}
if(r != 0){
cord.x+=10;
curb.move(1);
}
if(t != 0)
cord.y -= 10;
if(b != 0)
cord.y += 10;
useBit = curb.getBit(); //CURB SELECTS THE BITMAP AND
USEBIT STORES IT
hbrush = /*CreateSolidBrush(RGB(255,0,
0));*/curb.getBrush();//THE TWO VERIONS FOR HBRUSH TOGETHER, NOW WORKING
WITH THE BRUSH RETURNED BY THE OBJECT'S METHOD
holdb = (HBRUSH)SelectObject(hdc, (HGDIOBJ)hbrush);//SELECT
THE BRUSH AND STORE THE DEFAULT BRUSH
FillRect(hdc, &rct, hbrush);//FILL THE RECT WITH THE BRUSH,
IT SHALL DO IT ALWAYS BUT IT SEEMS TO FAIL WITH OBJECT'S BRUSH
SelectObject(hdc, holdb);//RESTORE DEFAULT BRUSH TO HDC AND
PREPARE DELETION OF THE CREATED ONE
DeleteObject(hbrush);
hdcMem = CreateCompatibleDC(hdc);//THE PREVIOUS MEMORY DEVICE
FOR COLOR BITMAP
temp2 = CreateCompatibleDC(hdc);//THE SECOND BITMAP FOR
CREATING THE MASK
con2 = (HBITMAP)SelectObject(temp2,
(HGDIOBJ)useBit);//hBitMap[(con % 7) + 1]);//SELECT THE COLOR BITMAP IN
THE SECOND MEMORY DEVICE, NOTICE IT HAS NOT BEEN SELECTED IN THE
PREVIOUS
GetObject(useBit, sizeof(bm), &bm);//OBTAIN DIMENSIONS OF THE
BITMAP
hBitMask = CreateBitmap(bm.bmWidth, bm.bmHeight,1, 1,
NULL);//CREATE THE MONOCHROME
temp1 = CreateCompatibleDC(hdc);//CREATE THE FIRST MEMORY
DEVICE FOR THE MASK
hdef = (HBITMAP)SelectObject(temp1,
(HGDIOBJ)hBitMask);//SELECT THE BITMAP MASK IN THE FIRST
SetBkColor(temp2, RGB(255, 255, 255));//SELECT WHICH COLOR
FROM THE COLOR BITMAP WILL BE THE TRANSPARENT
BitBlt(temp1, 0, 0, bm.bmWidth, bm.bmHeight, temp2, 0, 0,
SRCCOPY);//COLOR BITMAP TO THE MONOCHROME, SO THE PARTS THAT WE WANT
BLACK WILL BE WHITE, AND THE ONES WE WANT WHITE ARE BLACK
BitBlt(temp2, 0, 0, bm.bmWidth, bm.bmHeight, temp1, 0, 0,
SRCINVERT);//FROM MONOCHROME TO COLOR, SO NOW WHAT WE WANT BLACK IS
BLACK AND WHAT WE WAN WHITE IS WHITE
SelectObject(temp2, (HGDIOBJ)con2);RESTORE DEFAULT BITMAP (IS
A 1*1 WHITE PIXEL)
DeleteDC(temp2);
hdefbit = (HBITMAP)SelectObject(hdcMem,
(HGDIOBJ)useBit);//hBitMap[(con % 7) + 1]);
BitBlt(hdc, cord.x, cord.y, bm.bmWidth, bm.bmHeight, temp1,
0, 0, SRCAND);
BitBlt(hdc, cord.x, cord.y, bm.bmWidth, bm.bmHeight, hdcMem,
0, 0, SRCPAINT);//HERE I GET A LITTLE CONFUSED THE FIRST BITBLT WITH
TEMP1?? BUT IF I CHANGE TO TEMP2 IT DOESN'T WORK (OR I FORGOT SOME
CHANGE DOING IT), THE WHITE AREAS OF THE BITMAP ARE THEN BLACK.
SelectObject(temp1, (HGDIOBJ)hdef);
DeleteObject(hBitMask);
DeleteDC(temp1);
SelectObject(hdcMem, (HGDIOBJ)hdefbit);
DeleteDC(hdcMem);
DeleteObject(useBit);
ReleaseDC(hwnd, hdc);
con++;
ValidateRect(hwnd, &rct);
bool res = false;
res = curb.trial(hInst);
if(res == false)
MessageBox(hwnd, "hInst failed", "Error!", MB_OK);
}
break;
I don't achieve to see any reason for that behavior, don't find my
error. Perhaps, that use of hbrush in objects is not correct?
Thanks in advance, and sorry if stupid message.
Yes, I suppose many things (other than the error which leads to the
color) can be improved, and I'll do that, now I am just in the beginning
of the experiment, doing things in the right first place, later I'll put
many of these things inside the object and more adequate places.
|