HTA(HTML应用程序)VBScript读取文本文件行并仅对该行着色 [英] HTA (Html Application) VBScript Reading Textfile line and colouring that line only
问题描述
我一直在研究Chat HTA文件,以便在工作中玩...
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...
或
如果我在不使用文本文件的情况下无法在窗口中显示聊天",那将是一个很好的\更好的操作(节省清理删除不需要的文本文件的可能性.)
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)
欢呼 密密麻麻.
' HTA聊天
' 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>`
推荐答案
以下是您的任务准则.
首先, textarea
不能具有HTML,因此不能用作富文本的填充.为此,您需要一个<pre>
或一个<div>
.例如:
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>
也可以是<div>
.然后,您需要带有一些类的样式表:
<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;
}
现在#DisplayBox
中有红色和蓝色线条.只需根据需要设置div
的样式.
Now you have red and blue lines in the #DisplayBox
. Just style the div
as you want.
(顺便说一句,这是我有史以来第一个VBScript ...)
这篇关于HTA(HTML应用程序)VBScript读取文本文件行并仅对该行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!