| | |
Need help with AddFontResource
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 12
Reputation:
Solved Threads: 0
Hey, it's me again. This time with a thing that has been bugging me for months and i still haven't gotten around to solving it.
I need to install a font without rebooting the computer. Sounds simple but i have to do it in code or with a batch file, popping windows in front of the user isn't an option. I've done a lot of trials on batch files and that fontinst.exe crap, nothing worked. Somehow i stumbled upon the MSDN documentation of AddFontResource() which sounds like the thing for me. I understand i also have to use SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) at the end so the fonts are refreshed in all windows.
I chose to do it in C++ because i also learn it at school (i'm in 11th grade). What we learn is the old Borland stuff but still it is of some use. Anyway, i really need to wrap up all those batches and javascripts i wrote in one single program with as little dependencies as possible, and C++ seems to be the best choice.
I'm currently using Dev-C++ 5 beta.
I wrote that just to test the function. The problem with the above code is that... It does nothing.
It compiles and runs, but the font is still not registered to the system until i reboot. Prior to running this program the font is copied to the Windows\Fonts folder and the necessary registry entries are created, via a batch file.
I need some explanation on the LPCTSTR thing. What the heck is it exactly? It seems it's not a string as it doesn't take the name stored in "myfont". The MSDN entry is very very vague.
Uhh... what??? I really don't understand the part in bold. Could someone shed some light on this please?
I need to install a font without rebooting the computer. Sounds simple but i have to do it in code or with a batch file, popping windows in front of the user isn't an option. I've done a lot of trials on batch files and that fontinst.exe crap, nothing worked. Somehow i stumbled upon the MSDN documentation of AddFontResource() which sounds like the thing for me. I understand i also have to use SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) at the end so the fonts are refreshed in all windows.
I chose to do it in C++ because i also learn it at school (i'm in 11th grade). What we learn is the old Borland stuff but still it is of some use. Anyway, i really need to wrap up all those batches and javascripts i wrote in one single program with as little dependencies as possible, and C++ seems to be the best choice.
I'm currently using Dev-C++ 5 beta.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <windows.h> using namespace std; int main(int argc, char *argv[]) { string myfont; cout<<"Gimme my font: ";cin>>myfont; int AddFontResource( LPCTSTR myfont // font file name ); SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); system("PAUSE"); return EXIT_SUCCESS; }
I wrote that just to test the function. The problem with the above code is that... It does nothing.
It compiles and runs, but the font is still not registered to the system until i reboot. Prior to running this program the font is copied to the Windows\Fonts folder and the necessary registry entries are created, via a batch file.I need some explanation on the LPCTSTR thing. What the heck is it exactly? It seems it's not a string as it doesn't take the name stored in "myfont". The MSDN entry is very very vague.
•
•
•
•
The AddFontResource function adds the font resource from the specified file to the system font table. The font can subsequently be used for text output by any application.
int AddFontResource(
LPCTSTR lpszFilename // font file name
);
Parameters
lpszFilename
[in] Pointer to a null-terminated character string that contains a valid font file name. This parameter can specify any of the following files.
•
•
Join Date: Aug 2008
Posts: 12
Reputation:
Solved Threads: 0
I'm a newbie, so i make newbie mistakes. I didn't understand Microsoft's notations and thought LPCTSTR was a parameter, while it was actually the LPCTSTR datatype. I got it solved like this:
And adding -lgdi32 to the Dev-Cpp linker's parameters. Damn, it was that simple.
Off to make it read the font name from an INI file and wrap up the rest of the batches.
c++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <windows.h> using namespace std; int main(int argc, char *argv[]) { char filename[255]; cout<<"Gimme my font: ";cin>>filename; LPCTSTR myFont = filename; AddFontResource(myFont); SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); system("PAUSE"); return EXIT_SUCCESS; }
And adding -lgdi32 to the Dev-Cpp linker's parameters. Damn, it was that simple.
Off to make it read the font name from an INI file and wrap up the rest of the batches.
Last edited by Th3_uN1Qu3; Oct 8th, 2008 at 12:16 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Preprocessor #warning Command Question
- Next Thread: Parse Error Before 'else'
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






