Bitmap Fade Display

Source: Internet
Author: User
Tags bool time interval

We often display a bitmap about the company or its own message in AboutBox, have you ever thought of making this bitmap a cooler effect? For example, plus fade effect? As long as you have this Calphactrl control can be easily implemented.

Calphactrl is inherited from CStatic. When used, just add Calphactrl to the form, and then call the Loadalphabitmap (UINT uID, int itimer) function to implement the bitmap fade in and out. Where the UID is a bitmap resource Id,itimer is the bitmap display time interval, the smaller the value display the faster.

Here is a talk about how the Calphactrl is achieved. A key implementation function is a Win32 api:alphablend, this function can realize the transparent display of the image, the relevant parameters and information please refer to MSDN, it is worth noting that use this function to link to the Msimg32.lib library.

The first step, we first in the Calphactrl class to add a few data member:cbitmap Bmp;

BOOL bCanPaint; UINT nBmpID; int nTimer;

In the second step, add a member Function to the Calphactrl class:

void AlphaDisplay(CDC &pDC, CClientDC &dc, BLENDFUNCTION& rBlendProps, int width, int heigh, byte nLevel)
{
    //nLevel是透明度,0表示不显示,255则完全显示
    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, which is a thread function for bitmap display.

UINT tdDisplay(LPVOID lpParam)
{
    CAlphaCtrl* AlphaCtrl = (CAlphaCtrl*)lpParam;    CClientDC dc(AlphaCtrl);
    CDC pDC;
    pDC.CreateCompatibleDC(&dc);
    pDC.SelectObject(&AlphaCtrl->Bmp);    BLENDFUNCTION rBlendProps;
    rBlendProps.BlendOp = AC_SRC_OVER;
    rBlendProps.BlendFlags = 0;
    rBlendProps.AlphaFormat = 0;   BITMAP bmInfo;
    ::GetObject( AlphaCtrl->Bmp.m_hObject, sizeof(BITMAP), &bmInfo );
    INT nWidth, nHeigh;
    nWidth = bmInfo.bmWidth;
    nHeigh = bmInfo.bmHeight;AlphaCtrl->SetWindowPos(NULL, 0, 0, nWidth, nHeigh, SWP_NOMOVE);
   int i = 0;
    while(i <= 255)
    {
    AlphaCtrl->AlphaDisplay(pDC, dc, rBlendProps, nWidth, nHeigh, i);
    i += 5;
    Sleep(AlphaCtrl->nTimer);
    }
   AlphaCtrl->bCanPaint = 1; //Make OnPaint Word
    AfxEndThread(0);
    return 0;
}

Step fourth, 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, execute the program, open your dialog box and look at that cool bitmap!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.