Using different text colors on the same printed line

Sep 13, 2021 at 8:00pm
If I'm using the default system font, which is not symmetrical, how can I print a list of 3 items per line, where the 3rd item is GREEN?

So, I'm doing a 'lstrcat' to create the line, and I can print out the first 2 items in black, but then I have to figure out what pixel the 3rd item starts. There has to be a better way than that.

Maybe something like this.

Test123 - Test456 - GREEN
Test12 - Test12 - RED
Test123456 - Test123456 - BLUE

I'm using TextOut, but is there a way to create a text line with different color attributes for each character?

Sutton
Sep 13, 2021 at 8:29pm
Drawing Text from Different Fonts on the Same Line
https://docs.microsoft.com/en-us/windows/win32/gdi/drawing-text-from-different-fonts-on-the-same-line

It would probably help you get on the right track.
Sep 13, 2021 at 8:49pm
SetTextColor and SetBkColor could/should work with DrawText/TextOut, I have never had a need so haven't actually tried, but doing it for individual characters is likely to be a VERY tedious and error-prone exercise in head banging futility. You'd make repeated calls to the functions to draw each individual character to the client area's device context.

It might not even be possible with your "XY problem." https://xyproblem.info/

There are ways to draw text on the same line using different fonts. That could be the jumping off point for drawing individual characters with different coloration. Foreground and background.
https://docs.microsoft.com/en-us/windows/win32/gdi/drawing-text-from-different-fonts-on-the-same-line

It still won't be a simple, easy route. Look at what's involved with changing colors in a Win console. That's dealing with characters at a simpler level than fonts in a WinAPI app.
http://www.cplusplus.com/articles/Eyhv0pDG/



Sep 14, 2021 at 6:02am
That's a simple way of changing the color and font of the text:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void printMsg()
{
    // auto detection
    int gdriver = DETECT,gmode,i;
  
    // initialize graphics mode
    initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
  
    for (i=3; i<7; i++)
    {
        // setcolor of cursor
        setcolor(i);
          
        // set text style as
        // settextstyle(font, orientation, size)
        settextstyle(i,0,i);
          
        // print text at coordinate x,y;
        outtextxy(100,20*i,"Geeks");
          
        delay(500);
    } 
    delay(2000);
}
  
// driver program
int main()
{
    printMsg();
    return 0;
}


Ref: https://www.theengineeringprojects.com/2021/10/data-types-variables-and-operators-in-c.html
Last edited on Oct 26, 2021 at 4:36am
Sep 14, 2021 at 6:05am
> initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
The 1980's phoned, and they want their 16-bit DOS back.
Sep 14, 2021 at 1:32pm
elizabethjones wrote:
That's a simple way to....

That's a DOS command-line display hack, not for WinAPI GUI apps.

And one that is seriously outdated as well. BGI? Seriously?

Win consoles aren't DOS command-line. Hooking into a Win console requires some grunt work:
http://www.cplusplus.com/articles/Eyhv0pDG/

For an embedded programmer you really need to get out of the last century.
Sep 14, 2021 at 2:28pm
Thanks for the responses. I think I'll just spread out my display like a table and print out individual items in the various colors. It seems like a real pain to print a text line with different word or phrase colors.
Topic archived. No new replies allowed.