CRgnbutton.cpp文件/ RgnButton.cpp : implementation file
//#include "stdafx.h"
#include "RgnButton.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CRgnButtonCRgnButton::CRgnButton()
{
ToleranceColor=0x101010;
TransparentColor=0x0;
}
/*
CRgnButton::CRgnButton(UINT nBmpID, COLORREF cTransparentColor , COLORREF cTolerance)
{
ToleranceColor=cTolerance;
TransparentColor=cTransparentColor;
if(LoadPicture(nBmpID))
AttachRgn(BitmapToRgn(TransparentColor,ToleranceColor));
}
*/
CRgnButton::~CRgnButton()
{
}
BEGIN_MESSAGE_MAP(CRgnButton, CButton)
//{{AFX_MSG_MAP(CRgnButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CRgnButton message handlers

解决方案 »

  1.   


    HRGN CRgnButton::BitmapToRgn(UINT nID,COLORREF cTransparentColor,COLORREF cTolerance)
    {
    HRGN hRgn = NULL;
    //if(hbmp)
    m_RgnBitmap=new CBitmap;
    if (m_RgnBitmap->m_hObject)//get bitmap.handle
    {
    // 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);
    m_RgnBitmap->GetObject(sizeof(BITMAP),&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, (HBITMAP)m_RgnBitmap->m_hObject);
    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));
    //BYTE mr=lr-GetRValue(cTolerance);
    //BYTE mg=lg-GetGValue(cTolerance);
    //BYTE mb=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
    SelectObject(hDC, holdBmp);
    DeleteDC(hDC);
    } DeleteObject(SelectObject(hMemDC, holdBmp));
    } DeleteDC(hMemDC);
    }
    }
    m_RgnBitmap->DeleteObject();
    return hRgn;
    }COLORREF CRgnButton::SetToleColor(COLORREF cToleranceColor)
    {
    this->ToleranceColor=cToleranceColor;
    return cToleranceColor;
    }COLORREF CRgnButton::SetTranColor(COLORREF cTransparentColor)
    {
    this->TransparentColor=cTransparentColor;
    return cTransparentColor;
    }
    /*
    BOOL CRgnButton::LoadPicture(UINT nID)
    {
    if(m_RgnBitmap->LoadBitmap(nID))
    return TRUE;
    else
    return FALSE;
    }void CRgnButton::AttachRgn(HRGN hrgn)
    {
    m_hRgn=hrgn;
    }
    */
    void CRgnButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct,UINT nID) 
    {
    // TODO: Add your code to draw the specified item
    RECT Rect;
    Rect=lpDrawItemStruct->rcItem; CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
    BITMAP bm;
    m_RgnBitmap->GetObject(sizeof(BITMAP),&bm);
    CDC memDC;
    memDC.CreateCompatibleDC(pDC);
    HRGN rgn;
    rgn=BitmapToRgn(nID,TransparentColor,ToleranceColor);
    if(rgn)
    AfxMessageBox("No Rigon Found",1,NULL);
    else 
    SetWindowRgn(rgn,TRUE); pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);
    memDC.DeleteDC();}
    /***************
    BOOL CRgnButton::Create(LPCTSTR lpszCaption,DWORD dwStyle,const RECT &rect,CWnd *pParentWnd,UINT nID,UINT nBmpID)
    {
    LoadPicture(nBmpID);
    AttachRgn(BitmapToRgn(TransparentColor,ToleranceColor));
    return CButton::Create(lpszCaption,dwStyle,rect,pParentWnd,nID);}
    **************************/void CRgnButton::PreSubclassWindow() 
    {
    // TODO: Add your specialized code here and/or call the base class

    CButton::PreSubclassWindow();
    ModifyStyle(0,BS_OWNERDRAW);
    //SetWindowRgn(m_hRgn,TRUE);}
      

  2.   

    成员里面有指针
    析构函数又没有
    是不是象
    CBitmap     *m_RgnBitmap;
    这样的成员引起问题呢?
      

  3.   

    to visualtree( 梦想依旧)按你的意见该怎么样??
    还有我保证那个BitmapToRgn函数是正确的,应为我在对话框里已经实现了
    这个功能是不是要在析构函数中用 delete m_RgnBitmap;
    ????
      

  4.   

    根本就不反应!!!!
    我是在对话框里静态画了个Cbutton 然后把它设为 CRgnButton的对象
    并且我在对话框的WM_DRAWITEM里调用了CRgnButton的drawitem函数没反应然后我在对话框里动态创建了一个CRgnButton,在对话框的初始化函数里
    调用create函数
    然后rgnbutton.showwindow(SW_SHOW);可是依然没反应:(哪位大哥能不能把这代码拷下去,自己创建个对话框程序,调试一下???如果分不够,再加!!!
      

  5.   

    是的,其中那个BitmapToRgn()是参考别人的代码,改的哪位高手帮忙一下啊!!!!
    绝对给分
      

  6.   

    在DrawItem函数中的
    pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);语句前后加入如下
    ::SelectClipRgn(pDC -> GetSafeHdc(), hRgn);
    pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);
    ::SelectClipRgn(pDC -> GetSafeHdc(), NULL);
    应该就可以了,你做的东西,前段时间我就做好了,但我的只是16色的,你做的好想是32位色,比我的好,我要学习一下.
      

  7.   

    在DrawItem函数中的
    pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);语句前后加入如下
    ::SelectClipRgn(pDC -> GetSafeHdc(), hRgn);
    pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);
    ::SelectClipRgn(pDC -> GetSafeHdc(), NULL);
    应该就可以了,你做的东西,前段时间我就做好了,但我的只是16色的,你做的好想是32位色,比我的好,我要学习一下.