Tai-San
asked on
replacing text in a textbox with pictures
id like to make a program that converts a string to hiragana, i have all the pictures but i just need a piece of code that can replace tow or three characters at a time in a rich text box.
hitori ja nai yo
() means that its picture so it will become
(HI)(TO)(RI) (JA) (NA)(I) (YO)
kono mama, atenado nakute ii
(KO)(NO) (MA)(MA), (A)(TE)(NA)(DO) (NA)(KU)(TE) (I)(I)
as you can see it replaces the text with pictures, is this possible
hitori ja nai yo
() means that its picture so it will become
(HI)(TO)(RI) (JA) (NA)(I) (YO)
kono mama, atenado nakute ii
(KO)(NO) (MA)(MA), (A)(TE)(NA)(DO) (NA)(KU)(TE) (I)(I)
as you can see it replaces the text with pictures, is this possible
listening...
Yes.. could you post a list of all letter combinations that will be replaced by a picture.
There is an easy way to do this.
Rename all the pictures to the strings they represent eg. (KO) should be KO.bmp
and put them in the same directory
This code has no error handling, will work only if images exist in the given path
and if valid input strings are given in Text1
Option Explicit
Const IMAGES_DIRECTORY = "C:\images\"
Const iImageW = 32
Const iImageH = 32
Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim tempBfr As String
x = 1
y = iImageH 'picture height
tempBfr = ""
For i = 1 To Len(Text1.Text)
tempBfr = tempBfr & Mid(Text1.Text, i, 1)
If FileExists(IMAGES_DIRECTOR Y & tempBfr) Then
Picture1.PaintPicture LoadPicture("C:\a.bmp"), x, y
tempBfr = ""
x = x + iImageW
'y remains the same if you place picures side by side
End If
Next
End Sub
Private Function FileExists(sFile As String) As Boolean
If UCase(Dir(sFile)) = UCase(sFile) Then
FileExists = True
Else
FileExists = False
End If
End Function
There is an easy way to do this.
Rename all the pictures to the strings they represent eg. (KO) should be KO.bmp
and put them in the same directory
This code has no error handling, will work only if images exist in the given path
and if valid input strings are given in Text1
Option Explicit
Const IMAGES_DIRECTORY = "C:\images\"
Const iImageW = 32
Const iImageH = 32
Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim tempBfr As String
x = 1
y = iImageH 'picture height
tempBfr = ""
For i = 1 To Len(Text1.Text)
tempBfr = tempBfr & Mid(Text1.Text, i, 1)
If FileExists(IMAGES_DIRECTOR
Picture1.PaintPicture LoadPicture("C:\a.bmp"), x, y
tempBfr = ""
x = x + iImageW
'y remains the same if you place picures side by side
End If
Next
End Sub
Private Function FileExists(sFile As String) As Boolean
If UCase(Dir(sFile)) = UCase(sFile) Then
FileExists = True
Else
FileExists = False
End If
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Took a little while to come up with a working example. In the below example, you'll need to change the following constants to test it for yourself: ImageDir, ImageExt, Image1, Image2 and Image3. You may also want to add more constants of Image* (*=#) or use an array. The below example selects the word it found an image to, and replaces it by pasting the image for it.
Form1:
---------------
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_PASTE = &H302
Private Const ImageDir As String = "C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bit maps\Assor ted\"
Private Const ImageExt As String = ".bmp"
Private Const Image1 As String = "club"
Private Const Image2 As String = "cup"
Private Const Image3 As String = "hand"
Private Sub Form_Load()
RichTextBox1.Text = ""
End Sub
Private Sub RichTextBox1_Change()
Call ConvertWords(RichTextBox1)
End Sub
Private Sub ConvertWords(ByVal RichTextBox As RichTextBox)
Dim strArray() As String, iLoop As Integer, iStart As Integer, iEnd As Integer
strArray = Split(RichTextBox.Text, " ")
For iLoop = LBound(strArray) To UBound(strArray)
iEnd = iEnd + Len(strArray(iLoop)) + IIf(iLoop < UBound(strArray), 1, 0)
iStart = iEnd - Len(strArray(iLoop))
Select Case LCase(strArray(iLoop))
Case Image1
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image1 & ImageExt, RichTextBox.hwnd)
Case Image2
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image2 & ImageExt, RichTextBox.hwnd)
Case Image3
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image3 & ImageExt, RichTextBox.hwnd)
End Select
Next iLoop
End Sub
Private Sub PasteImage(ByVal strPath As String, ByVal lHandle As Long)
Clipboard.Clear
Call Clipboard.SetData(LoadPict ure(strPat h))
Call SendMessage(lHandle, WM_PASTE, 0, 0)
End Sub
-----------
If you have the folder (VS6) I used for testing, then you can test it by typing one of the Image* strings such as "club", "cup" or "hand". When you space it, the word will be replaced by the image. This may be what you're looking for, if not, sorry. :)
Form1:
---------------
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_PASTE = &H302
Private Const ImageDir As String = "C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bit
Private Const ImageExt As String = ".bmp"
Private Const Image1 As String = "club"
Private Const Image2 As String = "cup"
Private Const Image3 As String = "hand"
Private Sub Form_Load()
RichTextBox1.Text = ""
End Sub
Private Sub RichTextBox1_Change()
Call ConvertWords(RichTextBox1)
End Sub
Private Sub ConvertWords(ByVal RichTextBox As RichTextBox)
Dim strArray() As String, iLoop As Integer, iStart As Integer, iEnd As Integer
strArray = Split(RichTextBox.Text, " ")
For iLoop = LBound(strArray) To UBound(strArray)
iEnd = iEnd + Len(strArray(iLoop)) + IIf(iLoop < UBound(strArray), 1, 0)
iStart = iEnd - Len(strArray(iLoop))
Select Case LCase(strArray(iLoop))
Case Image1
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image1 & ImageExt, RichTextBox.hwnd)
Case Image2
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image2 & ImageExt, RichTextBox.hwnd)
Case Image3
RichTextBox.SelStart = iStart
RichTextBox.SelLength = iEnd
Call PasteImage(ImageDir & Image3 & ImageExt, RichTextBox.hwnd)
End Select
Next iLoop
End Sub
Private Sub PasteImage(ByVal strPath As String, ByVal lHandle As Long)
Clipboard.Clear
Call Clipboard.SetData(LoadPict
Call SendMessage(lHandle, WM_PASTE, 0, 0)
End Sub
-----------
If you have the folder (VS6) I used for testing, then you can test it by typing one of the Image* strings such as "club", "cup" or "hand". When you space it, the word will be replaced by the image. This may be what you're looking for, if not, sorry. :)