How to get font from winform control of another application?

When the Handle of the control of another application is known,
I can get Font object of a win32 control of another application(Different process) by SendMessage(hwnd,WM_GETFONT....) & Graphics class.

But when the target control is a WinForm control(.Net control) of another application, I can't get the HFont from SendMessage(hwnd,WM_GETFONT).
So, I guess the font is not stored in the Device Context(HDC) for a winForm control. Am I Right
Then How Can I get the Font from that winForm control

The purpose to do so is that I want to measure the string size of controls in other applications.

Anyone has idea



Answer this question

How to get font from winform control of another application?

  • ian field

    I assume that you have a handle to a control.(IntPtr)

    Control c = Control.FromHandle(handle);

    c.Font (when c is the Control)gives you the font object for the control.I mean if you know that the target control is a WinForms control,then you can get the Font by directly querying the property.

    Once you have the Font,measuring a string is trivial,if you have access to the Graphics object of the control(Graphics.FromHandle)

    g.MeasureString(text,font);

    the MeasureString has several overloads which are very useful.

    Hope this solves the problem.

     

     


  • ros_amir

    Thanks for your help, Karthik Krishnaswami!

    This method is the first try I had.

    For my program and other application are differen process,

    Control c = Control.FromHandle(handle); Will return null.

    Anyway, Thanks!

    Any other idea


  • How to get font from winform control of another application?