In one of your samples I see this
What is StdPicture?VB Code:
Public Function PictureToRTF(Pic As StdPicture) As String
Printable View
In one of your samples I see this
What is StdPicture?VB Code:
Public Function PictureToRTF(Pic As StdPicture) As String
StdPicture
VB doesn't open correct page on local MSDN if you press F1 from code window. Open MSDN separately and in the "Index" tab type "StdPicture" to get the offline reference.
Thanks.
@MartinLiss,
RE: RTB Hyperlink problem
As the Wordpad of XP (SP2) can display hyperlinks the way you want, I thought may be we should try using XP's rtf dll.
In XP SP1 or later the dll is msftedit.dll and the class name is RICHEDIT50W. RichEdit version 4.0 (5.0 in XP2 ?).
So, I've created a window of that class and IT WORKED ! :D
But creating a RTB from scratch is a very big task. :(
vbAccelerator RichEdit Control uses similar method to load Riched20.dll (version 2.0 and version 3.0) library using the LoadLibrary function and create a RichEdit window of that version. So, if we load msftedit.dll library, then may be it can create a v4.0 window ?
Unfortunetly, that ocx is not running in my system. Some registry problem. :( (this XP installation is damaged). I've worked with this control before. It used to work smoothly.
So, if you try my idea and modify that control's code, I hope it will work.
BTW, I'm using XP SP2. If the attatched code can't create the window, open Wordpad and get it's classname and try with that class name.
Above method works with VBBox CRichEditCtl.cls
I've used the code inside a usercontrol and it is working.
Now all is left to do is - Modifying properties for this version and Getting the URL. I hope someone can help me on this. :D
Edit:
As far as I can remember, we are not allowed to distribute 'msftedit.dll'. So, even if we create a new RTB control, it will work only in XP. :(
Moeur, I'm using code that I believe I got from your GIFAnimatorV3 project to put smilies in a RichTextbox. I'm using the following code (in part) but when I do the background of the picturebox is not transparent.
Any hints as to where I can look to fix this?VB Code:
Set GIF = New clsGIF With GIF .LoadGIF LoadResData(GIFNumber(mstrSmilies(intTag)), "CUSTOM") ' Clear the picture picSmilies.Picture = LoadPicture .CopyFrame 1, picSmilies.hdc, 10, 10 Set picSmilies.Picture = GIF.Frames(1).Picture End With strRTF = PictureToRTF(picSmilies) mstrRTF = VBA.Left$(mstrRTF, mintSmiliePos - 1) _ & strRTF _ & Right$(mstrRTF, Len(mstrRTF) - (mintSmiliePos - 1 + Len(mstrSmilies(intTag))))
Bill, I've found a serious problem. If you put just the following in a timer (where GIF is member of clsGIF) the app runs out of memory in a minute or so.
VB Code:
With GIF .LoadGIF LoadResData(113, "CUSTOM") rtbMessage.TextRTF = PictureToRTF(.Frames(1).Picture) End With
I don't know why the problem occurs, but if LoadGIF is in a timer then memory(?) gets used up and the program crashes. LoadGIF can be removed from the timer and still allow for animation of a smilie, but only one smilie.
I was able to resolve the problem by doing this
VB Code:
Private GIF() As clsGIF 'instead of Private WithEvents GIF As clsGIF
You can't do WithEvents with an array but apparently it isn't needed (at least in my code). After that do the following outside of the timer
And in the timerVB Code:
If UBound(GIF) < the_nbr_of_gifs_to_animate - 1 Then ReDim Preserve GIF(UBound(GIF) + 1) Set GIF(UBound(GIF)) = New clsGIF End If With GIF(UBound(GIF)) .LoadGIF LoadResData(the_number_of_the_gif), "CUSTOM") End With
VB Code:
For lngIndex = 0 To the_nbr_of_gifs_to_animate - 1 With GIF(lngIndex) tmrGIF.Interval = .Frames(1).DelayTime mbFrameDisplayed = False If iFrame <= .Frames.Count Then ' The following line is simplified. In an app you would want to place ' the picture somplace within the existing TextRTF rather than ' replacing the TextRTF as this does. (PM me for further details) MyRTB.TextRTF = PictureToRTF(.Frames(iFrame).Picture) mbFrameDisplayed = True End If End With Next If mbFrameDisplayed Then iFrame = iFrame + 1 Else iFrame = 1 End If
Marty,
I couldn't reproduce the out of memory error you get, however the solution you came up with is definitely the way to go. You don't want to have to keep reloading the gif.
In the version of my program that I sent you I don't believe that the LoadGIF code at that time was in the Timer.Quote:
Originally Posted by moeur
Have you been able to figure out the non-transparent background problem?
OK, try this. It copies the frame to a picturebox first. The picturebox background color has been set to the same color as the RTB.
Each time you press the button it loads the next smilie from the resource file.
VB Code:
Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function CreateSolidBrush Lib "gdi32" ( _ ByVal crColor As Long _ ) As Long Private Declare Function FillRect Lib "user32" ( _ ByVal hdc As Long, _ lpRect As RECT, _ ByVal hBrush As Long _ ) As Long Private Declare Function SetRect Lib "user32" ( _ lpRect As RECT, _ ByVal X1 As Long, _ ByVal Y1 As Long, _ ByVal X2 As Long, _ ByVal Y2 As Long _ ) As Long Private Declare Function GetSysColor Lib "user32" ( _ ByVal nIndex As Long _ ) As Long Dim GIF As clsGIF Private FrameIndex As Integer Dim hbrBkgnd As Long Dim lpRect As RECT Private Sub Command1_Click() Static index As Integer GIF.LoadGIF LoadResData(101 + index, "CUSTOM") Picture1.Width = GIF.xWidth Picture1.Height = GIF.yHeight hbrBkgnd = CreateSolidBrush(CheckSysColor(Picture1.BackColor)) SetRect lpRect, 0, 0, GIF.xWidth, GIF.yHeight FrameIndex = 0 Timer1.Interval = 50 index = index + 1 If index = 16 Then index = 0 End Sub Private Sub Form_Load() Set GIF = New clsGIF Me.ScaleMode = vbPixels With Picture1 .BorderStyle = 0 .BackColor = RTB.BackColor .AutoRedraw = True .AutoSize = False End With End Sub Private Sub Timer1_Timer() EraseBackground Picture1.hdc With GIF FrameIndex = FrameIndex + 1 If FrameIndex > .Frames.Count Then FrameIndex = 1 .CopyFrame FrameIndex, Picture1.hdc, 0, 0 Picture1.Picture = Picture1.Image End With RTB.TextRTF = PictureToRTF(Picture1.Picture) End Sub Private Sub EraseBackground(hdc As Long) FillRect hdc, lpRect, hbrBkgnd End Sub Public Function CheckSysColor(ByVal ColorRef As Long) As Long 'sometimes VB colors are expressed as system colors 'we need to change this to an RGB color Const HighBit = &H80000000 If ColorRef And HighBit Then CheckSysColor = GetSysColor(ColorRef And Not HighBit) Else CheckSysColor = ColorRef End If End Function
Thanks, I'm working on another issue now and I'll get back to you as soon as I can.
OK, here you go.
Here is a sample project that replaces the tags with the proper animated smilie. The smilies are in gif files so you don't have to mess with resource files.
The project also demonstrates the hyperlink functionality we discussed earlier. this is because everytime you change TextRTF you have to reenable the EN_LINK effect for the hyperlinks. Since the animator messes with TextRTF, hyperlinks have to be updated too.
I'm sure there are some bugs and it can be cleaned up a bit but here it is.
Let me know how it works.
I'll also check this out but I don't find any problem in my code with animation vs. the EN_LINK effect.
Hmm...Quote:
I'll also check this out but I don't find any problem in my code with animation vs. the EN_LINK effect.
I noticed in your code that you do the hyperlinks last. I assumed this was because if you set them first then changed RTB.TextRTF the blue links would go away. This is what happens on my system, so each time the animation timer fires and RTB.TextRTF is updated the links need to be reestablished.
Your soultion in post #51 works. Thanks!
That's a very nice example.Quote:
Originally Posted by moeur
Bill (or anyone). I sometimes store my TextRTF in a string variable, manipulate the string and then replace the TextRTF with the string. Would you have any idea why when the string contains this
blah blah \v\'10s105\'11\v0 \i0
it becomes the following in the TextRTF????
blah blah \v\'10s105\'11\i0\v0
Why was vbcode turned off in this forum? ._.
This is truly a great thread with loads of useful information. I stumbled upon it when I wanted to see what was involved in setting up a RTB to allow hyperlinks. Great stuff, Bill!
In addition to turning a URL you enter into a hyperlink (as in "Auto Detect and respond to URLs" above) you can turn ordinary text into hyperlinked text that points to a URL of your choice.
To do this the text in the Richtextbox has to have its link notification turned on. Then, as before, the form containing the Richtextbox has to be subclassed so that you can respond to user actions. I've created a class that simplifies these tasks.
This class has two events:Clicked - raised when user clicks a hyperlink.And three methods
MouseMove - raised when the user moves the mouse over a hyperlink
Initialize - called once to attach the RTB to the hyperlink classHere is an example of how to use the class
InsertHyperlink - can hyperlink selected text or insert new text and hyperlink it.
RefreshHyperlinks - Unfortunately, whenever the richtextbox's TextRTF property is changed, the RTB will lose all link notification information. To remind the RTB of previously hyperlinked text you must call this method each time the textRTF property is changed.
To respond to user action on the hyperlinks, simply place code in the events.Code:'instantiate and initialize the class
Dim Hypertext As clsHyperText
Set Hypertext = New clsHyperText
Hypertext.Initialize RTB
'hyperlink the word "this" in the Richtextbox
RTB.Find ("this")
Hypertext.InsertHyperlink ("http://www.vbforums.com")
Attached is a demo project which contains the class.Code:Private Sub Hypertext_Clicked(Button As Integer, URL As String, UnderlyingText As String)
'launch the browser here
If Button = vbLeftButton Then
ShellExecute 0&, "OPEN", URL, vbNullString, "C:\", SW_SHOWNORMAL
Else
'popup menu?
Debug.Print URL, UnderlyingText
End If
End Sub
Here is a control that shows you how to add animated GIF images to a Richtextbox. This control was built to replace VBCode smilie tags with their corresponding animated images. It takes advantage of my VB GIF Animator Control and the PictureToRTF function in my above post Insert Pictures.
I placed this functionality on a usercontrol because it uses a timer and an array of picture boxes. The GIFs cannot by placed into the richtextbox transparently, so I change the background of each image to match the background color of the RTB.
The control has two propertiesBackColor - set this property to the backcolor of your Richtextbox to simulate transparency. This property can only be set from code and cannot be changed once pictures have been inserted into the RTB.two methods
FrameInterval - This interval in ms, affects the speed of the animation.LoadIcon - load a GIF file and its corresponding tag into the controland one event
ReplaceTags - Replaces each tag in the Richtextbox text with its corresponding animated image.NextFrame - Raised at each frame advance interval.The following is an example of how you might initialize the controlNow to replace all the tags in the RTB text with the proper smilieCode:'set the picture backgrounds to match the RTB background
GIF.BackColor = RTB.BackColor
'load some icon files along with the corresponding tags
Path = App.Path & "\icons\"
With GIF
.loadIcon ":)", Path & "smile.gif"
.loadIcon ":(", Path & "Frown.gif"
.loadIcon ":o", Path & "redface.gif"
.loadIcon ":D", Path & "BigGrin.gif"
End With
Attached is a demo project which illustrates the proper use of this control.Code:GIF.replaceTags RTB
I've got a problem with insertion of rtf, when the text in the target rtb control is colored and the text/rtf being inserted has some colors also, when inserted the color of the inserted rtf is lost... :-( Any workarounds there? :-)
what do you mean by "insertion of rtf"?
Using your function to insert. :-) For instance, if the target rtb control got some colored text then the rtf being inserted got some colored text also, the color of the rtf being inserted is overridden, the color table is not updated... :-(
what you're saying still does not make sense to me.
what do you mean by my function to insert :-) ?
Do you mean inserting an animated GIF? If so then how can that have colored text?
Please explain in detail or supply example code.
I think he just means that if for example a RichTextBox is red and you pasted red text into it the text could not be seen.
For example, the ff. is the tex of the target rtb: dee-uQuote:
Originally Posted by moeur
And the text being inserted is: moeur
If you will try to insert the rtf of moeur to the rtf of dee-u then the colors of meour is not preserved, it is being overridden. Clear now? :-)
Not clear,
I still don't know how you are trying to insert text.
why don't you just show me the code you are using to insert text?
Ok, here is further explanation since I don't have VB6.0 here.
RichTextbox1 control has the 'dee-u' text (with the colors as in my previous post), RichTextbox2 control has the 'moeur' text (with the colors as in my previous post), get the TextRTF of Richtexbox1 then insert it, using your function to insert rtf, in the RichTextbox2, perhaps in the middle of 'm' and 'o'.... The color of 'dee-u' will be overriden... :-(
I hope I made myself clear already.... :-)
After asking 3 times.
I give up.
If you want to supply code mabye I can help.
Ok, have a look at the attached form... :-)
works ok for me.
Maybe you have an outdated version of the Richtextbox control.
When you click the button and move "dee-u" to rtb2, dee-u takes on the colors of rtb2 rather than maintaining the colors it had in rtb1.
Yup, that is what I am trying to tell... :)Quote:
Originally Posted by MartinLiss
The simplest way to do what you want to do is thisCode:RTB2.SelRTF = RTB1.TextRTF
Moeur, thanks for all of this, it's great and alot of it is what I have been looking for, although I don't confess to totally understand all of the code.
I am working through it slowly. ;)
I have used the hyperlink code and there are a couple of question I have:
Firstly, I would only like to enable the autourldetect under certain circumtances. How can I turn it off?
Secondly, if there is a link in the RTB, if I click anywhere in the RTB it opens the link. Is this how you planned it?
I have other text in the RTB which I would be editing and when I click somewhere in the RTB, it opens the link.
If the non-link text is first in the RTB it's ok, but if I click anywhere after the link, where there is no text, just space, it still selects the link and opens the link.
I hope you can follow what I am trying to explain. :rolleyes:
I have just done a straight copy of your code in the example project, with the exception of this line which is not in the Form_Load, but in a treeview node_click.
vb Code:
EnableAutoURLDetection RTB
I have now used the following to turn off the detection.
I just wondered, being new to subclassing, is this ok, or do I need anything else.
It seems to work ok. :D
vb Code:
Public Sub DisableAutoURLDetection(rtb As RichTextBox) Set FormSubClass = Nothing End Sub
try this insteadAs to your other problem:Code:Public Sub DisableAutoURLDetection(RTB As RichTextBox)
'disable auto URL detection
SendMessage RTB.hwnd, EM_AUTOURLDETECT, 0&, ByVal 0&
'turn off subclassing
FormSubClass.Unsubclass
End Sub
I only see this when the hyperlink is the very last text in the richTextBox. This is a bug in the richtextbox itself. To eliminate this problem, put something after the hyperlink even just a new line.
Thanks moeur.Quote:
Originally Posted by moeur
I will change the Disable code.
Is this to change the message from the RTB back to, I guess the form?
I eventually realised that the way to do this was to put the link first, then the text afterwards. :D
Thanks for all your hard work.