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);
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?