[C\C++] - about flicker(or something like it)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
i have these code for move an image and works:#include <conio.h>
#include <conio2.h>
#include <stdio.h>
#include <windows.h>


void DrawBitmap(HDC hdcDest, char* filename, int x, int y);
void DrawTransparentBitmap(HDC hdcDest, char *filename, int x, int y, int width, int height, COLORREF a );
void apidoevents();

HWND foco;
HDC a;

int main()
{
foco = GetForegroundWindow();
a=GetDC( foco);
HBRUSH b;
HPEN c;
SetWindowPos(foco, 0, 100, 100, 500, 500, 0);
int x=0;
int y=0;


SetWindowText(foco, "Draw image and rectangle with brush and pen sample");
fflush(stdin);
do
{

clrscr();
backcolor(RED);
textcolor(WHITE);
//DrawBitmap(a,"C:\test\Bleu Battle.bmp",0,0);
DrawBitmap(a,"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.bmp",0,0);
DrawTransparentBitmap(a,"C:\test\Bleu Battle.bmp",x,y,100,100, -1);

c=CreatePen(PS_DOT, 1, RGB(255,0,0));
SelectObject( a, c);

b=CreateHatchBrush(HS_CROSS, RGB(255,0,0));
SelectObject( a,b );
//SelectObject( a, CreateSolidBrush(RGB(0,255,0)));

Rectangle(a,100,0,150,50);
//RedrawWindow(foco,NULL, NULL, RDW_UPDATENOW);
//apidoevents();
getch();
if (GetAsyncKeyState (VK_LEFT) & 0x8000)
x=x-1;
else if (GetAsyncKeyState (VK_RIGHT) & 0x8000)
x=x+1;
else if (GetAsyncKeyState (VK_UP) & 0x8000)
y=y-1;
else if (GetAsyncKeyState (VK_DOWN) & 0x8000)
y=y+1;

}
while(!(GetAsyncKeyState (VK_RETURN) & 0x8000));
DeleteObject (c);
DeleteObject (b);
fflush(stdin);
getch();
ReleaseDC(foco,a);
return 0;
}

void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
{
HBITMAP image;
BITMAP bm;
HDC hdcMem;
image = (HBITMAP)LoadImage(0, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(image, sizeof(BITMAP), &bm);
hdcMem = CreateCompatibleDC(hdcDest);

SelectObject(hdcMem, image);

BitBlt(
hdcDest,
x, y,
bm.bmWidth, bm.bmHeight,
hdcMem,
0, 0,
SRCCOPY);

/*StretchBlt (hdcDest,
x, y,
300, 300,
hdcMem,
0, 0,
bm.bmWidth, bm.bmHeight, SRCCOPY);*/

DeleteDC(hdcMem);
DeleteObject((HBITMAP)image);
}

void DrawTransparentBitmap(HDC hdcDest, char *filename, int x, int y, int width, int height, COLORREF a )
{
//posiçoes no ciclo for
int posx=0;
int posy=0;

//para comparar os pixels
COLORREF b;

//a imagem normal
HBITMAP image;
BITMAP bm;
HDC hdcMem;

//a mascara da imagem
HBITMAP imagemask;
BITMAP bmmask;
HDC hdcMemmask;



//criar 1 imagem normal do ficheiro
image = (HBITMAP)LoadImage(0, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(image, sizeof(BITMAP), &bm);
hdcMem = CreateCompatibleDC(hdcDest);
SelectObject(hdcMem, image);

//criar 1 imagem normal do ficheiro para fazer a mascara
imagemask= (HBITMAP)LoadImage(0, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(imagemask, sizeof(BITMAP), &bmmask);
hdcMemmask = CreateCompatibleDC(hdcDest);
SelectObject(hdcMemmask, imagemask);

//if a is -1, isso quer dizer que o pixel 0,0 - Automatico
if (a==-1) a=GetPixel(hdcMemmask,0,0);

if (width==0) width=bm.bmWidth;
if (height==0) height=bm.bmHeight;
//se a cor b for igual á cor a(cor que tem de ser transparente)
//a cor da mascara tem de ser branca
//caso contrario é preta
for (posy=0;posy<bm.bmHeight;posy++)
{
for (posx=0;posx<bm.bmWidth ;posx++)
{
b=GetPixel(hdcMemmask,posx,posy);
if (b==a)
SetPixel(hdcMemmask,posx,posy,RGB(255,255,255));
else
SetPixel(hdcMemmask,posx,posy,RGB(0,0,0));
}
}

//esta parte copia a imagem com a mask(fazendo a transparencia)
//1 - bitbltstretchblt com bitmap vbSrcInvert
//1 - bitbltstretchblt com bitmap mask vbSrcAnd
//1 - bitbltstretchblt com bitmap vbSrcInvert
/*BitBlt(
hdcDest,
x, y,
bm.bmWidth, bm.bmHeight,
hdcMem,
0, 0,
SRCINVERT);
BitBlt(
hdcDest,
x, y,
bm.bmWidth, bm.bmHeight,
hdcMemmask,
0, 0,
SRCAND);
BitBlt(
hdcDest,
x, y,
bm.bmWidth, bm.bmHeight,
hdcMem,
0, 0,
SRCINVERT);*/

//SetStretchBltMode(hdcDest,STRETCH_ANDSCANS);
StretchBlt (hdcDest,
x, y,
width, height,
hdcMem,
0, 0,
bm.bmWidth, bm.bmHeight,SRCINVERT);
StretchBlt (hdcDest,
x, y,
width, height,
hdcMemmask,
0, 0,
bm.bmWidth, bm.bmHeight, SRCAND);
StretchBlt (hdcDest,
x, y,
width, height,
hdcMem,
0, 0,
bm.bmWidth, bm.bmHeight, SRCINVERT);

//temos de apagar os DCs e os objectos

//quando usamos CreateCompatibleDC() temos de usar o DeleteDC()
//para apagar o resultado do CreateCompatibleDC()
DeleteDC(hdcMem);
DeleteDC(hdcMemmask);

//quando usamos SelectObject() temos de usar o DeleteObject()
//para apagar o resultado do DeleteObject()
//neste caso o resultado de LoadImage()
DeleteObject((HBITMAP)image);
DeleteObject((HBITMAP)imagemask);
}
see the main() function, in do..while. i clean the screen and show the images. but 1 image is showed by a diferent positions(using the directional keys). but why i see the flicker?
why isnt showed normaly?
is about my DrawTransparentBitmap() function(dont return a value, but in C stills be a function) using for?

View the full article
 
Back
Top