Can't figure out the pointer var in WriteConsoleOutputAttribute...

I'm using the WriteConsoleOutputCharacter and WriteConsoleOutputAttribute function to write text to a buffer in a console application. I've figured out WriteConsoleOutputCharacter and most of WriteConsoleOutputAttribute, but I can't seem to wrap my mind around the "CONST WORD *lpAttribute" var:

[CODE]
BOOL WriteConsoleOutputAttribute(

HANDLE hConsoleOutput, // handle to a console screen buffer
CONST WORD *lpAttribute, // pointer to buffer to write attributes from
DWORD nLength, // number of character cells to write to
COORD wWriteCoord, // coordinates of first cell to write to
LPDWORD lpNumberOfAttrsWritten // pointer to number of cells written to
);[/CODE]

I understand the basic concept of pointers, but I just can't figure out how to declare this var (how to select the color of the character to write to the buffer). I'm only writing one character at a time, if that has any influence on how to declare the var. I'd like to declare the attribute var by using numbers (i.e. 15 for white on black background), and not by the RED_BACKGROUND | BLUE_WHATEVER format, as the data I'm reading and writing to the buffer has the color defined by numbers. Any advice?

Comments

  • : I'm using the WriteConsoleOutputCharacter and WriteConsoleOutputAttribute function to write text to a buffer in a console application. I've figured out WriteConsoleOutputCharacter and most of WriteConsoleOutputAttribute, but I can't seem to wrap my mind around the "CONST WORD *lpAttribute" var:
    :
    : [CODE]
    : BOOL WriteConsoleOutputAttribute(
    :
    : HANDLE hConsoleOutput, // handle to a console screen buffer
    : CONST WORD *lpAttribute, // pointer to buffer to write attributes from
    : DWORD nLength, // number of character cells to write to
    : COORD wWriteCoord, // coordinates of first cell to write to
    : LPDWORD lpNumberOfAttrsWritten // pointer to number of cells written to
    : );[/CODE]
    :
    : I understand the basic concept of pointers, but I just can't figure out how to declare this var (how to select the color of the character to write to the buffer). I'm only writing one character at a time, if that has any influence on how to declare the var. I'd like to declare the attribute var by using numbers (i.e. 15 for white on black background), and not by the RED_BACKGROUND | BLUE_WHATEVER format, as the data I'm reading and writing to the buffer has the color defined by numbers. Any advice?
    :
    The color constants are just identifiers to numbers. You should look for a list, which specifies which identifier has which value; or write a quick program to map them.
    As for the parameter:
    [code]
    type
    PlpAttribute = ^word;

    const lpAttribute: PlpAttribute
    [/code]
    That should work, although I'm not entirely sure.
  • : The color constants are just identifiers to numbers. You should look for a list, which specifies which identifier has which value; or write a quick program to map them.
    : As for the parameter:
    : [code]
    : type
    : PlpAttribute = ^word;
    :
    : const lpAttribute: PlpAttribute
    : [/code]
    : That should work, although I'm not entirely sure.
    :

    That gave me an error. I fiddled around with it a bit and could compile it if I changed "const lpAttribute: PlpAttribute" to "const lpAttribute = PlpAttribute();". I don't know if this is correct, but it did compile without errors. However, the characters written to the buffer didn't show up when I made the buffer the active screen (it worked before without the color attributes set). If this is correct, then what do I write inside the parenthesis?
  • : : The color constants are just identifiers to numbers. You should look for a list, which specifies which identifier has which value; or write a quick program to map them.
    : : As for the parameter:
    : : [code]
    : : type
    : : PlpAttribute = ^word;
    : :
    : : const lpAttribute: PlpAttribute
    : : [/code]
    : : That should work, although I'm not entirely sure.
    : :
    :
    : That gave me an error. I fiddled around with it a bit and could compile it if I changed "const lpAttribute: PlpAttribute" to "const lpAttribute = PlpAttribute();". I don't know if this is correct, but it did compile without errors. However, the characters written to the buffer didn't show up when I made the buffer the active screen (it worked before without the color attributes set). If this is correct, then what do I write inside the parenthesis?
    :
    Obviously this was meant as a part of the parameter list:
    [code]
    OOL WriteConsoleOutputAttribute(

    HANDLE hConsoleOutput, // handle to a console screen buffer
    [red]const lpAttribute: PlpAttribute;[/red] // pointer to buffer to write attributes from
    DWORD nLength, // number of character cells to write to
    COORD wWriteCoord, // coordinates of first cell to write to
    LPDWORD lpNumberOfAttrsWritten // pointer to number of cells written to
    );
    [/code]
    Your code makes the constant into a procedure pointer, which doesn't have a real address.
  • : Obviously this was meant as a part of the parameter list:
    : [code]
    : OOL WriteConsoleOutputAttribute(
    :
    : HANDLE hConsoleOutput, // handle to a console screen buffer
    : [red]const lpAttribute: PlpAttribute;[/red] // pointer to buffer to write attributes from
    : DWORD nLength, // number of character cells to write to
    : COORD wWriteCoord, // coordinates of first cell to write to
    : LPDWORD lpNumberOfAttrsWritten // pointer to number of cells written to
    : );
    : [/code]
    : Your code makes the constant into a procedure pointer, which doesn't have a real address.
    :



    Ok, I got the code to compile by declaring:

    [CODE]
    TYPE PTextAttribute = ^TTextAttribute;
    TTextAttribute = Word;

    VAR ColorAttribute : PTextAttribute;

    WriteConsoleOutputAttribute(BufferHandle, ColorAttribute, 1, Coord, dW);[/CODE]

    However, I can't figure out how to pass a value to the ColorAttribute. I thought I could just use, for example:

    [CODE]
    ColorAttribute := FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;[/CODE]

    But this returns the error "Illegal character in input file: '|' ($7C)". I also tried "ColorAttribute := 15", but this returns the error "Incompatible types: 'Integer' and 'PTextAttribute'".

    I'm really going insane trying to do something as simple as changing the color of a character in the buffer. I just can't believe this should be so complicated... :o(

    I've searched everywhere on the net for an example of somebody actually using WriteConsoleOutputAttribute in Delphi code, so that I could see how they declare the textattributes, but I haven't been succesful. Damn, I'm longing for the good old Pascal days...
  • [b][red]This message was edited by zibadian at 2006-9-19 13:15:59[/red][/b][hr]
    : : Obviously this was meant as a part of the parameter list:
    : : [code]
    : : OOL WriteConsoleOutputAttribute(
    : :
    : : HANDLE hConsoleOutput, // handle to a console screen buffer
    : : [red]const lpAttribute: PlpAttribute;[/red] // pointer to buffer to write attributes from
    : : DWORD nLength, // number of character cells to write to
    : : COORD wWriteCoord, // coordinates of first cell to write to
    : : LPDWORD lpNumberOfAttrsWritten // pointer to number of cells written to
    : : );
    : : [/code]
    : : Your code makes the constant into a procedure pointer, which doesn't have a real address.
    : :
    :
    :
    :
    : Ok, I got the code to compile by declaring:
    :
    : [CODE]
    : TYPE PTextAttribute = ^TTextAttribute;
    : TTextAttribute = Word;
    :
    : VAR ColorAttribute : PTextAttribute;
    :
    : WriteConsoleOutputAttribute(BufferHandle, ColorAttribute, 1, Coord, dW);[/CODE]
    :
    : However, I can't figure out how to pass a value to the ColorAttribute. I thought I could just use, for example:
    :
    : [CODE]
    : ColorAttribute := FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;[/CODE]
    :
    : But this returns the error "Illegal character in input file: '|' ($7C)". I also tried "ColorAttribute := 15", but this returns the error "Incompatible types: 'Integer' and 'PTextAttribute'".
    :
    : I'm really going insane trying to do something as simple as changing the color of a character in the buffer. I just can't believe this should be so complicated... :o(
    :
    : I've searched everywhere on the net for an example of somebody actually using WriteConsoleOutputAttribute in Delphi code, so that I could see how they declare the textattributes, but I haven't been succesful. Damn, I'm longing for the good old Pascal days...
    :
    Pointers have always been in Pascal. Just like incompatible types. Here's the solution:
    [code]
    var
    Attr: word;

    begin
    Attr := 15; // or whatever
    WriteConsoleOutputAttribute(BufferHandle, @Attr, 1, Coord, dW);[
    [/code]
    The @-operator (also "old" Pascal) gives you the pointer to a variable. If you want to assign something to a pointer reference, you always need to dereference the pointer (also "old" Pascal):
    [code]
    TYPE PTextAttribute = ^TTextAttribute;
    TTextAttribute = Word;

    VAR ColorAttribute : PTextAttribute;
    begin
    Attr^ := 15;
    WriteConsoleOutputAttribute(BufferHandle, Attr, 1, Coord, dW);[
    [/code]
    The easiest solution is not to use a console window, but use a true GUI. Then you can use a TPaintBox to position your text and give it a color:
    [code]
    PaintBox1.Canvas.Font.Color := clBlue;
    PaintBox1.Canvas.TextOut(10, 10, 'Hello World');
    [/code]
    Also the problem isn't Delphi, but windows, especially the console window. That's because it still handles windows messages, which is different from DOS.


  • Ahhh, it finally works now. Thanks!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories