locked
Font to LOGFONTW issue RRS feed

  • Question

  • I have a std::string font name I want to copy into a LOGFONTW (as shown below) but when I display the lf.lfFaceName it doesn't seem to take the font name, despite not reporting any errors....

    Any ideas what I'm doing wrong here?

         std::wstring fontname=registryfontname;

         wcscpy_s(lffont.lfFaceName,fontname);

    I have tried:

             std::wstring wtxt=strtowstr(checklength(b,LF_FACESIZE));
             //fontname=wtxt.c_str();
             lf.lfFaceName[0]=(WCHAR)wtxt.c_str();
             //wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);

    and none of those combinations seem to work...





    • Edited by TallGuy63 Saturday, May 21, 2016 5:42 PM
    Friday, May 20, 2016 11:49 PM

Answers

  • I fixed the problem by using a constant wchar_t...

    For some reason the 64-bit requires it.  It works fine now. Here's the working code:

             const wchar_t * fontname;

             std::string b=registryfontname;
             std::wstring wtxt=strtowstr(b);
             fontname=wtxt.c_str();
             wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);


    • Edited by TallGuy63 Saturday, May 21, 2016 5:44 PM
    • Marked as answer by TallGuy63 Saturday, May 21, 2016 5:44 PM
    Saturday, May 21, 2016 5:39 PM

All replies

  • On 5/20/2016 7:49 PM, TallGuy63 wrote:

    I have a std::string font name I want to copy into a LOGFONTW (as shown below) but when I display the lf.lfFaceName it doesn't seem to take the font name, despite not reporting any errors....

    What do you mean, display the lf.lfFaceName? Display where and how?

         std::wstring wfacename=strtowstr(facename);

    What's strtowstr ?

    Saturday, May 21, 2016 12:04 AM
  • I write the font name into the registry and it writes in ok, but when read the font and try to write it into a LOGFONTW to create the actual font, when I try to read lf.lfFaceName after assigning the LOGFONTW, it doesn't read any characters: its blank.  I use this:

             //initialize logfontw settings

             lf.lfEscapement=0;
             lf.lfOrientation=0;
             lf.lfWeight=FW_NORMAL;
             lf.lfItalic=0;
             lf.lfUnderline=0;
             lf.lfStrikeOut=0;
             lf.lfCharSet=ANSI_CHARSET;
             lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
             lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
             lf.lfQuality=DEFAULT_QUALITY;
             lf.lfPitchAndFamily=FF_DONTCARE;

             wtxt=strtowstr(checklength(fontnamefromregistry,LF_FACESIZE));
             //fontname=wtxt.c_str();
             lf.lfFaceName[0]=(WCHAR)wtxt.c_str();
             //wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);

             lf.lfHeight=(long)sval(fontsize);
             lf.lfWidth=floor((double)sval(fontsize)/2);

             hfont=CreateFontIndirect(&lf);

    the strtowstr is a std::string to std::wstring translator.  That works fine.


    • Edited by TallGuy63 Saturday, May 21, 2016 12:14 AM
    Saturday, May 21, 2016 12:13 AM
  • On 5/20/2016 8:13 PM, TallGuy63 wrote:

    I write the font name into the registry and it writes in ok, but when read the font and try to write it into a LOGFONTW to create the actual font, when I try to read lf.lfFaceName after assigning the LOGFONTW, it doesn't read any characters: its blank.

    What do you mean, "it doesn't read"? Who doesn't read what? How exactly do you observe lf.lfFaceName?

             wtxt=strtowstr(checklength(fontnamefromregistry,LF_FACESIZE));
             //fontname=wtxt.c_str();
             lf.lfFaceName[0]=(WCHAR)wtxt.c_str();

    This makes no sense whatsoever.

             //wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);

    This should work, assuming wtxt has the correct value.

    the strtowstr is a std::string to std::wstring translator.  That works fine.

    What makes you so sure?

    Saturday, May 21, 2016 12:27 AM
  • //wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);

    This should work, assuming wtxt has the correct value.

    Yes I thought so too...

    the strtowstr is a std::string to std::wstring translator.  That works fine.

    What makes you so sure?

    Because I use it throughout the program and have tested the return values.

    Believe me; it works.

    I'm going to try some more variations....

    Saturday, May 21, 2016 2:17 AM
  •          wtxt=strtowstr(checklength(fontnamefromregistry,LF_FACESIZE));

    If strtowstr is flawless then perhaps the functions checklength and/or fontanamefromregistry are the culprits?  You should also confirm that the code used to save the font name to the registry works properly.

    • Edited by RLWA32 Saturday, May 21, 2016 10:48 AM
    Saturday, May 21, 2016 10:47 AM
  • I already verified that the fonts are bei ng written correctly to the registry - and being read correctly as well

    Saturday, May 21, 2016 4:38 PM
  • I fixed the problem by using a constant wchar_t...

    For some reason the 64-bit requires it.  It works fine now. Here's the working code:

             const wchar_t * fontname;

             std::string b=registryfontname;
             std::wstring wtxt=strtowstr(b);
             fontname=wtxt.c_str();
             wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);


    • Edited by TallGuy63 Saturday, May 21, 2016 5:44 PM
    • Marked as answer by TallGuy63 Saturday, May 21, 2016 5:44 PM
    Saturday, May 21, 2016 5:39 PM
  • I fixed the problem by using a constant wchar_t...

    For some reason the 64-bit requires it.  It works fine now. Here's the working code:

             const wchar_t * fontname;

             std::string b=registryfontname;
             std::wstring wtxt=strtowstr(b);
             fontname=wtxt.c_str();
             wcscpy_s(lf.lfFaceName,LF_FACESIZE,fontname);

    Well yes, wstring::c_str() returns const wchar_t*. This has nothing to do with 64-bit versus 32-bit compilation.

    BTW, you should be careful caching the return value of c_str() into a variable, because it will become invalid if the wstring goes out of scope. Better would be

    std::string b = registryfontname;
    std::wstring wtxt = strtowstr(b);
    wcscpy_s(lf.lfFaceName, LF_FACESIZE, wtxt.c_str());
    

    Two unnecessary (and potentially dangerous) lines of code removed.

    BTW, your code would be much more readable if you put spaces around '=' and after ','.


    David Wilkinson | Visual C++ MVP

    Saturday, May 21, 2016 7:46 PM
  • Actually, I've been programming since 1988 (in DOS back then), and been a professional programmer since 1990, and I read code easier the way I have it. (And I'm the one who has to read it! LOL)

    I just haven't ever programmed this much in C since the early 90s... so I'm getting back in the hang of this language.  Lots of changes from the "C" I knew on mainframe



    • Edited by TallGuy63 Saturday, May 21, 2016 8:01 PM
    Saturday, May 21, 2016 7:53 PM
  • Actually, I've been programming since 1988 (in DOS back then), and been a professional programmer since 1990, and I read code easier the way I have it. (And I'm the one who has to read it! LOL)


    I have been programming since 1965 (in Fortran until around 1995) and I am only a recent convert to spacing. But I would never go back. But to each his/her own.

    David Wilkinson | Visual C++ MVP

    Saturday, May 21, 2016 9:43 PM