How to print bold string in C++?

10,831

Solution 1

There is no concept of bold text in C++, there may be in a particular device that displays character text, for example rich-text-format or HTML tagging or a terminal screen. The latter usually involves sending some "escape sequence" relevant to that particular terminal.

Solution 2

OK, I've knocked up some code that should give an overview of what you're after, I've not managed to compile it as I'd need to write a lot more to test, but it should point you in the right direction:

// Create the font you need
LOGFONT lf;
zeromemory(&lf, sizeof(LOGFONT))
lf.lfHeight = 20; // 20 pixel high font
lf.lfWeight = FW_BOLD;
strcpy(lf.lfFaceName, "Arial");
HFONT hFont = ::CreateFondIndirect(&lf);

// Set the control to use this font
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT, (WPARAM)hFont, NULL);

I hope this helps.

Share:
10,831
ilija veselica
Author by

ilija veselica

Updated on August 15, 2022

Comments

  • ilija veselica
    ilija veselica over 1 year

    I got an old application which was written in a C++. I have 0 experience with it but I am suppose to make some changes in app. One of them is to change some text. Problem is that part of updated text needs to be bold, but i have no idea how to do that. I googled but with no much success. Only think I now is to go to new line with \nand new tab with \t.

    Any clever advise?

    EDIT:
    Example of code:

    BEGIN
        STRING1                              "First Example"
        STRING2                              "Second Example"
    

    And place where STRING1 is used:

    // WelcomeTip ---------------------------------------------//
        LPSTR idsWelcomeTip = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * 4098 );
        LoadString( waveInDlg->hInstance, STRING1, idsWelcomeTip, 4098 );
        waveInDlg->hwndWelcomeTip = CreateWindow(
            "STATIC",
            idsWelcomeTip,
            WS_CHILD | WS_VISIBLE | SS_LEFT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            waveInDlg->hwnd,
            NULL,
            waveInDlg->hInstance,
            NULL
        );
        SetWindowLongPtr(waveInDlg->hwndWelcomeTip, GWLP_USERDATA ,(LONG)waveInDlg );
        SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT , (WPARAM)waveInDlg->hFontDefault , TRUE );
        ShowWindow(waveInDlg->hwndWelcomeTip, SW_HIDE);
        GlobalFree( (HGLOBAL)idsWelcomeTip );
    

    Thanks,
    Ile

  • ilija veselica
    ilija veselica over 13 years
    The thing is I need only part of string to be bold. For example: First <b>Example</b> (this is how it would look like in HTML). Thank you!
  • John Warlow
    John Warlow over 13 years
    Ah ok, not sure how or if you can do that with a static control. You could do something like that with a read-only rich text control, maybe.
  • MSalters
    MSalters over 13 years
    +1, rich text is the correct solution then. A normal static label is intended for simple cases.
  • MSalters
    MSalters over 13 years
    Which WM_PAINT? "STATIC" is a built-in windows class with its own WndProc.