We often display a bitmap about the company or our own information in AboutBox. Have you ever wondered how to make this bitmap look cool? For example, add the fade-in and fade-out effect? With this CAlphaCtrl control, you can easily implement it.
CAlphaCtrl is inherited from CStatic. You only need to add CalphaCtrl to the form, and then call the LoadAlphaBitmap (UINT uID, int iTimer) function to fade in and out the bitmap. The uID is the resource ID of the bitmap, And the iTimer is the time interval of the bitmap display. The smaller the value, the faster the display.
The following describes how CalphaCtrl is implemented. One of the key implementation functions is a win32 API: AlphaBlend, which enables Transparent Display of images. For relevant parameters and materials, see MSDN, it is worth noting that this function should be linked to the msimg32.lib library.
Step 1: First add several Data Member: CBitmap Bmp in the CalphaCtrl class;
BOOL bCanPaint; UINT nBmpID; int nTimer;
Step 2: Add a Member Function to the CalphaCtrl class:
Void AlphaDisplay (CDC & pDC, CClientDC & dc, BLENDFUNCTION & rBlendProps, int width, int heigh, byte nLevel) {// nLevel is transparent, 0 indicates not displayed, rBlendProps is displayed in 255. sourceConstantAlpha = nLevel; AlphaBlend (dc. m_hDC, 0, 0, width, heigh, pDC. m_hDC, 0, 0, width, heigh, rBlendProps );}
Step 3: Add a global function named 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 4: Now everything is ready, and the initialization function is added:
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 and open your dialog box to see the cool bitmap!