A bitmap fade-out control

zhaozj2021-02-11  222

We often show a bitmap about the company or your own message in Aboutbox. Have you ever thought that this bitmap is more cool? For example, add fade out? As long as you have this Calphaactrl control, you can easily implement it.

CalphaCtrl is inherited from CSTATIC. When you use the CalphaActrl to add a form, then the loadalphabitmap (uint uid, int itimer) function can realize the fade out of the bitmap. Where the UID is the resource ID of the bitmap, itimer is a bitmap display time interval, and the bigity is smaller.

Here is how CalphaCtrl is implemented. A key implementation function is a WIN32 API: Alphablend, which enables the transparent display of the image, the relevant parameters, and information, please refer to MSDN, it is worth noting to use this function to be linked to the MSIMG32.lib library.

The first step, we first add a few Data Member: cbitmap bmp in the Calphaactrl class;

Bool bcanpaint; uint nbmpid; int NTIMER;

Step 2, add a Member function in the Calphaactrl class:

Void Alphadisplay (CDC & PDC, CClientDC & DC, Blendfunction & RBLENDPROPS, INT Width, Int Heigh, BYTE NLEVEL)

{

// NLEVEL is transparent, 0 means not displayed, 255 completely displayed

Rblendprops.SourceConstantalpha = NLEVEL;

Alphablend (dc.m_hdc, 0, 0, width, heigh, pdc.m_hdc, 0, 0,

Width, Heigh, RblendProps;

}

In the third step, add a global function called TDDisplay, this function is a thread function, used for the display of bitmaps.

UINT TDDISPLAY (LPVOID LPPARAM)

{

Calphaactrl * alphactrl = (calphaCtrl *) LPPARAM;

CclientDC DC (alphactrl);

CDC PDC;

PDC.createCompatibleDC (& DC);

PDC.SelectObject (& AlphaActrl-> BMP);

Blendfunction rblendprops;

RblendProps.blendop = AC_SRC_OVER;

Rblendprops.blendflags = 0;

Rblendprops.alphaformat = 0;

Bitmap bminfo;

:: getObject (alphaactrl-> bmp.m_hobject, sizeof (bitmap), & bminfo;

Int nwidth, nheigh;

NWIDTH = Bminfo.Bmwidth;

NHEigh = bminfo.bmheight;

Alphaactrl-> setWindowPos (NULL, 0, 0, NWIDTH, NHEigh, SWP_Nomove);

INT i = 0;

While (i <= 255)

{

AlphaDisplay (PDC, DC, RBLENDPROPS, NWIDTH, NHEigh, i);

I = 5;

Sleep (alphaactrl-> ntimer);

}

AlphaCtrl-> bcanpaint = 1; // make OnPaint Word

AFXENDTHREAD (0); RETURN 0;

}

In the fourth step, now everything is ready, plus the initialization function:

Bool loadalphabitmap (uint uid, int itimer)

{

INT i = bmp.loadbitmap (uid);

IF (i)

{

AfxBeginthread (TDDisplay, this);

NBMPID = UID;

Ntimer = itimer;

Return 1;

}

Else

{

Trace ("Load Bitmap Failed / N);

Return 0;

}

Return 1;

}

Finally, open your dialog and see the cool bitmap!

You need the source code to leave E-mail.

Code download: alphaaboutdlg.rar

转载请注明原文地址:https://www.9cbs.com/read-5023.html

New Post(0)