问题
I have been working on a Chat HTA file to play with at work...
:)
It works pretty good, but someone asked to colour each person text a different colour ?
I have been trying to get it but im having trouble, can someone help me out if possible ?
below is the basic code for what i have done\found
i got as far as putting each line into a array but then to assign a colour to each line i cant work out...
OR
if i cant display the "chat" in the windows with out the use of a text file then that would be good\better (saves cleaning up deleting unwanted text files..)
Please also tell me how to better my coding...i havent been doing this that long...(few months)
Cheers Pavle.
' HTA Chat
<HTA:APPLICATION
SCROLL="auto"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
>
</head>
<SCRIPT Language="VBScript">
Sub Window_OnLoad
Window.ResizeTo 400,300
iTimerID = window.setInterval("Display", 100)
End Sub
strPath = "C:\Users\Pavle\Desktop\"
Set wshShell = CreateObject( "WScript.Shell" )
strSender = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
Sub Display
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(StrPath & "Chat.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.ReadAll
Loop
objFile.Close
DisplayBox.Value = strCharacters
DisplayBox.ScrollTop = DisplayBox.ScrollHeight
' This splits each line.
' sArray = Split(DisplayBox.Value, vbcrlf)
End Sub
Sub InputBox
With document.parentWindow.event
If .keycode = 13 then
Const NORMAL_WINDOW = 1
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(StrPath & "Chat.txt", ForAppending, True)
objTextFile.WriteLine strSender & ":" & Input.Value
objTextFile.Close
Input.Value = ""
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{BACKSPACE}"
Else
.cancelbubble = false
.returnvalue = true
End If
End With
End Sub
</SCRIPT>
<body>
<textarea name="DisplayBox" rows="6" cols="40"></textarea>
<BR>
<BR>
<BR>
<textarea name="Input" rows="3" onKeydown=InputBox cols="40"></textarea>
</body>
</html>`
回答1:
Here's some guidelines for your task.
At first, textarea can't have HTML, so it can't be used as a pad for rich text. You need a <pre>
or rather a <div>
for this purpose. For example:
<div name="DisplayBox" class="textpad" contenteditable="true"></div>
Then add formatting tags for the lines while reading the file. Something like this:
Dim n
n = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(StrPath & "Chat.txt", 1)
Do Until objFile.AtEndOfStream
n = n * (-1)
strCharacters = strCharacters & "<p class='line" & n & "'>" & objFile.ReadLine() & "</p>"
Loop
objFile.Close
DisplayBox.innerHTML = strCharacters
<p>
could be <div>
as well. Then you need a stylesheet with some classes:
.line1 {
color: red;
}
.line-1 {
color: blue;
}
.textpad {
position: relative;
width: 400px;
height: 200px;
border: 2px inset;
overflow-y: scroll;
}
Now you have red and blue lines in the #DisplayBox
. Just style the div
as you want.
(BTW this was my first VBScript ever...)
来源:https://stackoverflow.com/questions/14276032/hta-html-application-vbscript-reading-textfile-line-and-colouring-that-line-on