BOOL CDlgTestDlg::OnInitDialog()
{
CDialog::OnInitDialog(); SetTimer(1, 1000, NULL);

    //m_hBmpΪCDlgTestDlgµÄ³ÉÔ±±äÁ¿
m_hBmp = (HBITMAP)::LoadImage(GetModuleHandle(NULL), BACKGROUND, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (m_hBmp == NULL)
{
MessageBox("Error loading bitmap");
return 0;
}
GetObject(m_hBmp, sizeof(m_Bitmap), &m_Bitmap);    //ÉèÖñ³¾°Í¸Ã÷
SetTranspareColor(); return TRUE;  // return TRUE  unless you set the focus to a control
}// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.void CDlgTestDlg::OnPaint() 
{ CPaintDC dc(this); 
//Create a memory DC
HDC hMemDC = ::CreateCompatibleDC(NULL);
//Select the bitmap in the memory dc.
SelectObject(hMemDC, m_hBmp);
//Copy the memory dc into the screen dc
  bool bResult = ::BitBlt(dc.m_hDC, 0, 0, m_Bitmap.bmWidth, m_Bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY);
//Delete the memory DC and the bitmap
::DeleteDC(hMemDC);
CDialog::OnPaint();
}
void CDlgTestDlg::SetTranspareColor()
{
CPaintDC  dc(this);
CDC m_MemDc; m_MemDc.CreateCompatibleDC(&dc); HBITMAP hBmp1 = (HBITMAP)LoadImage(AfxGetInstanceHandle(), BMPTEST, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap* pOldBitmap = m_MemDc.SelectObject(CBitmap::FromHandle(m_hBmp));  //×¢Òâ¸Ã¾ä m_strMsg = "Hello World!!"; RECT  rect;
rect.left = m_nPosX;
rect.right = m_nPosX + 100;
rect.top = 0;
rect.bottom = 30;  m_MemDc.SetBkColor(RGB(255, 0, 255));
m_MemDc.DrawText(m_strMsg, &rect, DT_INTERNAL); CRgn crRgn, crRgnTmp;
//create an empty region
crRgn.CreateRectRgn(0, 0, 0, 0);
//Create a region from a bitmap with transparency colour of Purple
COLORREF crTransparent = TRANSPARENTCOLOR;
int iX = 0;
for (int iY = 0; iY < m_Bitmap.bmHeight; iY++)
{
do
{
//skip over transparent pixels at start of lines.
while (iX <= m_Bitmap.bmWidth && m_MemDc.GetPixel(iX, iY) == crTransparent)
iX++;
//remember this pixel
int iLeftX = iX;
//now find first non transparent pixel
while (iX <= m_Bitmap.bmWidth && m_MemDc.GetPixel(iX, iY) != crTransparent)
++iX;
//create a temp region on this info
crRgnTmp.CreateRectRgn(iLeftX, iY, iX, iY+1);
//combine into main region.
crRgn.CombineRgn(&crRgn, &crRgnTmp, RGN_OR);
//delete the temp region for next pass (otherwise you'll get an ASSERT)
crRgnTmp.DeleteObject();
}while(iX < m_Bitmap.bmWidth);
iX = 0;
}
//Centre it on current desktop
SetWindowRgn(crRgn, TRUE);
//m_MemDc.SelectObject(pOldBitmap);
m_MemDc.DeleteDC();// SetWindowPos(&wndTopMost, iX, iY, m_Bitmap.bmWidth, m_Bitmap.bmHeight, NULL);
crRgn.DeleteObject();
}
void CDlgTestDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default m_nPosX += 50;
if(m_nPosX > 640)
m_nPosX = 0; SetTranspareColor(); Invalidate();
CDialog::OnTimer(nIDEvent);
}

解决方案 »

  1.   

    上面的程序,是实现透明背景,同时有字幕滚屏的程序。
    在SetTranspareColor()函数中,如果在语句使用
    m_MemDc.SelectObject(CBitmap::FromHandle(hBmp1)), 即背景的BMP使用临时变量hBmp1, 就能实现字幕的滚屏,但是如果使用成员变量m_hBmp, 即程序如下  
    m_MemDc.SelectObject(CBitmap::FromHandle(m_hBmp)),
    屏幕上就会出现一长串文字,就是下一次滚屏,会把上一次的字一起带出来。
    不知是什么原应。
    上面 hBmp1跟m_hBmps是加载了同一幅bmp
      

  2.   

    你的程序在下一次滚屏时把上一次字带出来,应该不是带出来,而是在你第二次绘图之前没有将上一次的屏幕覆盖造成的,你的程序也有一点问题,你运行程序已后,当你用别的程序的窗口覆盖你的程序再切回来,会有一小段时间没有字幕,这是因为你的绘制字的代码没有写在OnPaint函数里的原因,你可以把SetTranspareColor()里的内容放到OnPaint里,再在OnTimer里加入:
    Onpaint();
    还有就是HBITMAP hBmp1 = (HBITMAP)LoadImage(AfxGetInstanceHandle(), BMPTEST, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    不知你的位图有多大,这个函数里的参数为什么是0,0,这两个值表示你要加载的位图的宽和高