Writing a HFONT to file.

This is a discussion on Writing a HFONT to file. within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm just wondering how the best way to write a HFONT variable into a file is. My initial thought ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18

    Writing a HFONT to file.

    Hi, I'm just wondering how the best way to write a HFONT variable into a file is.
    My initial thought was to use an ifstream but obviously I cannot stream a HFONT data type. So should I typecast to something else? I'm really not sure. Any help would be great.

    Thanks for any replies.
    SwitchCase

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A Windows "handle" is a temporal object - it is not a permanent variable that you can re-use, so I don't really see the point of storing it in a file.

    If you want to output it as a debug value, then you can use a cast to long, such as static_cast<long>myfont. But storing a HFONT value in a file is not meaningfull in general, becasue the value you get back is related to some data that the OS is holding for you, and it needs to be created for you.

    You could save the data used for CreateFont if you like, but saving the HFONT that you get back is about as useful as saving a FILE pointer from fopen() - once the application has finished (or the file is closed), the FILE pointer is meaningless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    18
    Quote Originally Posted by matsp View Post
    A Windows "handle" is a temporal object - it is not a permanent variable that you can re-use, so I don't really see the point of storing it in a file.

    If you want to output it as a debug value, then you can use a cast to long, such as static_cast<long>myfont. But storing a HFONT value in a file is not meaningfull in general, becasue the value you get back is related to some data that the OS is holding for you, and it needs to be created for you.

    You could save the data used for CreateFont if you like, but saving the HFONT that you get back is about as useful as saving a FILE pointer from fopen() - once the application has finished (or the file is closed), the FILE pointer is meaningless.

    --
    Mats

    I see. Thanks for the help, I didn't actually think about that. That probably would have ended up causing hours of frustration. seems I'll have to store th variables passed to CreateFont instead.

    Thanks again.
    SwitchCase

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by switchcase View Post
    I see. Thanks for the help, I didn't actually think about that. That probably would have ended up causing hours of frustration. seems I'll have to store th variables passed to CreateFont instead.

    Thanks again.
    Yes, that makes more sense.

    Of course, some of them are probably "meaningless to save" because there are only one value that "makes sense".

    If you want to just have a few different fonts that you use within your application, you could build a table of which fonts you use [with the relevant values to send to CreateFont], and then save the index of the table or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    You can write HFONT to a file...it's the same saving a HDC or HBITMAP to a file...you must be know the .ttf or .fon files format
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Joelito View Post
    You can write HFONT to a file...it's the same saving a HDC or HBITMAP to a file...you must be know the .ttf or .fon files format
    I don't think creating another font defition file was what the original poster actually was after - and whilst it is of course possible to write a file in the suitable format to use as a font, it doesn't seem that Windows actually has an API to do that. See: http://msdn2.microsoft.com/en-us/library/ms534233.aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by switchcase View Post
    seems I'll have to store th variables passed to CreateFont instead.
    Alternatively, you can use GetObject on your font handle to fill a LOGFONT struct and then save its contents to file. When you load the LOGFONT from file, use CreateFontIndirect to re-create the original font - assuming that font exists on the target system (enumerating the fonts might be useful in this respect).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21