views:

486

answers:

2

We have a RichEdit control into which we allow the user to insert an Office MathML equation object.

Basically the logic goes like this: the user click on insert math equation, we allow them to use an external MathML editor, then we will paste an image to represent the equation into the RichEdit control:

' Paste the picture into the RichTextBox.
    SendMessage ctlLastFocus.hwnd, WM_PASTE, 0, 0

Find its position and lock it down using:

 With ctlLastFocus
        'lock the image
        .SelStart = .SelStart - 1
        .SelLength = 1
        .SelProtected = True

It's all nice and good in the beautiful world of ANSI, but we also allow Unicode characters, and what I've noticed is that when you use Chinese characters, the position of the insertion is wrong by half the total position, i.e if it's supposed to be the 7th position now it's inserted at the third.

Basically divided by two, I guess because Unicode takes two bytes as compared to ANSI which requires just one. So because I'm a dummy with no experience with the RTF, RichEdit and Visual Basic 6.

So my question is: can I change the position of the image when I paste it using the sendMessage line?

Or via some other way to control the position of the image inserted into the RichEdit box?

A: 

My approach would be this, if you look at the rtf.SelRTFproperty you'll be able to see exactly what the RTF code is that's creating the visual in the RichTextBox. You can then save that into a file, load it in word and move the image around until it's in the right place, save the file and look at the RTF code again. At that point you should know enough about the combination of chinese or other Unicode languages to build the string manipulation code to do what you want. I'm not entirely sure that every unicode character is 2 bytes - worth checking out if your serious about supporting the full range.

Hope that helps.

MrTelly
A: 

Why not retrieve the position before pasting?

 Dim iStartPos As Long
 Dim iLength As Long
 With ctlLastFocus
        iStartPos = .SelStart
        SendMessage.hwnd, WM_PASTE, 0, 0
        iLength = .SelStart - iStartPos
        .SelStart = iStartPos
        .SelLength = iLength
        .SelProtected = True
 End With
svinto