Author: Jonas Bilinkevicius
I need to be able to draw the text in a TForm's caption area manually, without
using WM_SETTEXT (setting the TForm's Caption property, or using the API call
SetWindowText, both use this method so they are unsuitable). I need functionality
similar to DrawText where the text is drawn directly rather than sent to a message
handler. Can anyone help?
Answer:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
7
8 type
9 TForm1 = class(TForm)
10 procedure WMPaint(var message: TWMPaint); message WM_PAINT;
11 procedure FormShow(Sender: TObject);
12 private
13 { Private declarations }
14 public
15 { Public declarations }
16 end;
17
18 var
19 Form1: TForm1;
20
21 implementation
22
23 {$R *.DFM}
24
25 procedure WriteTexttoDC(WinHandle: HWND; Text: string; X, Y: Integer);
26 var
27 DC: HDC;
28 begin
29 DC := GetWindowDC(WinHandle);
30 ExtTextOut(DC, 1, 1, ETO_CLIPPED, nil, PChar(Text), Length(Text), nil);
31 ReleaseDC(WinHandle, DC);
32 end;
33
34 procedure TForm1.WMPaint(var message: TWMPaint);
35 begin
36 WriteTexttoDC(Handle, 'Is it OK?', 5, 5);
37 end;
38
39 procedure TForm1.FormShow(Sender: TObject);
40 begin
41 WriteTexttoDC(Handle, 'Is it OK?', 5, 5);
42 end;
43
44 end.
|