Jump to content
  • Advertisement
Sign in to follow this  

Transparent text with TextOut or ScriptTextOut

This topic is 2004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!