0

I want to draw text on the screen above all the windows. I found out about HDC and start working with it. I had 2 problems: the text was flashing and there was background. I found out the function:

SetBkMode(hdc, TRANSPARENT);

but all it done is cancel the flashing. I still got background. My final code now is:

    RECT rect = { 20, 20, 200, 200 };
    SetTextColor(hdc,RGB(255,0,0));
    SetBkMode(hdc, TRANSPARENT);
    SetBkColor(hdc,RGBA(0,255,0,0));
    DrawText(hdc, L"My text",-1,&rect,DT_LEFT);

I put this code in while(true) statement and sleep for 1 millisec. Before the while i got the hdc init:

HDC hdc = GetDC(0);

So at this point i got non flashing text but with background (not transparent). The background is half transparent so i can see what below it but it doesn't update. When i put a new window below it i can see the "background" of the old one.

I tried using wndproc like in this question: How to draw text with transparent background using c++/WinAPI? But it does nothing at all (i cant even see the text) I tried using the textout example from msdn site: http://msdn.microsoft.com/en-us/library/windows/desktop/dd145133(v=vs.85).aspx But it does nothing too.

How can i draw text on the screen without background at all? Thank you guys

Community
  • 1
  • 1
MyNick
  • 536
  • 1
  • 9
  • 25
  • I never used this before, but what happens if you remove the SetBkColor() function? – John Odom Oct 30 '14 at 20:16
  • Use `CreateSolidBrush` and `FillRect` to clear the background. Search for "Double Buffering" to avoid the flashing. – wimh Oct 30 '14 at 20:17
  • John - It got no effect (looks the same) Wimmel - If i will fill rect it will draw rectangle and hide the background, i dont want to hide it. Or i didnt understand you correctly – MyNick Oct 30 '14 at 20:20
  • @user2462683 ah, I misunderstood that. What you want is a bit harder, but take a look at [Creating a transparent window in C++ Win32](http://stackoverflow.com/q/3970066/33499) – wimh Oct 30 '14 at 20:34
  • You have used `RGB()` with SetTextColor() but `RGBA()` with `SetBkColor()`... for clarity I would recommend sticking with one or the other (`RGB()` for preference if you aren't setting the alpha value). However I don't think that will fix your problem... – AAT Oct 30 '14 at 22:42
  • Does the text background which you see correspond to the background colour you have set (with `SetBkColor()`, i.e. green in the example you give)? – AAT Oct 30 '14 at 22:47
  • @AAT first of all: #define RGBA(r,g,b,a) ((COLORREF)((((DWORD)(BYTE)(a))<<24)|RGB(r,g,b))) so it is the same as RGB, COLORREF got alpha channel in it value and RGB doesn't give it a value. so they are the same. second, now the background is always black, i didnt touch the code, weird. – MyNick Oct 31 '14 at 06:58
  • 1
    Yeah I know that using `RGBA()` this way is the same as using `RGB()`: I said use one or the other __for clarity__, it was just some additional free advice! – AAT Oct 31 '14 at 19:37

0 Answers0