- Having got my little Staff programming course to a reasonable level I thought you would like to play too. This is homework set last week and is all about graphics windows and text handling.
As part of a larger project we (the course) are building an extension to the QUIZ program that basically inverts the learning process. That is you display the answers and the user has to type in the question. Before getting bogged down in the validity of that from a pedagogical perspective, it does work - I have a version in use. One routine we need to is to show the correct words as they are typed in :
The Task :
Build a subroutine that accepts a user string, a keyword string, a font size, a window pointer and a window width - and
Formats text in that graphics window with the keywords in BLACK and the other words in RED. Wrap at whole words and assume adequate window depth for the input string to be fully displayed.
Use no global variables. Window width can be calculated outside of the routine.
Create a small calling loop that assembles the string from Inkey$ and displays the growing string each time a character is typed. Cater for BackSpace (8) and reduce the string accordingly.
Since I have to have a master version to mark with - my result is in the files section under HOMEWORK. I will not display it here as the formatting will suffer and it might encourage you to try it for yourselves first. I will be very interested in better answers to the problem, since I have no doubt that there are.
Alyce's Codachrome is one, but that would rather deplete the learning points I am trying to make. I may show them the DLL when this is done, although for what we need now, this routine is probably enough....
Here is the calling routine and window setup to get you on the same page :
' String manipulation in graphics window coloring key words
WindowWidth = 571 : WindowHeight = 423
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
graphicbox #main.input, 15, 60, 510, 95
Open "Word Coloring" for Window as #main
#main "trapclose [quit]"
#main.input "down; fill Yellow; flush"
#main.input "setfocus; when characterInput [fetch]"
#main "font ms_sans_serif 20"
KeyWord$ = "THIS THEN THE WORD VALID INVALID"
' Build a structure to get window info
struct Rect, x1 As Long, y1 As Long,_
x2 As Long, y2 As Long
call GetWindowRect hwnd(#main.input)
winwide = Rect.x2.struct-Rect.x1.struct ' This window's width
' winheight = Rect.y2.struct-Rect.y1.struct ' Window height - not needed here
' winx = Rect.x1.struct ' Window x pos - not needed here
' winy = Rect.y1.struct ' Window y pos - not needed here
k$ = right$(Inkey$,1)
if asc(mid$(Inkey$,1)) = 8 then
i$ = left$(i$,len(i$)-2)
i$ = i$ + upper$(k$)
call ShowColorWords i$, KeyWord$,20,"#main.input",winwide
close #main : END
Sub GetWindowRect hW
CallDLL #user32, "GetWindowRect",hW As Long,_
Rect As struct, result As Long
PS There is a deliberate extra feature (bug) in my routine that allows partial words to be marked as correct (BLACK). Can you fix that ?
Supplementary : For more advanced users. Create another routine with the same inputs that works in characters (ShowColorChrs i$, KeyWord$,20,"#main.input",winwide) rather than words. For example, colors vowels RED and Consonants BLACK.