|

Win32/ FPC - Chapter 8 - by Delax
To get one thing straight: DrawText is very Windows. To know how it works you have to have a little bit of Windows background. But if you really understood it, then you have understood the principle of Windows.
Last chapter we used WM_PAINT for the fist time. It is a Windows message that is send every time a Window has to be redrawn. A standard WM_PAINT handling looks something like this:
WM_PAINT : begin
dcWindow := BeginPaint(hWindow, paintstructure);
...
EndPaint(hWindow, paintstructure);
end;
A Window has to be redrawn if it is moved, resized, minimized and so on. Instead of using GetDC to get a Device Context, we'll get a Device Context with BeginPaint this time. To do so, we have to give the handle of our Window and a paint structure. Let's just say it is more correct to do it this way. It has something to do with validating the parts that are redrawn. If a part of the window need redrawing, it gets invalidated. After we have drawn the window again, we have to validate these parts again so Windows knows we repainted them. Something like that ;) More on that when Windows messages are due.
Then we need a paint structure. A paint structure is a structure (record) that contains information for an application. This information can be used to paint an area of a window. We define a paint structure like this.
ps : paintstruct;
A paintstruct is a definition how the output is drawn in the given Window. In this case, we want to print simple text and nothing else. As I mentioned before, DrawText needs some kind of rectangle to draw in.
r : rect;
Here we got a rect(angle). Try to imagine that his rect is a small window inside our window. Everything we draw now is inside this little rect-window. Now we set the rect inside our Window. To do this we could either give Windows a pixel value (something like: the rect goes from 10/10 to 100/100) or let Windows use the whole Window. We do this by using the GetClientRect() procedure.
GetClientRect(hWindow, @r);
Let's summarize. Everytime a Window needs to be redrawn for whatever reason, we get a WM_PAINT message. So if you want to print out text, all you have to do is to put the print command in the event handler of WM_PAINT. A typical WM_PAINT has to start with BeginPaint() where you have to give the handle of the window we want to draw in and a paint structure. A paint structure contains the definition of the way out output is redrawn. Then we have to tell Windows where the output should be printed inside the window. And everything we want to put out is drawn inside this rect. Still with me?
Now that we have our rect and paintstruct, we can start putting out the actual text. DrawText() has a few parameters. First (of course) the Device Context. Then the text we want to print and the lenght of the text. By setting it to -1, the characters will be counted by Windows. Followed by a pointer
to the rect to draw in and some text-drawing flags. Take a look at the SDK for a list of those flags.
Our final WM_PAINT looks like this.
WM_PAINT : begin
dcWindow := BeginPaint(hWindow ,@ps);
GetClientRect(hWindow, @r);
SetBKMode(dcWindow, TRANSPARENT);
SetTextColor(dcWindow, RGB(0,0,0));
DrawText(dcWindow, 'This is a DrawText( ) example.', -1, @r,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(hWindow, ps);
end;
Get the Source Code here.
Phew. Now you know two different ways to print out Text in Windows: TextOut() and DrawText(). TextOut() is much easier, so where is the advantage in using DrawText() ? Let's say you open a new Window and put out some TextOut(). Windows keeps sending WM_PAINT messages all the time, no matter if you have redrawn your text. This has to do with validating the redrawn windows. If you want the WM_PAINT message-flood to stop you could put an empty BeginPaint/ EndPaint call in your WM_PAINT handling. But this also works for TextOut(). Where is the real money?
The real thing is, that with DrawText() you can use multiple lines, alignments (left, right, centered text) and much, much more stuff to format your text. With TextOut() you would have to do all this by hand. And this takes a LOT of work. So you might now get an idea why the rect is so important. Windows has to have some idea where the end of it's lines are to format a text.
That's it for now. Sorry to throw something like this at you this early, but this is how Windows works. Many handles, numerous pointers and a dozens of structures and flags for everything. But don't worry, you'll get a hang of it soon enough.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|