HTA (Html Application) VBScript Reading Textfile line and colouring that line only

假装没事ソ 提交于 2019-12-23 01:54:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!