c++dosturbo-c++borland-c++borland-c

Borland c++ console functions


I'm studing now and I got this homework / tasks to do:

1) If you press the CTRL + L key, all numeric symbols should change the color.

2) If you press the CTRL + S key, you will get the length of the word, left from the cursor.

I found this function int bioskey(int cmd); So now I can check if the key is pressed, but how to change the color only of numeric symbols, or read words from console to get their length ?


Solution

  • Some of us still remember the MS-DOS (let it rest in peace or pieces...)

    if you are really in MS-DOS then you can not expect that the content of the console would be changed in colors for only specific areas. You need to do that your self. The problem is we do not know anything about your project background so we do not know what and how yours stuff is represented,rendered/outputed/inputed etc...

    I assume EGA/VGA BIOS text mode is used so you can exploit direct access to the VRAM. So you need to set pointer to the address B800:0000 and handle it as array where each character on screen has 2 BYTEs. one is color attribute and the other is ASCII code (not sure in which order anymore)...

    So for already rendered stuff you just:

    1. loop through whole screen

      usually 80x25x2 Bytes

    2. test each ASCII for alpha numeric value

      so ASCII code >= '0' and code<='9' for numbers or add all the stuff you are considering as alphanumeric like code>' ' and code<='9'.

    3. change colors for selected characters

      just by changing the attribute byte.

    When you put it together for numbers it will look like this:

    char far *scr=(char far*)0x0B0000000;
    int x,y,a;
    for (a=0,y=0;y<25;y++)
     for (x=0;x<80;x++,a+=2)
      if ((scr[a+0]>='0')&&((scr[a+0]<='9'))
      {
      scr[a+1]=7; //attribute with the different color here
      }
    

    if it does not work than try swap scr[a+0] and scr[a+1]. If an exception occur then you are not in MS-DOS and you do not have access to VRAM. In that case use DOS-BOX or driver that allows access to memory like dllportio ... For more info see some more or less related QA's:

    If you got problem with the CTRL+Key detection not sure if in-build function in TC++ allows CTRL (was too long ago) then you can exploit BIOS or even hook up the keyboard ISR. See the second link where ISR for keyboard handler is there present... You can port it to C++ or google there must be a lot of examples out there especially TP7.0 (which is pascal but easily portable to TC++)