把文字输出到屏幕

最主要是取得了桌面的DC,并且设置为背景色透明:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//把文字输出到屏幕
procedure TForm1.Button1Click(Sender: TObject);
var
  cvs: TCanvas;
  Rect: TRect;
  Str: string;
begin
  cvs := TCanvas.Create; // 现场创建
  cvs.Handle := GetDC(0);
  SetBkMode(cvs.Handle, TRANSPARENT); // 设置指定DC的背景混合模式,应用于文字、画刷和画笔风格(当它们不是solid风格的时候),有OPAQUE和TRANSPARENT两种模式
  //cvs.Font.Name := '宋体';
  cvs.Font.Style := [fsBold, fsItalic];
  cvs.Font.Size := 48;
  Randomize;
  cvs.Font.Color := Random($FFFFFF);
  Rect := Screen.DesktopRect; // 取得屏幕的区域
  Str := '万一的 Delphi 博客';
  cvs.TextRect(Rect, Str, [tfSingleLine, tfCenter, tfVerticalCenter]); // 在指定区域内输出文字,单行,居中,竖向居中。注意D7不支持此参数
  cvs.Free;
end;

//刷新显示
procedure TForm1.Button2Click(Sender: TObject);
begin
  InvalidateRect(0, nil, False); // API,消除上面的文字
end;

end.

InvalidateRect API函数的说明:
hWnd:要更新的客户区所在的窗体的句柄。如果为NULL,则系统将在函数返回前重新绘制所有的窗口, 然后发送 WM_ERASEBKGND 和 WM_PAINT 给窗口过程处理函数。
lpRect:无效区域的矩形代表,它是一个结构体指针,存放着矩形的大小。如果为NULL,全部的窗口客户区域将被增加到更新区域中。
bErase:指出无效矩形被标记为有效后,是否重画该区域,重画时用预先定义好的画刷。当指定TRUE时需要重画。
返回值:函数成功则返回非零值,否则返回零值。
说明:被标记为无效矩形的区域直到WM_PAINT消息被处理完之后才会消失,或者使用ValidateRect(),ValidateRgn()函数来使之有效。当应用程序的消息队列中为空时,并且窗体要更新的区域非空时,系统会发送一个WM_PAINT消息到窗体。

参考:
http://www.cnblogs.com/del/archive/2008/05/22/1204672.html

原文地址:https://www.cnblogs.com/findumars/p/4104227.html