98下没有SetLayeredWindowAttributes函数,要怎么搞定这个问题呢?
我不希望是通过扫描来确定出一个区域的,那样速度很慢。
有没有什么好办法来确定一张位图中我想得到的RGN呢?还有怎么做半透明呢?

解决方案 »

  1.   

    Win98下面不能使用SetLayeredWindowAttributes(),只能自己做,有一种做法大致是这样的:获取当前窗体后面的背景的图片内容,然后以SRCAND方式贴到当前窗体上
      

  2.   

    CRgn m_rgnWnd;// 创建RoundRect区域m_rgnWnd 
    m_rgnWnd.CreateRoundRectRgn(0, 0, 300, 300, 30, 30); 
    // 将m_rgnWnd设置为窗口区域 
    SetWindowRgn((HRGN)m_rgnWnd, TRUE);
      

  3.   

    我要的是去掉位图背景的区域在转成RGN要怎么做呢?楼上几位的回答未免也太易了。
      

  4.   


    早就想写一篇我这方面的心得, 一直特忙没有空, 不过这次点名找我, 就
    不好再缩头缩脑了.
    其实我也还有很多问题没有解决, 在这里也向高手请教.
    此篇在V-Galaxy BBS发表, 如要转贴请和作者联系
    [email protected]
    1. Windows 2000 的GDI+扩充集
    Windows 2000内部扩充了原有的GDI指令集, 将直接提供高效的图像处理指
    令如Alpha Blending等. 在Win2000里有一个专门
    的窗口风格WS_EX_LAYERED提供透明窗口的可能. Win2000还提供如
    AnimateWindow(...)一类的API, 直接支持窗口动画. 
    有关连接
    http://www.microsoft.com/hwdev/video/GDInext.htm
    http://x.wonder.ca/stevex/nthack/
    2. Win95/NT下的实现方法
    既然Windows不提供现成的, 就只好自己做了. 原则上是这么过程:
    a. 获取桌面的DC, 生成一个兼容的Bitmap, 送一个WM_PAINT消息, 把桌面
    取到Bitmap里. (就像一个Screen Capture)
    b. 把自己要开的窗口画到另一幅Bitmap
    c. 用这两幅图片做Alpha Blending, 或者其他效果, 生成第三幅图.
    d. 最后再把图片BitBlt出来
    说起来并不难, 但技术上有几个问题.
    1. 如何把hidden的窗口拷贝到图像里?
    原则上说, 是做不到的. 但在自己程序里可以做一些手脚. 我在
    SafeClean Utilities2里用的Wizard窗口, 就需要在显示之前
    获取图像, 再做处理. 我用的是WM_PRINT消息, 平时用的很少, 但可以把
    Z-Order低的窗口也拦截下来(hidden的还不行). 比如
    我要从第1页(p1)翻到第二页(p2), 我就得在显示新窗口之前, 把p1移到最
    上面, 再显示p2(被p1遮挡着), 向p2送WM_PRINT消息
    拦截图片, 再做动画, 关闭p1.
    2. WM_PRINT的问题
    WM_PRINT其实和WM_PAINT差不多, 但可以拦截被其他窗口遮住的部分. 但
    不是所有窗口都支持WM_PRINT, 比如Static control,
    只能自己在Bitmap里把他们画上.
    3. 如何把程序下面的窗口储存在Bitmap里?
    要想实现真正的象Win2000的Transparent Window, 就必须得把自己程序下
    面的窗口存在图片里. 能否实现我也不清楚.
    我用的技术其实只适合做Splash Window和象金山词霸那样静态的透明窗
    口.我用的代码列在下面, 因为涉及到一些其他类, 分布在不同地方, 所以并
    不完整. 仅供参考. 关于使用Alpha Blending, 可以参看
    www.codeguru.com的Bitmap部分, 有很好的代码. 这段时间实在很忙, 学
    校又要开始考试, 只是粗略介绍, 请大家谅解.int CFadeWindow::FadeAndSwitchWindow(CWnd* pWndFrom, CWnd* 
    pWndTo)
    {
    CWnd* pDesktop = CWnd::GetDesktopWindow();
    CWnd* pMain = AfxGetMainWnd();
    CClientDC dc(pMain);
    CRect rc;
    pWndFrom->GetWindowRect(rc);
    pMain->ScreenToClient(rc);
    CDC *pDCFrom;
    pDCFrom=pWndFrom->GetDC ();
    m_dib1.PasteDC ( pDCFrom, 0, 0, rc.Width(), rc.Height());
    m_dib3.PasteDC ( pDCFrom, 0, 0, rc.Width(), rc.Height());
    pWndFrom->ReleaseDC ( pDCFrom );
    CClientDC dcWndTo(pWndTo);
    CDC dcTo;
    dcTo.CreateCompatibleDC(&dcWndTo);
    CBitmap bmpTo;
    bmpTo.CreateCompatibleBitmap(&dcWndTo, rc.Width(), rc.Height());
    CBitmap * pOldbmp = dcTo.SelectObject(&bmpTo);
    SetWindowPos(pWndFrom->GetSafeHwnd(), HWND_TOP, 
    0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
    pWndTo->ShowWindow(SW_SHOW);
    pWndTo->SendMessage(WM_PRINT, (WPARAM) dcTo.GetSafeHdc(), 
    (LPARAM) PRF_CLIENT|PRF_CHILDREN|PRF_OWNED);
    m_dib2.PasteDC ( &dcTo, 0, 0, rc.Width(), rc.Height());
    dcTo.SelectObject(pOldbmp);
    SetWindowPos(pWndTo->GetSafeHwnd(), HWND_TOP, 
    0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_SHOWWINDOW);
    SetWindowPos(pWndFrom->GetSafeHwnd(), NULL, 
    0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOZORDER|SWP_HIDEW
    INDOW);
    int index = 5;
    for (int i=0; i {
    m_dib3.Paste(&m_dib1);
    m_dib3.Blend(&m_dib2, i*256/index);
    m_draw.DrawDib ( &m_dib3, dc.GetSafeHdc(), rc.left, rc.top,
    rc.Width(), rc.Height(), DDF_HALFTONE );
    Sleep(10);
    }
    m_draw.DrawDib ( &m_dib2, dc.GetSafeHdc(), rc.left, rc.top,
    rc.Width(), rc.Height(), DDF_HALFTONE );
    return 1;
    }(摘自《程序员大本营》)
      

  5.   

    HRGN GetBmpRgn(HBITMAP hBitmap)
    {
        int  x1,x2;
        int  nBit;
        HRGN hRgn0;
        HRGN hRgn1;
        HRGN hRgn2;
        BYTE *Data;
        BITMAP Bmp;    if (GetObject(hBitmap,sizeof(
            Bmp),&Bmp)==0)
            return NULL;
        if (Bmp.bmBitsPixel != 1) {
            return NULL; //不是单色的位图
        }    hRgn0 = NULL;
        for(int h=0; h<Bmp.bmHeight; h++) {
            Data = (BYTE*)Bmp.bmBits+(Bmp.bmHeight
                -1-h)*Bmp.bmWidthBytes;
            for(nBit=7,x1=0; x1<Bmp.bmWidth; ) {
                //首先查找白点
                for(; x1<Bmp.bmWidth; x1++,nBit--) {
                    if (nBit < 0) {
                        nBit = 7;  Data++;
                    }
                    if ((*Data>>nBit) & 1)
                        break; //找到白点
                }
                //然后查找黑点
                for(x2=x1+1,nBit--; x2<Bmp.bmWidth;
                    x2++,nBit--) {
                    if (nBit < 0) {
                        nBit = 7;  Data++;
                    }
                    if (((*Data>>nBit)&1)==0)
                        break; //找到黑点
                }
                if (x1 < Bmp.bmWidth) {
                    hRgn1 = CreateRectRgn(x1, h,
                        x2, h+1);
                    if (hRgn0 == NULL)
                        hRgn0 = hRgn1;
                    else {
                        CombineRgn(hRgn0, hRgn0,
                            hRgn1, RGN_OR);
                        DeleteObject(hRgn1);
                    }
                    x1 = x2+1;  nBit--;
                }
            }
        }
        return hRgn0;
    }
      

  6.   

    针对各种颜色的HBITMAP转化为region。HRGN BitmapToRegion (HBITMAP hBmp, COLORREF cTransparentColor = 0, COLORREF cTolerance = 0x101010)
    {
    HRGN hRgn = NULL; if (hBmp)
    {
    // Create a memory DC inside which we will scan the bitmap content
    HDC hMemDC = CreateCompatibleDC(NULL);
    if (hMemDC)
    {
    // Get bitmap size
    BITMAP bm;
    GetObject(hBmp, sizeof(bm), &bm); // Create a 32 bits depth bitmap and select it into the memory DC 
    BITMAPINFOHEADER RGB32BITSBITMAPINFO = {
    sizeof(BITMAPINFOHEADER), // biSize 
    bm.bmWidth, // biWidth; 
    bm.bmHeight, // biHeight; 
    1, // biPlanes; 
    32, // biBitCount 
    BI_RGB, // biCompression; 
    0, // biSizeImage; 
    0, // biXPelsPerMeter; 
    0, // biYPelsPerMeter; 
    0, // biClrUsed; 
    0 // biClrImportant; 
    };
    VOID * pbits32; 
    HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
    if (hbm32)
    {
    HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32); // Create a DC just to copy the bitmap into the memory DC
    HDC hDC = CreateCompatibleDC(hMemDC);
    if (hDC)
    {
    // Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
    BITMAP bm32;
    GetObject(hbm32, sizeof(bm32), &bm32);
    while (bm32.bmWidthBytes % 4)
    bm32.bmWidthBytes++; // Copy the bitmap into the memory DC
    HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
    BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY); // For better performances, we will use the ExtCreateRegion() function to create the
    // region. This function take a RGNDATA structure on entry. We will add rectangles by
    // amount of ALLOC_UNIT number in this structure.
    #define ALLOC_UNIT 100
    DWORD maxRects = ALLOC_UNIT;
    HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
    RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
    pData->rdh.dwSize = sizeof(RGNDATAHEADER);
    pData->rdh.iType = RDH_RECTANGLES;
    pData->rdh.nCount = pData->rdh.nRgnSize = 0;
    SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0); // Keep on hand highest and lowest values for the "transparent" pixels
    BYTE lr = GetRValue(cTransparentColor);
    BYTE lg = GetGValue(cTransparentColor);
    BYTE lb = GetBValue(cTransparentColor);
    BYTE hr = min(0xff, lr + GetRValue(cTolerance));
    BYTE hg = min(0xff, lg + GetGValue(cTolerance));
    BYTE hb = min(0xff, lb + GetBValue(cTolerance)); // Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
    BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
    for (int y = 0; y < bm.bmHeight; y++)
    {
    // Scan each bitmap pixel from left to right
    for (int x = 0; x < bm.bmWidth; x++)
    {
    // Search for a continuous range of "non transparent pixels"
    int x0 = x;
    LONG *p = (LONG *)p32 + x;
    while (x < bm.bmWidth)
    {
    BYTE b = GetRValue(*p);
    if (b >= lr && b <= hr)
    {
    b = GetGValue(*p);
    if (b >= lg && b <= hg)
    {
    b = GetBValue(*p);
    if (b >= lb && b <= hb)
    // This pixel is "transparent"
    break;
    }
    }
    p++;
    x++;
    } if (x > x0)
    {
    // Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
    if (pData->rdh.nCount >= maxRects)
    {
    GlobalUnlock(hData);
    maxRects += ALLOC_UNIT;
    hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
    pData = (RGNDATA *)GlobalLock(hData);
    }
    RECT *pr = (RECT *)&pData->Buffer;
    SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
    if (x0 < pData->rdh.rcBound.left)
    pData->rdh.rcBound.left = x0;
    if (y < pData->rdh.rcBound.top)
    pData->rdh.rcBound.top = y;
    if (x > pData->rdh.rcBound.right)
    pData->rdh.rcBound.right = x;
    if (y+1 > pData->rdh.rcBound.bottom)
    pData->rdh.rcBound.bottom = y+1;
    pData->rdh.nCount++; // On Windows98, ExtCreateRegion() may fail if the number of rectangles is too
    // large (ie: > 4000). Therefore, we have to create the region by multiple steps.
    if (pData->rdh.nCount == 2000)
    {
    HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
    if (hRgn)
    {
    CombineRgn(hRgn, hRgn, h, RGN_OR);
    DeleteObject(h);
    }
    else
    hRgn = h;
    pData->rdh.nCount = 0;
    SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
    }
    }
    } // Go to next row (remember, the bitmap is inverted vertically)
    p32 -= bm32.bmWidthBytes;
    } // Create or extend the region with the remaining rectangles
    HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
    if (hRgn)
    {
    CombineRgn(hRgn, hRgn, h, RGN_OR);
    DeleteObject(h);
    }
    else
    hRgn = h; // Clean up
    GlobalFree(hData);
    SelectObject(hDC, holdBmp);
    DeleteDC(hDC);
    } DeleteObject(SelectObject(hMemDC, holdBmp));
    } DeleteDC(hMemDC);
    }
    } return hRgn;
    }
      

  7.   

    用photoshop转化成2色位图很快就出来了
      

  8.   


    半透明的混色方法?????
    kkkkkkkk