| {\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}} {\colortbl ;\red0\green0\blue255;} \viewkind4\uc1\pard\f0\fs17 The following code will show you a fast and easy way to handle messages (including smileys/emoticons) for a chat window. Before using this code, you'll a few things on your form: 1) A RichTextBox control (RTB) 2) A Picturebox (Picture1) 3) An array of Images (imgIcon()) RTB is the name of the RichTextBox for the chat window. Pictuer1 is simply a buffer to hold whatever content was originally in the Clipboard if you desire to not permanently write over it. The imgIcon array is for the smiley faces. Each control in the array should be loaded with the desired icon beforehand and its tag should contain the string of characters that will be turned into the icon (delimited by spaces) Final Note: I tried to keep this code as simple as possible so it can focus on just showing how to add in the picture files and not be cluttered with other things. Please let me know if there's anything that require further explanation. \par \par Before using this code, you'll a few things On your form: \par 1) A RichTextBox control (RTB) \par 2) A Picturebox (Picture1) \par 3) An array of Images (imgIcon()) \par RTB is the name of the RichTextBox For the chat window. \par Pictuer1 is simply a buffer To hold whatever content was originally in the Clipboard If you desire to Not permanently write over it. \par The imgIcon array is For the smiley faces. Each control In the array should be loaded With the desired icon beforehand and its tag should contain the String of characters that will be turned into the icon (delimited by spaces) \par Final Note: I tried To keep this code as simple as possible so it can focus on just showing how to add in the picture files and not be cluttered With other things. Please Let me know If there's anything that require further explanation. \par \par \par \par \cf1 Public Sub ChatMessage(ByVal sUser As String, ByVal sMessage As String)\cf0 \par Dim lImagePos As Long \par Dim lStartMessage As Long \par Dim i As Integer, iVC As Integer \par Dim ValidCodes() As String \par Dim ClipboardContents As Variant \par Dim bClipHasImage As Boolean \par \par bClipHasImage = Clipboard.GetFormat(vbCFBitmap) \par If bClipHasImage Then Picture1.Picture = Clipboard.GetData \par 'RTB must be unlocked for the SendMessage call to work \par RTB.Locked = False \par RTB.SelStart = Len(RichTextBox1.Text) \par \par RTB.SelBold = True \par RTB.SelText = sUser & ": " \par \par lStartMessage = Len(RTB.Text) - 1 \par \par RTB.SelBold = False \par RTB.SelText = sMessage & vbCrLf \par \par For i = 0 To imgIcon.count - 1 \par ValidCodes = Split(imgIcon(i).Tag, " ") \par \par For iVC = 0 To UBound(ValidCodes) \par lImagePos = InStr(lStartMessage, RTB.Text, ValidCodes(iVC)) \par \par \par While lImagePos > 0 \par 'Clear away the text that represented the smiley \par RTB.SelStart = lImagePos - 1 \par RTB.SelLength = Len(ValidCodes(iVC)) \par RTB.SelText = "" \par \par 'Clear the clipboard to ensure that you get the desired data in there \par Clipboard.Clear \par 'Set the smiley to the clipboard \par Clipboard.SetData imgIcon(i).Picture \par 'Paste the clipboard to the RTB \par SendMessage RTB.hWnd, WM_PASTE, 0, 0 \par \par 'Search for another of the same smiley \par lImagePos = InStr(lImagePos, RTB.Text, ValidCodes(iVC)) \par Wend \par Next iVC \par Next i \par \par 'Re-lock the RTB since we're done adding images \par RTB.Locked = True \par 'Put the old contents of the clipboard back \par If bClipHasImage Then Clipboard.SetData Picture1.Picture \par \par 'Set the cursor to the bottom of the RTB \par RTB.SelStart = Len(RTB.Text) \par \cf1 End Sub\cf0 \par } |
Add Images to Rich Textbox Controls |
India web developer web development India | India web development company India ecommerce web developer