The article demonstrates how to create an animation with palette. As we all know, palette is outdated, but there are still many places where it is used. If you didn't know more about palette, go to search online to check out more about it. It's so cool that when you add some animation to your project, it could attract other's eyeballs. Of course, this code is very simple. You can change it in any way as per your wish to make it more useful and stronger.
I had nothing to do, so I wrote this code. I love Codeproject.com, but I feel that I found it very late.
Using the Code
It's quite easy to use this code:
- Add the two class files to your project, "PalAnimClass.h" and "PalAnimClass.cpp".
- Call its
Init() to finish initialization.
- Now, call its
UpdateDate() frequently. I call this function once every 30ms. You can alter the value and adjust your computer. For this reason, no handle of window was passed into the class, so that we must use another timer to loop it. Let's take a look at the definition of the class:
BOOL SetType(int nType);
BOOL SetDirect(BOOL dir);
BOOL SetColor(int r,int g,int b);
BOOL SetSize(int iWidth,int iHeight);
void UpdateData(HDC hdc);
void DrawBackground(HDC hdc,int type);
void TimerRoutine(HPALETTE hPalette);
public methods were found.
GetDirection() is used to get the direction of movement. Actually it's a
BOOL date type, indacates the movement to left or to right, in or out. We use this method to get the value of movement and to use another method
SetDirect() to set a new value.
SetType() is used to set the type of animations. The scope of animations' type is 0~6. Be sure that is not out of bound.
SetColor(): Three parameters should be passed into, they are
BLUE colors. The scope of each of them is 0~255, just like the macro RGB.
SetSize() is used to set the size of animation. The default size is 300X300.
Init() is used for initialization.
The principle of palette animation is really very easy. There are only three steps in total.
- Create a palette. The palette is special, we will talk about it later.
- Draw shapes with the colors in the palette which we created just now.
- We change the value of
So easy, isn't it? Let's start implementing it.
Create a special palette:
plp->palVersion =0x300; 0
plp->palPalEntry [i].peRed =m_r*sin(PI*i/64);
plp->palPalEntry [i].peBlue =m_b*sin(PI*i/64);
plp->palPalEntry [i].peFlags =PC_RESERVED;
Using the method we create a palette, now we draw shapes in the background. Remember to use the colors in the palette which we just created. This step is easy, look:
hBrush = CreateSolidBrush (PALETTEINDEX (min(64*sin(PI*j/64),64*sin(PI*i/64)))) ;
PALETTERGB to use the color in our new palette. Why are there so many sine functions here, that's just a math problem. You could understand it well. After painting our shapes in the background, now let's go to alter the value of palette. The snippets are as follows:
void PalAnimClass::TimerRoutine(HPALETTE hPalette)
AnimatePalette function replaces entries in the specified logical palette.
HPALETTE hpal, e
UINT iStartIndex, e
UINT cEntries, e
CONST PALETTEENTRY *ppe t
An application can determine whether a device supports palette operations by calling the
GetDeviceCaps function and specifying the
AnimatePalette function only changes entries with the
PC_RESERVED flag set in the corresponding
palPalEntry member of the
If the given palette is associated with the active window, the colors in the palette are replaced immediately.
Getting to the start now. It's your turn, try to write more interesting code. Sorry about my shortcomings. Any questions, add me to your ICQ chat.
Points of Interest
I found that I became more and more patient and efficient. Thanks to everyone's help! Thanks again! CodeProject helped me improve!^.^