How to "adjust" RichEdit control to accept fractions?

234 views Asked by At

I have read the official docs but still was not able to figure this out.

Since English is not my native, I have a hard time constructing effective Google query. I am still trying though...

I would like to enter fractions into RichEdit control, something like the image below:

enter image description here

Can this be done? If so, can you show me how, on a smallest possible example?

1

There are 1 answers

3
DavidK On

I don't think that you're going to get this to work with RichEdit - later versions do have at least some math display support, but as I understand it they don't handle direct input of it. At least, I couldn't find anything online that described how you might enter a formula.

If you can rely on clients having at least Windows 7, you could pop up the Math Input Panel, which will put the resulting equation in MathML format on the clipboard - see, for example, this blog post. Whether that really helps or not depends on what you want to do with the resulting equation. Without more context it's impossible to say.

EDIT1: A few more suggestions occur to me. If you can rely on Microsoft Office being installed you could access the Microsoft Equation Editor from OLE. The catch there is that that's old and there is a danger they might drop it, as it's only now present to allow equations in old Word documents to be edited.

A more interesting approach would be to allow user input in some form (e.g. LaTeX) and then process it to produce an image. For example, you could run the equation through LaTeX in the background, as this website does, or you could investigate MathJax, which is a Javascript library to render equations - you could run this in an MSHTML instance. None of these are particularly easy, of course.

EDIT2: If you can rely on RichEdit 8 (for which you'll need either Windows 8, or a recent Office version), you do get some math support in RichEdit. With some testing, I can turn the linear form entry of "M_0,3/b" into this:

Example equation

This used the following code (a recent Windows SDK is needed) to build up the math zone from the "M_0,3/b" text:

  CWnd* edit = GetDlgItem(IDC_RICHEDIT);
  CComPtr<IRichEditOle> reo;
  edit->SendMessage(EM_GETOLEINTERFACE,0,(LPARAM)&reo);
  CComQIPtr<ITextDocument2> doc = reo;
  CComPtr<ITextRange2> range;
  doc->Range2(0,0,&range);
  range->Expand(tomStory,0);
  range->BuildUpMath(0);