Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to put text onto a bitmap before the bitmap is put to the screen.

I have been searching and trying to program this in C++ for a few days and I do not have it working.

I can now put text onto a bitmap, but the text has a white background.

I load the bitmap as a device independent bitmap and then I want to put this text onto the bitmap before I show the bitmap on the screen.

No Visual Studio and no .net please.
Please tell me if your answer requires an additional header file beyond what make this work as it is.

What I have tried:

C++
case WM_PAINT:
    {
        PAINTSTRUCT	ps;

        HDC_of_MainWindow = BeginPaint(hwnd, &ps);

        Draw_From_FRONT_BUFFER_001_To_MainWindow();

        ADD_TEXT_to_Graph(hwnd, ps.hdc); // This shows but does not have a transparent background. It shows black text with a white background. I want it to have a transparent background. I just want the text to be there, NOT the background.



   // Another way I have been trying but this does not show anything:

/// START adding text

        HDC buffDC = CreateCompatibleDC(HDC_of_MainWindow);
        SetTextColor(buffDC, RGB(0, 0, 0));
        SetBkColor(buffDC, RGB(255, 255, 255));

        RECT rc;
        GetClientRect(hwnd, &rc);

        // Create the HBITMAP "canvas", or surface on which to draw.
        HDC_of_MainWindow = GetDC( Handle_of_MainWindow );
        HBITMAP buffBitmap = CreateCompatibleBitmap( HDC_of_MainWindow, rc.right, rc.bottom );

        if(buffBitmap == NULL)
            {
                MessageBox(nullptr,L"failed to create buffBitmap",L"Error!",MB_ICONEXCLAMATION | MB_OK);
                // No error here but it still does not show up.
            }

        int savedDC = SaveDC(buffDC);
        SelectObject(buffDC, buffBitmap);

        HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
        FillRect(buffDC, &rc, hBrush);
        DeleteObject(hBrush);
        TextOut(buffDC, 10, 10, L"Test Text", 9);
        BitBlt(HDC_of_MainWindow, 0, 0, rc.right, rc.bottom, buffDC, 0, 0, SRCCOPY);

        RestoreDC(buffDC, savedDC);

        DeleteObject(buffBitmap);
        DeleteDC(buffDC);
/// END adding text

        return 0;
   }

   break;



and I am trying this


C++
void ADD_TEXT_to_Graph(HWND hwnd, HDC dc)
{
  RECT rc;
  GetClientRect(hwnd, &rc);
  DrawText(dc, L"Hello!\r\nLine Number 2", -1, &rc, DT_CENTER|DT_VCENTER|DT_WORDBREAK);
}



I see Hello! centered and at the top of my bitmap that they are in front of.
I see Line Number 2 below that and centered.
Both have black text and a white background.
I am trying to get rid of the white background and to be able to see the bitmap that is behind them.



No Visual Studio and no .net please.

Please tell me if your answer requires an additional header file beyond what make this work as it is.

Thank you.
Posted
Updated 15-Mar-21 17:17pm
v3
Comments
Rick York 15-Mar-21 23:55pm
   
What is this "Visual Studio" that you don't want?
Member 15078716 16-Mar-21 21:01pm
   
I have tried code that was VC++ (Microsoft Visual Studio C++) and it needed some header that I found and included, then it needed another header for that one particular header to work, and so on. After 4 of these, I gave up. It has happened similar to that at least 3 times and I concluded that I do not want to fight with Visual Studio anything. YOU GUYS HERE have made lots of code work that I studied and learned from that I did NOT have to get into a fight with Visual Studio headers. I see the coders here make things work with or without my having to fight with any Visual Studio, so the "No Visual Studio and .net please," request. Please do not take that as an insult, but rather a desperation to not have to fight so much with learning to program in C++.

Thank you Rick York for all of your help, here and other places which I have often read.

1 solution

You are creating a white brush, selecting it, and also setting the background color to white. All of these are why you see a white background. Try commenting all of those background calls off and adding a call to
SetBkMode( dc, TRANSPARENT );
Remember to call it with the correct device context.
   
Comments
Member 15078716 16-Mar-21 21:11pm
   
YES !

Rick York you have done it again.

Thank you !

I put it in here
case WM_PAINT:
    {
        Processing_A_Buffer_Update = 1;

        PAINTSTRUCT	ps;

        HDC_of_MainWindow = BeginPaint(hwnd, &ps);


        Draw_From_FRONT_BUFFER_001_To_MainWindow();


    RECT rc;
    GetClientRect(hwnd, &rc);

    SetBkMode( ps.hdc, TRANSPARENT );
    SetTextColor(ps.hdc, RGB(255, 0, 0));
    DrawText(ps.hdc, L"Red Text on Transparent Background!", -1, &rc, DT_CENTER|DT_VCENTER|DT_WORDBREAK);

    SetBkMode( ps.hdc, OPAQUE );
    SetTextColor(ps.hdc, RGB(255, 0, 0));
    SetBkColor(ps.hdc, RGB(0, 255, 0));
    DrawText(ps.hdc, L"\r\nRed Text on Green Background!", -1, &rc, DT_CENTER|DT_VCENTER|DT_WORDBREAK);

    SetBkMode( ps.hdc, TRANSPARENT );
    SetTextColor(ps.hdc, RGB(0, 255, 0));
    DrawText(ps.hdc, L"\r\n\r\nGreen Text on Transparent Background!", -1, &rc, DT_CENTER|DT_VCENTER|DT_WORDBREAK);

    SetBkMode( ps.hdc, OPAQUE );
    SetTextColor(ps.hdc, RGB(0, 255, 0));
    SetBkColor(ps.hdc, RGB(0, 0, 255));
    DrawText(ps.hdc, L"\r\n\r\n\r\nGreen Text on Blue Background!", -1, &rc, DT_CENTER|DT_VCENTER|DT_WORDBREAK);


YES !

Thank you.
Member 15078716 16-Mar-21 22:22pm
   
That worked great.

Now when I move part of my window off of the monitor screen then this whole text disappears. I tried fixing that, but even though it is probably a simple fix, I have not gotten it to work. Should I start another question for that?

Thank you Rick York.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900