Sample code appreciated. Thank you.file on screen, using win32 MFC ?
Sample code appreciated. Thank you.
Below is a win32 api way to fade-in a bitmap on screen which I wrote several
years ago. It Should be easy to adapt it to MFC. Basicly it generates a
sequence of RGB 'bitmaps' in real time and blits them to the screen. The
routines were written with 16/24 bit bitmaps and display depths in mind. When
fading bitmaps with just a few colors, it's probably faster (and possible) to
use the pallet to do the fading. If the method used below is too slow,
consider using a pixel for pixel 'fake' fade. This can be done efficiently
using PatBlt/BitBlt and several PatternBrushes.
Pieter.
//////////////////////
#include <windows.h>
#include <math.h>
#define FADESTEP 256 // Nr of steps the fade process is divided into
BYTE FadeTable[FADESTEP][256]; // Lookup table for RGB scaling
//////////////////////////////////////////
// MakeFadeTable
/////////////////////////////////////////
// Calculates a lookuptable for every fadestep 0..FADESTEP-1. Call once before
// using the FadeBitmap set of functions
void MakeFadeTable(void)
{
int i, j;
for (j=0;j<FADESTEP;j++) {
for (i=0;i<=255;i++) {
FadeTable[j][i]=(BYTE)((i*j)/FADESTEP);
}
}
}
/////////////////////////////////////////////////////////////////////
// FadeBitmap
/////////////////////////////////////////////////////////////////////
int FadeBitmap(HDC hdc, HBITMAP hBitmap, int x, int y, DWORD InSpeed)
// Fades bitmap hBitmap in device context hdc at position x, y in with speed
// InSpeed. InSpeed is the time in ms the fadein process should take.
{
int CurStep;
int k;
int nScanLine;
BITMAPINFO bmi;
BYTE *lpvBits, *lpvBitsSrc;
register BYTE *pbFT, *pbS, *pbD, *pbSE;
DWORD dwSize;
DWORD dwStartTime, dwTime;
HBITMAP hDIB, hDIBSrc;
InSpeed/=FADESTEP;
// Get hBitmap attributes
bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biBitCount=0;
// Fill BITMAPINFO structure
if(GetDIBits(hdc, hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS)==0)
return 1; // Error
// Get bitmapbits in RGB format.
dwSize= ((((bmi.bmiHeader.biWidth * 24) + 31)& ~31) >> 3) *
bmi.bmiHeader.biHeight; // Set bitmapsize in bytes
bmi.bmiHeader.biBitCount=24; // Set bitsize we want
bmi.bmiHeader.biSizeImage=dwSize;
bmi.bmiHeader.biCompression=BI_RGB;
// Get memory for the image
hDIB=CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &lpvBits, NULL, 0);
if (hDIB==NULL)
return 1; //Create DIB section failed
hDIBSrc=CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &lpvBitsSrc,
NULL, 0);
if (hDIBSrc==NULL)
{ //alloc failed
DeleteObject(hDIB);
return 1;
}
// Copy image to the Source buffer in RGB format
nScanLine=GetDIBits(hdc, hBitmap, 0, bmi.bmiHeader.biHeight,
lpvBitsSrc, &bmi, DIB_RGB_COLORS); // Get bitmap bits
// Fade in
dwStartTime=GetTickCount();
if (InSpeed!=0)
CurStep=0;
else
CurStep=FADESTEP;
while (CurStep<FADESTEP)
{
// Create pointers so the compiler can put them into registers
pbFT=FadeTable[CurStep]; // Start of the fade table for
// this iteration
pbS=lpvBitsSrc; // Source image
pbD=lpvBits; // Dest buffer
pbSE=pbS+dwSize; // End of the source image
while(pbS<pbSE)
*pbD++=*(pbFT+*pbS++); // Seems easiest to optimize
// for the compiler...
// Copy DIB from buffer to screen
SetDIBitsToDevice(hdc, x, y, bmi.bmiHeader.biWidth, nScanLine,
0, 0, 0, nScanLine, lpvBits, &bmi,
DIB_PAL_COLORS);
dwTime=GetTickCount();
if (dwTime<=dwStartTime+InSpeed*CurStep)
{
Sleep(dwStartTime+InSpeed*CurStep-dwTime);
CurStep++;
}
else
CurStep=((dwTime-dwStartTime)/InSpeed);
}
// Free resources
DeleteObject(hDIB);
DeleteObject(hDIBSrc);
return 0;
}