Sign in to follow this  

Transparent text with TextOut or ScriptTextOut

Recommended Posts

anders211    250

I have had a problem but I solved it. This is the solution which tell You how to set transparency to the background window.

The solution depends on ivoking twice BitBlt operation. First time when copying current background window into HDC of our bitmap and secondly when You write the text into bitmap and copy them back to HDC of window.





HRESULT Buffer::drawText(i32 x, i32 y, RECT* rect)
/*HRESULT hr = ScriptTextOut(hdc
_In_ const HDC hdc,
_Inout_ SCRIPT_CACHE *psc,
_In_ int x,
_In_ int y,
_In_ UINT fuOptions,
_In_opt_ const RECT *lprc,
_In_ const SCRIPT_ANALYSIS *psa,
_In_ const WCHAR *pwcReserved,
_In_ int iReserved,
_In_ const WORD *pwGlyphs,
_In_ int cGlyphs,
_In_ const int *piAdvance,
_In_opt_ const int *piJustify,
_In_ const GOFFSET *pGoffset*/

if(mFont.font != NULL)
hDC = mFont.font->GetDC(); //get font DC
::MessageBox(NULL, "Buffer::drawText - font not initialized", "Warning", MB_ICONWARNING);
return S_FALSE;

//Prepare ID3DXFont
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hDC, mBitmap);
COLORREF prevBkColor = SetBkColor(hDC, 0x00FF00);
//HBRUSH hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
//HBRUSH hBrushOld = (HBRUSH)SelectObject(hDC, hBrush);
//COLORREF dcColorOld = GetDCBrushColor(hDC);
//SetDCBrushColor(hDC, 0xFF0000);
i32 prevBkMode = SetBkMode(hDC, TRANSPARENT);
COLORREF prevTextColor = SetTextColor(hDC, 0x000000);
BitBlt(hDC, rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top, GUI::hDCWin, rect->left, rect->top, SRCCOPY);

SCRIPT_CACHE scriptCache = NULL; //in: font info

//TextOutW(hDC,rect->left, rect->top, mBuffer,getTextSize()); //used for debug purposes
HRESULT hr = ScriptTextOut(hDC, &scriptCache, x, y, ETO_CLIPPED, rect, &mItems[0].a, NULL, 0, &mGlyphs[0], //ETO_OPAQUE set BkColor so don't use it
static_cast<i32>(mGlyphs.size()), &mPiAdvance[0], NULL, &mGoffset[0]); //&mPiJustify[0]
BitBlt(GUI::hDCWin, rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top, hDC, rect->left, rect->top, SRCCOPY);


//Restore hDC of ID3DXFont
SelectObject(hDC, hBitmapOld);
SetBkMode(hDC, prevBkMode);
//SelectObject(hDC, hBrushOld);
SetBkColor(hDC, prevBkColor);
SetTextColor(hDC, prevTextColor);
//SetDCBrushColor(hDC, dcColorOld);

return hr;


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this