|

Win32/ FPC - Chapter 7 - by Delax
Now we are going to print a simple line of text in our window. I really _do_ mean simple text as there are a couple of ways to put out text into a window. For now we'll have a look at two possibilities.
The simple way is called "TextOut()". The WinHello.pp uses the luxury edition called "DrawText()". TextOut() simply draws one line of text. With DrawText() you have to set a text area in which the text is then drawn.
Anyway. If you want to work with a window in some graphical way, you will need a so called "Device Context" or "DC" for short. It is sometimes called "Graphics Device Context" as well. This DC has something to do with the "GDI". GDI is a TLA for "Graphics Device Interface". The GDI is the Interface to the Gfx system used in Windows. Anything you see on your screen is actually printed with this GDI system. (Maybe not if you are playing games as DirectX, OpenGL or something like that takes over then). So let's resume: to draw something you'll have to use the GDI and to use the GDI you'll need a Device Context.
A DC is defined quite similar to a Windows Handle.
dcWindow : hdc;
We need to set the Device Context for our window first. This is done with GetDC(). We also have to give the name of our DC as a parameter.
dcWindow := GetDC(hWindow);
Right. Anything we do with our Device Context is now happening in our window. Let's define the background color and text color.
SetBKMode(dcWindow, TRANSPARENT);
SetTextColor(dcWindow, RGB(0,0,0));
This should be self explaining. SetBKMode() sets the background color. By setting it to TRANSPARENT we tell Windows we don't want any background color for our text.
SetTextColor() is setting the color of our text. Lucky for us we can define the color by using the RGB system. Note that we have to give the handle to our DC every time.
Let's take a look at the TextOut call now.
TextOut(DeviceContext, X, Y, OutputString, Lenght of the String);
By the way: Windows does NOT use normal Pascal strings, but PChars. As Windows is more C/ Cpp oriented you have to deal with this little fact. PChars are pointers to a number of chars which are terminated. We will focus on them a little later.
The X/ Y coordinates are integer values and represent the position of the first character in pixels. The lenght of the string has to be set as well. To simplify matters you can use Lenght(String).
TextOut(dcWindow, 15, 5, 'Simple Text Output!', Length('Simple Text Output!'));
All right. Simple, eh? When we are done, we need to release our Device Context again.
ReleaseDC(hWindow, dcWindow);
That's it. Here now the code in whole.
dcWindow := GetDC(hWindow);
SetBKMode(dcWindow, TRANSPARENT);
SetTextColor(dcWindow, RGB(0,0,0));
TextOut(dcWindow, 15, 5, 'Simple Text Output!', Length('Simple Text Output!'));
ReleaseDC(hWindow, dcWindow);
Now there is a problem: where do we put this code? After the creation of our window? Then it would be shown once. As soon as another window would be in front of ours, the text would not be redrawn anymore - meaning it would not be visible anymore. If we'd put it in our main loop it would be drawn all the time and the application would hog all CPU power just to draw the text.
Now think of the following: what if we'd put it in the WinProc? We would check for messages if our window needs to be redrawn (WM_PAINT). Then, and only then, we would draw out text. This way, we put out our text only if it's necessary. The complete check and output looks like this:
WM_PAINT : begin
dcWindow := GetDC(hWindow);
SetBKMode(dcWindow, TRANSPARENT);
SetTextColor(dcWindow, RGB(0,0,0));
TextOut(dcWindow, 15, 5, 'Simple Text Output!', Length('Simple Text Output!'));
ReleaseDC(hWindow, dcWindow);
end;
And that's really all to printing simple texts in Windows. Get the Source Code here and have a look. But as you play around with TextOut() you may encounter some problems. More on that, and how DrawText() is solving these problems, in the next chapter.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|