Hi, Following is my code..
<br />
HBITMAP m_hBmp;<br />
HRGN m_hClientRgn;<br />
HRGN m_hWndRgn;<br />
HICON m_hIcon;<br />
COLORREF m_colorBk;<br />
<br />
HRGN DIBToRgn(HBITMAP hBmp,COLORREF BkColor,BOOL Direct);
<br />
char drive1[_MAX_DIR];<br />
<br />
BEGIN_MESSAGE_MAP(CFormDlg, CDialog)<br />
ON_WM_ERASEBKGND()<br />
ON_WM_PAINT()<br />
ON_WM_QUERYDRAGICON()<br />
END_MESSAGE_MAP()<br />
<br />
<br />
BOOL CFormDlg::OnInitDialog() <br />
{<br />
<br />
CDialog::OnInitDialog();<br />
<br />
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);<br />
m_hBmp=(HBITMAP)LoadImage(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);<br />
m_hClientRgn=CreateEllipticRgn(933,934,934,935);<br />
m_hWndRgn=DIBToRgn(m_hBmp,0x00ff00,FALSE);<br />
m_colorBk = 0x00000000;<br />
<br />
return TRUE;
}<br />
<br />
HRGN CFormDlg ::DIBToRgn(HBITMAP hBmp, COLORREF BkColor, BOOL Direct)<br />
{<br />
HRGN hRgn = NULL; <br />
#define MAX_ALLOC_RECTS 100<br />
COLORREF Tolerance=0x00101010;<br />
if (hBmp)<br />
{<br />
HDC hMemDC = CreateCompatibleDC(NULL); <br />
if (hMemDC)<br />
{<br />
BITMAP bm;<br />
GetObject(hBmp, sizeof(bm), &bm); <br />
<br />
BITMAPINFOHEADER BmpInfoh = {
sizeof(BITMAPINFOHEADER),
bm.bmWidth,
bm.bmHeight,
1,
32,
BI_RGB,
0,
0,
0,
0,
0
};<br />
LPVOID pBit32; <br />
HBITMAP hDib32 = CreateDIBSection(hMemDC, <br />
(BITMAPINFO *)&BmpInfoh, <br />
DIB_RGB_COLORS, &pBit32, NULL, 0);<br />
if (hDib32)<br />
{<br />
HBITMAP hOldib32 = (HBITMAP)SelectObject(hMemDC, hDib32);<br />
HDC hDC = CreateCompatibleDC(hMemDC);<br />
if (hDC)<br />
{<br />
BITMAP bm32;<br />
GetObject(hDib32, sizeof(bm32), &bm32);<br />
while (bm32.bmWidthBytes % 4)<br />
bm32.bmWidthBytes++;<br />
HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);<br />
BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);<br />
DWORD MaxRects = MAX_ALLOC_RECTS;<br />
SYSTEM_INFO Sysinfo;<br />
GetSystemInfo(&Sysinfo);<br />
HANDLE hRcData=HeapCreate(HEAP_GENERATE_EXCEPTIONS,Sysinfo.dwPageSize, 0);<br />
RGNDATA * pRcData=(RGNDATA*)HeapAlloc(hRcData,HEAP_ZERO_MEMORY,<br />
sizeof(RGNDATAHEADER)+sizeof(RECT)*MaxRects);<br />
pRcData->rdh.dwSize = sizeof(RGNDATAHEADER);<br />
pRcData->rdh.iType = RDH_RECTANGLES;<br />
pRcData->rdh.nCount = pRcData->rdh.nRgnSize = 0;<br />
SetRect(&pRcData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);<br />
BYTE hr,hg,hb,lr,lg,lb;<br />
switch(BkColor)<br />
{<br />
case RGB(255,255,255):
hr = GetRValue(BkColor);<br />
hg = GetGValue(BkColor);<br />
hb = GetBValue(BkColor);<br />
lr = min(0xff, hr - GetRValue(Tolerance));<br />
lg = min(0xff, hg - GetGValue(Tolerance));<br />
lb = min(0xff, hb - GetBValue(Tolerance));<br />
break;<br />
case RGB(0,0,0):
lr = GetRValue(BkColor);<br />
lg = GetGValue(BkColor);<br />
lb = GetBValue(BkColor);<br />
hr = min(0xff, lr + GetRValue(Tolerance));<br />
hg = min(0xff, lg + GetGValue(Tolerance));<br />
hb = min(0xff, lb + GetBValue(Tolerance));<br />
break;<br />
default:
Tolerance=0x111111;<br />
lr =max(0, GetRValue(BkColor)-GetRValue(Tolerance));<br />
lg = max(0,GetGValue(BkColor)-GetGValue(Tolerance));<br />
lb = max(0,GetBValue(BkColor)-GetBValue(Tolerance));<br />
hr=min(0xff,GetRValue(BkColor)+GetRValue(Tolerance));<br />
hg=min(0xff,GetGValue(BkColor)+GetGValue(Tolerance));<br />
hb=min(0xff,GetBValue(BkColor)+GetBValue(Tolerance));<br />
break;<br />
}<br />
BYTE *pBits = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;<br />
for (int y = 0; y < bm.bmHeight; y++)<br />
{<br />
for (int x = 0; x < bm.bmWidth; x++)<br />
{<br />
int x0 = x;<br />
DWORD *pColor = (DWORD *)pBits + x;<br />
BYTE dr,dg,db;<br />
while (x < bm.bmWidth)<br />
{<br />
dr=GetRValue(*pColor);<br />
dg=GetGValue(*pColor);<br />
db=GetBValue(*pColor);<br />
<br />
if ((dr>= lr && dr<= hr)&&(dg>=lg&&dg<=hg)&&(db>=lb&&db<=hb))<br />
{<br />
if(Direct)<br />
break;<br />
else<br />
{<br />
pColor++;<br />
x++;<br />
}<br />
}<br />
else if(Direct)<br />
{<br />
pColor++;<br />
x++;<br />
}<br />
else<br />
break;<br />
<br />
}<br />
if (x > x0)<br />
{<br />
if (pRcData->rdh.nCount >= MaxRects)<br />
{<br />
MaxRects += MAX_ALLOC_RECTS;<br />
pRcData=(RGNDATA*)HeapReAlloc(<br />
hRcData,HEAP_ZERO_MEMORY,pRcData, <br />
sizeof(RGNDATAHEADER)+sizeof(RECT)*MaxRects);<br />
}<br />
RECT *pr = (RECT *)&pRcData->Buffer;<br />
SetRect(&pr[pRcData->rdh.nCount], x0, y, x, y+1);<br />
pRcData->rdh.rcBound.left = x0;<br />
pRcData->rdh.rcBound.top = y;<br />
pRcData->rdh.rcBound.right = x;<br />
pRcData->rdh.rcBound.bottom = y+1;<br />
pRcData->rdh.nCount++;<br />
<br />
if (pRcData->rdh.nCount == 3000)<br />
{ <br />
HRGN tmphRgn = ExtCreateRegion(NULL,<br />
sizeof(RGNDATAHEADER) + (sizeof(RECT) * MaxRects),<br />
pRcData);<br />
if (hRgn)<br />
{<br />
CombineRgn(hRgn, hRgn, tmphRgn, RGN_OR);<br />
DeleteObject(tmphRgn);<br />
}<br />
else<br />
hRgn = tmphRgn;<br />
pRcData->rdh.nCount = 0;<br />
SetRect(&pRcData->rdh.rcBound, <br />
MAXLONG, MAXLONG, 0, 0);<br />
}<br />
}<br />
}<br />
<br />
pBits -= bm32.bmWidthBytes;<br />
}<br />
HRGN tmphRgn = ExtCreateRegion(NULL, <br />
sizeof(RGNDATAHEADER) + (sizeof(RECT) * MaxRects), pRcData);<br />
if (hRgn)<br />
{<br />
CombineRgn(hRgn, hRgn, tmphRgn, RGN_OR);<br />
DeleteObject(tmphRgn);<br />
}<br />
else<br />
hRgn = tmphRgn;<br />
if(!Direct)<br />
{<br />
HRGN hRect=CreateRectRgn(0,0,bm.bmWidth,bm.bmHeight);<br />
if(hRect)<br />
{<br />
CombineRgn(hRgn,hRgn,hRect,RGN_XOR);<br />
DeleteObject(hRect);<br />
}<br />
else<br />
return NULL;<br />
}<br />
HeapFree(hRcData,HEAP_NO_SERIALIZE,pRcData);<br />
SelectObject(hDC, holdBmp);<br />
DeleteDC(hDC);<br />
DeleteObject(holdBmp);<br />
}<br />
SelectObject(hMemDC,hOldib32);<br />
DeleteDC(hMemDC);<br />
DeleteObject(hOldib32);<br />
DeleteObject(hDib32);<br />
}<br />
else<br />
DeleteDC(hMemDC);<br />
}<br />
}<br />
return hRgn;<br />
}<br />
<br />
BOOL CFormDlg::OnEraseBkgnd(CDC* pDC) <br />
{<br />
if(m_hBmp)<br />
{<br />
BITMAP bm;<br />
GetObject(m_hBmp,sizeof(bm),&bm);<br />
HDC hMemdc=CreateCompatibleDC(pDC->m_hDC); <br />
if(hMemdc)<br />
{<br />
HBITMAP hOldBmp=(HBITMAP)SelectObject(hMemdc,m_hBmp);<br />
if(hOldBmp)<br />
{<br />
BitBlt(pDC->m_hDC,0,0,bm.bmWidth,bm.bmHeight,hMemdc,0,0,SRCCOPY);<br />
SelectObject(hMemdc,hOldBmp);<br />
DeleteDC(hMemdc);<br />
DeleteObject(hOldBmp);<br />
return TRUE;<br />
}<br />
else<br />
DeleteDC(hMemdc);<br />
}<br />
}<br />
return CDialog::OnEraseBkgnd(pDC);<br />
return FALSE;<br />
}<br />
<br />
void CFormDlg::OnPaint() <br />
{<br />
if (IsIconic())<br />
{<br />
CPaintDC dc(this);
<br />
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);<br />
<br />
int cxIcon = GetSystemMetrics(SM_CXICON);<br />
int cyIcon = GetSystemMetrics(SM_CYICON);<br />
CRect rect;<br />
GetClientRect(&rect);<br />
int x = (rect.Width() - cxIcon + 1) / 2;<br />
int y = (rect.Height() - cyIcon + 1) / 2;<br />
<br />
dc.DrawIcon(x, y, m_hIcon);<br />
}<br />
else<br />
{<br />
CDialog::OnPaint();<br />
}<br />
}<br />
<br />
<br />
HCURSOR CFormDlg::OnQueryDragIcon()<br />
{<br />
return (HCURSOR) m_hIcon;<br />
}<br />
With Regards,
Sangeetha.
|