Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Two (2) questions that I need advise on at the same time.


I have been trying to put text to a screen buffer and then to bitblt the screen buffer to the screen. It works, but I would like some advice on it.

How I place the text to the screen buffer is my question.

Am I doing this correct?

(1) Is DrawText what I should be using?
And
I tried to get the handle of the front buffer from its HDC,
to use it here instead of the handle of the main window, which I use here:
RECT rc;
GetClientRect(Handle_of_MainWindow, &rc);

but all attempts have failed.
(2) How do I get the handle of the front buffer if all that I have its HDC, which has not worked for me?

What I have tried:

I have the following where I draw text to a previous buffer then bitblt from a buffer to the screen. It works, I think.

I am not certain if I am doing this correct. Would you please look at it and advise me?


void Draw_From_FRONT_BUFFER_001_To_MainWindow()
    {
        Processing_A_Buffer_Update = 1;

        HDC_of_MainWindow = GetDC( Handle_of_MainWindow ) ;


/// current attempt

    // Created earlier and saved as a Global.
    // HDC_of_FRONT_BUFFER_001 = CreateCompatibleDC( HDC_of_MainWindow );
    // The FRONT_BUFFER_001 is the same size as the MainWindow, so I am using the MainWindow handle. But it could be a different size, so what do I do for that?

    PAINTSTRUCT	ps;

    RECT rc;
    GetClientRect(Handle_of_MainWindow, &rc);

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


/// current attempt


        // Now copy the FRONT_BUFFER_001 to the MainWindow.
        BitBlt
            (
                HDC_of_MainWindow,	// destination buffer, in this case the front buffer.
                0,                  // Horizontal start for drawing.
                0,	                // Vertical   start for drawing.
                FRONT_BUFFER_001_WIDTH,       // Width to draw.
                FRONT_BUFFER_001_HEIGHT,       // Height to draw.
                HDC_of_FRONT_BUFFER_001,  // SOURCE buffer.  We are taking from that back canvas.
                0,                  // Horizontal start copying from.
                0,		            // Vertical   start copying from.
                SRCCOPY             // Just want a straight copy.
            );

        ReleaseDC( Handle_of_MainWindow, HDC_of_MainWindow ) ;

        Processing_A_Buffer_Update = 0;
    }



I tried to use the handle of the FRONT_BUFFER_001 so that I could write text to that buffer in case I had used a different width or height than for the MainWindow but that did not work. Here is my attempt that did not work. No text shows up.
HWND Handle_of_FrontBuffer001 = WindowFromDC(HDC_of_FRONT_BUFFER_001);

    RECT rc;
    GetClientRect(Handle_of_FrontBuffer001, &rc);


That is why I am using the handle of the main window like this
RECT rc;
GetClientRect(Handle_of_MainWindow, &rc);



Please advise.

Thank you.
Posted
Updated 18-Mar-21 20:18pm
v2
Comments
Richard MacCutchan 19-Mar-21 6:09am    
You can only update the screen when responding to a WM_PAINT message. If you do it at any other time then it will be cleared as soon as a WM_PAINT message is sent. And if you do it properly you can write direct to the main Window, you do not need BitBlt.

1 solution

Please learn some basics like frome this GDI Drawing and Printing or / and this GDI tutorial and also use the naming conventions. This helps other people to understand your code.

some tips:
- use the standard naming conventions
- work ONLY at one HDC one time
- release EVERY resource when done
- read tutorials and documentations
 
Share this answer
 

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