problem with coloured cursor in vc++
I am facing a problem while creating and setting coloured cursor. Our requirement is to create a coloured cursor from a bmp made from a text(using DrawText to dc). We need to set the background of text as transparent so that only the text appears as the cursor.
In some of our machines , this is working successfully but in some other machines at site , there is a problem.. Each letter of the text in cursor apears twice( just as a thich shadow for each letter). Can anyone point out the reason for this..If it is related to system settings, pls point out, what is actually causing this problem.
As the problem is very critical, it would be highly apprciable if anyone can give me some clues on the matter
The code is attached below
void CCursorTestDlg::CreateAnnoCursor(CString& strNewAnnotation_i)
{
SIZE textSize = {0};
HBITMAP hNewBmp = CreateBmpFromText((LPTSTR)
strNewAnnotation_i.operator LPCTSTR(),textSize);
m_hCursor= CreateCursorFromBmp(hNewBmp,textSize);
}
HBITMAP CreateBmpFromText(TCHAR *szText,SIZE &size)
{
HBITMAP hNewBmp = NULL;
HDC hDC = ::GetDC((HWND) NULL);
//HFONT FontOldObj = (HFONT)::SelectObject(hDC,
// m_TextFont.operator HFONT());
GetTextExtentPoint32(hDC,szText,_tcslen(szText),&size);
//::SelectObject(hDC,FontOldObj);
HDC hMemDC = ::CreateCompatibleDC(hDC);
hNewBmp = ::CreateCompatibleBitmap(hDC,size.cx,size.cy);
HBITMAP hOldBit = (HBITMAP)::SelectObject(hMemDC,hNewBmp);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = size.cx;
rect.bottom = size.cy;
//FontOldObj = (HFONT)::SelectObject(hMemDC,
// m_TextFont.operator HFONT());
COLORREF ColorRefObj= SetTextColor(
hMemDC, // handle to DC
RGB(255,128,64) // text color
);
COLORREF BkColorRefObj = ::SetBkColor(hMemDC,RGB(255,255,255));
::DrawText(hMemDC,szText,_tcslen(szText),&rect,DT_LEFT);
//::SelectObject(hMemDC,FontOldObj);
SetTextColor(hMemDC,ColorRefObj);
::SetBkColor(hMemDC,BkColorRefObj);
hNewBmp = (HBITMAP)::SelectObject(hMemDC,hOldBit);
::DeleteDC(hMemDC);
::ReleaseDC((HWND)NULL,hDC);
return hNewBmp;
}
HCURSOR CreateCursorFromBmp(HBITMAP hBitmap,SIZE& size)
//@END method
{
// Local identifier
HCURSOR hNewCursor = NULL;
//Getting the mask bitmap to make transparent cursor
HBITMAP hMaskBitmap = CreateMaskBitmap(hBitmap,RGB(255,255,255),
size);
// The iconinfo.fIcon must be FALSE
ICONINFO iconinfo = {0};
iconinfo.fIcon = FALSE;
//akj
iconinfo.xHotspot = 0;
iconinfo.yHotspot = 0;
// Set color and mask bitmaps
iconinfo.hbmMask = hMaskBitmap;
iconinfo.hbmColor = hBitmap;
// Create the cursor
hNewCursor = CreateIconIndirect(&iconinfo);
return hNewCursor;
}
HBITMAP CreateMaskBitmap(HBITMAP hbmColour_i,
COLORREF crTransparent_i,
SIZE& sizeObj)
{
//Local identifier to hold the mask bitmap
HBITMAP hbmMask;
HDC hdcColor, hdcMask;
//BITMAP bm;
//GetObject(hbmColour_i, sizeof(BITMAP), &bm);
// Create monochrome (1 bit) mask bitmap.
//hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hbmMask = CreateBitmap(sizeObj.cx, sizeObj.cy, 1, 1, NULL);
TCHAR szTemp[100]={0};
_stprintf(szTemp,_T("Mask Bmp Width is %d,Mask Bmp Height is %d"),
sizeObj.cx, sizeObj.cy);
// Get some HDCs that are compatible with the display driver
hdcColor = CreateCompatibleDC(0);
hdcMask = CreateCompatibleDC(0);
HBITMAP hOldClrBmp = (HBITMAP)::SelectObject(hdcColor, hbmColour_i);
HBITMAP hOldMskrBmp = (HBITMAP)::SelectObject(hdcMask, hbmMask);
// Set the background colour of the colour image to the colour
// you want to be transparent.
SetBkColor(hdcColor, crTransparent_i);
// Copy the bits from the colour image to the B+W mask... everything
// with the background colour ends up white while everythig else ends up
// black...Just what we wanted.
//BitBlt(hdcMask, 0, 0, bm.bmWidth, bm.bmHeight, hdcColor, 0, 0, SRCCOPY);
BitBlt(hdcMask, 0, 0, sizeObj.cx, sizeObj.cy, hdcColor, 0, 0, SRCCOPY);
// Take our new mask and use it to turn the transparent colour in our
// original colour image to black so the transparency effect will
// work right.
//BitBlt(hdcColor, 0, 0, bm.bmWidth, bm.bmHeight, hdcMask, 0, 0, SRCINVERT);
BitBlt(hdcColor, 0, 0, sizeObj.cx, sizeObj.cy, hdcMask, 0, 0, SRCINVERT);
// Clean up.
::SelectObject(hdcColor,hOldClrBmp);
::SelectObject(hdcMask,hOldMskrBmp);
DeleteDC(hdcColor);
DeleteDC(hdcMask);
return hbmMask;
}