Skip to main content
gamedev.net
🔒 Locked

using WriteConsoleOutputCharacter with a GUI

Started by minusInfinity 2 replies 1.9k views
Original Post
minusInfinity
minusInfinity
Hey everyone, I have a quick question about the WriteConsoleOutputCharacter function. How could I use it to display text in a window? In particular I am having trouble understanding the screen buffer parameter. I have seen its usage in a DOS prog thanks to GameTutorials.com, but would really like to use it in a windows program. I know the constant screen refresh/redrawing makes displaying text in a window a very different thing than in a DOS console, so any ideas would be greatly appreciated. Thanks a lot, -Mike
Evil Steve
Evil Steve
WriteConsoleOutputCharacter can only be used to write text to a console window. To write to a "normal" window, look into DrawText() and TextOut().

EDIT: For a "Hello World" sample, have a look at Wikipedia (Scroll down to "Windows API (in C)"), in particular:
case WM_PAINT:   hdc = BeginPaint(hwnd, &ps);   TextOut(hdc, 15, 3, "Hello, world!", 13);   EndPaint(hwnd, &ps);   break;

minusInfinity
minusInfinity
Thank you so much for the reply. DrawText definitely looks friendlier and easier to grasp.

On a somewhat related note, how could I paramertize the amount and the content of the text displayed?

For example, let's say I wanted to display 50 'hello's' displayed randomly on the screen. I know hard coding that would not be a problem in WndProc, but let's say I wanted to parameterize the number and text to display (i.e. 100 the next time the project ran for example) in my WinMain function? Is that possible considering the nature of callback functions (WndProc)?

Sorry btw..I am a noob when it comes to the Windows API.

Thanks again,

-Mike
Evil Steve
Evil Steve
There's a 3 ways you could do it that I can think of. In WM_PAINT (where you do the drawing), you need to be able to get the number of strings to draw (obviously). Assuming you get the number from the command line with something like: int nNumberOfStrings = atoi(szCmdLine); (in WinMain()).
You could:
  1. Store the number as a global (Which you probably don't want to do)
  2. Get the number from inside WM_PAINT with GetCommandLine(): int nNumberOfStrings = atoi(GetCommandLine());
  3. Save the number in the user data section of the window:
    // In winmain, after you create the window:SetWindowLong(hWnd, GWL_USERDATA, (LONG)nNumberOfStrings);// In WM_PAINT:int nNumberOfStrings = (int)GetWindowLong(hWnd,GWL_USERDATA);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.