Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am trying to change the font of notepad using WM_SETFONT but its not changing.
Here is my code:
C#
    [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);
      GetError();
      SendMessage(AppHandle, 0x0030,HFont,new IntPtr(1));
      GetError();
    }

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
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
Comments
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. :-)
—SA
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 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?
—SA
Mike Meinz 15-Feb-13 16:28pm    
Use WordPad!
Sergey Alexandrovich Kryukov 15-Feb-13 16:58pm    
:-)

1 solution

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-13 18:30pm    
Sure, a 5. The whole idea is pointless.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900