Draw / print text with transparent background in c++ win32 - c++

I am trying to draw/print a text, but i need a transparent background. I have been trying this:
TextOut(hDC, 50, 50, "MY TEXT", lstrlen("MY TEXT"));
The problem is that this generates an white background.
I have also tried with this:
ExtTextOut(hDC, 50, 50, ETO_OPAQUE, TA_LEFT, "MY TEXT", lstrlen("MY TEXT"), NULL);
ETO_OPAQUE - sets the background color on the text area to the current background color of the program. But if my background is dynamic and changes, this would not work, and that's why i need a transparent background.
For more information on these 2 functions http://msdn.microsoft.com/en-us/library/dd144821(v=vs.85)
I know i could insert the text into an bitmap and use "TransparentBlt" to achieve the "same" effect, but... Anyone who knows another function to draw / print text with transparent background, or any ideas on how i could solve this so called problem?
Thanks... =)

SetBkMode( hdc, TRANSPARENT );

Related

setting transparency for layered windows

I have a multi-level window, I need to display translucent text. The problem is that I can call SetLayeredWindowAttributes() only once, ie I need to make the background transparent first:
SetLayeredWindowAttributes(hwnd, 0, 0, LWA_COLORKEY);
and then draw text and apply transparency to the entire window:
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
but each subsequent call to SetLayeredWindowAttributes() overrides the previous one.
I tried to display the text using AlphaBlend(), but since what I need to mix with lies in another window, the text just turned gray.

How to clear label caption?

I am using label with OnCtrlcolor event:
I have set the background color of the label to be the same as the form,
if (iD == IDCmylabel)
{
pDC->SetTextColor(blue);
COLORREF normal = RGB(245, 245, 245);
pDC->SetBkColor(normal);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
So I was thinking to use toogle:
SetWindowTextW("abc..."); // will show the color as expected.
SetWindowTextW(nullptr); // will remove the text color.
However this is not working for me (the caption didn't redraw because it still there).
How do I fix this?
NULL_BRUSH is a brush that instructs the system to turn any painting operations that use that brush into no-ops. Using it doesn't actually make the control transparent. It just appears to be transparent until (part of it) has been painted.
If you want a control that has a particular background color, irrespective of the the size of text displayed, you're going to have to provide a solid color brush.
The easiest way to do this would be to return a DC_BRUSH, with an accompanying call to SetDCBrushColor to request the color, i.e.
if (iD == IDCmylabel) {
pDC->SetTextColor(blue);
COLORREF normal = RGB(245, 245, 245);
// Still required so that the text background matches that of the rest
pDC->SetBkColor(normal);
// Request brush color for the control background
pDC->SetDCBrushColor(normal);
// Note: Stock objects do not need to be freed by client code
return (HBRUSH)GetStockObject(DC_BRUSH);
}
With that you can call SetWindowText with arbitrary parameters and get the result you are looking for.
Someone needs to erase the background under the old text.
You are returning NULL_BRUSH, so "erase background" does nothing.
Return the solid brush of the color RGB(245, 245, 245). You may also need to call Invalidate for that window after setting new text.

What might I be doing wrong to draw text with QPainter in QT 5 using C++?

In my paintEvent method for a custom widget for a game I'm writing, After calling the various model objects' render() methods to render them to the widget, I am trying to render the "Hi-Score" text. Here's the general code for just the text drawing:
painter.fillRect(event->rect(), QColor(0, 0, 0));
painter.drawImage(QRectF(event->rect().x(), event->rect().y() + 30, 512, 512), getGameBoardImage());
//...rendering other model components
painter.setBrush(QBrush(QColor(255, 255, 255)));
//painter.setFont(getGameFont());
painter.setFont(QFont("Times New Roman", 16, QFont::Bold));
painter.drawText(0, 0, "HI-SCORE");
I 'was' trying to draw the text in a custom font loaded from resource (I found an answer on here for that) but it wouldn't even display, even with a white brush. I thought maybe it was because it was because I didn't 'set' a font, but setting it to Times New Roman doesn't display anything either. What might I be doing wrong? As you can see the background is a black background with the game board painted on top but leaving a small buffer at the top and bottom. Do I need to do something special to display the text? Please don't suggest using a QLabel because I am trying to keep it all in one widget if possible. If I must, I will split the Hi-Score and 1-Ups into 2 other label sets with specialty fonts.
you code look ok, but you are drawing at 0,0 which is the top left corner of the widget canvas AND the text is actually there but not visible...
draw instead at
painter.drawText(margin, y+margin, "HI-SCORE");
where y is the high of the font used to draw the text and margin is you know a little margin border to make it look better something like 5 units
update
you can get the value of the text you are painting doing somthing like
QFont font("times", 25);
QFontMetrics fm(font);
int pixelsW{fm.width("HI-SCORE")};
int pixelsH{fm.height()};

Draw text on screen C++

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

Drawing a system-like cursor, top-most, anywhere

I need to draw a system-like cursor that I simply can control the position of.
In other words, I need to draw a transparent image that looks just like the system cursor and I need it to be rendered on top of all other windows.
I've tried multiple approaches, but they all seem to have some downside.
I've figured out that I can load the cursor image by using LoadImage() and passing the resource OCR_NORMAL and casting it into a HBITMAP.
HICON NormalCursor = (HICON)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
Then getting the "desktop" HDC
hDC = GetDC(NULL);
Then I can try to draw it using DrawIconEx()
DrawIconEx(hDC, (int)x, 0, NormalCursor, 0, 0, NULL, NULL, DI_DEFAULTSIZE | DI_NORMAL);
The DI_NORMAL flag is supposed to combine the DI_IMAGE & DI_MASK flags giving me a transparent image/icon/cursor, but this is my result on the desktop:
Not to mention that if it moves it creates trails.
By making a transparent window using SetLayeredWindowAttributes like this:
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 50, LWA_COLORKEY);
And having the background color of my window to be black, I can remove the background from the window. But due to doing alpha based on a color I get ugly black pixels around my cursor icon.
Can I make the background of a window transparent in some other way than using a color mask?
How do I draw a transparent cursor on top of all windows properly?
I would recommend that you do make your own window, and do something like what's described at http://www.codeproject.com/KB/GDI-plus/CsTranspTutorial3.aspx . It's in C#, but most of it is just win32 calls. It does a nice job of variable transparency, too, not just 0%/100%.
Isn't the outline of the cursor black? Is the problem just that you're making the outline transparent too? Why don't you just change the transparency color (and the background color of the window) to anything other than black or white?