Click here to Skip to main content
12,942,930 members (46,793 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to change the font of notepad using WM_SETFONT but its not changing.
Here is my code:
        [DllImport("user32.dll", CharSet = CharSet.Auto,SetLastError=true)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateFontIndirect(ref LOGFONT lplf); 
        static void Main(string[] args)
          IntPtr AppHandle=Process.GetProcessesByName("notepad").First().MainWindowHandle;
          LOGFONT t = new LOGFONT();
          t.lfHeight = 30;
          t.lfCharSet = 1;
          var b = new StringBuilder("Tahoma");
          b.Append(' ',b.Capacity-6);
          t.Name =b.ToString();
          IntPtr HFont = CreateFontIndirect(ref t);
          SendMessage(AppHandle, 0x0030,HFont,new IntPtr(1));
    public  struct LOGFONT
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        public int lfOrientation;
        public int lfWeight;
        public byte lfItalic;
        public byte lfUnderline;
        public byte lfStrikeOut;
        public byte lfCharSet;
        public byte lfOutPrecision;
        public byte lfClipPrecision;
        public byte lfQuality;
        public byte lfPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst =32)]
        public String Name;

I am running Windows XP.
GetError() is a method which just prints out the LastError Message.
I monitored the Messages received by notepad using Spy++
On executing above code ,notepad receives WM_SETFONT Message but with system font.
here is the message:WM_SETFONT hfont:650A0D90("System",10pt)fRedraw:True

Things I have tried:
-tried CreateFont() instead of CreateFontIndirect
-Marshaled byte variables in LOGFONT by U1 type
-Tried byte[],stringbuilder,string for Name Variable in LOGFONT.
-tried changing the Charset
-tried WM_ACTIVATE after WM_SETFONT message.
-tried different fonts.
I am getting a valid handle from Notepad and CreateFontIndirect.
SendMessage return 0 but the operation is successful.

Any ideas Guys?
Posted 15-Feb-13 9:50am
Sergey Alexandrovich Kryukov 15-Feb-13 16:08pm
Can you explain what you are doing? .NET already has fonts and everything. Are you really trying to change the font of miserable "Notepad.exe"?! Why?!
You can create a number of much better text editors much faster. :-)
Mike Meinz 15-Feb-13 16:28pm
Use WordPad!
Silent Guardian 15-Feb-13 16:34pm
Hi ,
the above code and notepad is just to test WM_SETFONT.
Actually I am trying to built a visual keyboard where user can select the language he wants and it will automatically set the language font in the applications user is working with.
Using the keyboard, user can easily change language and also see the mapping of letters in the keyboard.
Regional and Language feature is not a good option.

Thanks for looking into my problem. :)

Sergey Alexandrovich Kryukov 15-Feb-13 16:58pm
Sergey Alexandrovich Kryukov 15-Feb-13 17:00pm
It's not always possible as not all applications support Unicode. In my opinion, this is a bad business in general...
You can make a virtual keyboard, which is much simpler and more useful, but changing font is just a bad thing. Why? If an application supports Unicode, you don't need it, if it does not, this activity is uselass anyway.
With a virtual keyboard, what's a problem?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

WM_SETFONT is NOT for setting the applications font selection. It is only for changing the font a window is rendered with, such as the text editor window inside Notepad.

The problem is Notepad maintains it's own font settings and objects, as does every other application in the world, which is overriding what you're sending. Every application has complete control over how its windows are rendered. By sending that message you are NOT telling Notepad to use a certain font. You're telling the window itself, in your case, probably the titlebar text gets changed, and that's not a permanent change. You won't see any changes persist from one instance of Notepad to another.

What you are attempting to do is not possible.
Sergey Alexandrovich Kryukov 15-Feb-13 18:30pm
Sure, a 5. The whole idea is pointless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,633
CHill60 2,880
Maciej Los 2,268
Jochen Arndt 1,900
ppolymorphe 1,755

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100