目前對 $0118 號消息的認識:
1、微軟和 Delphi 都沒有給改消息定義一個常量, 假如定義的話用 WM_SYSTIMER 比較合適;
2、此消息只在文本輸入類控件(譬如: TMemo、TRichEdit、TEdit)獲得焦點時才會發出, 用於控制輸入光標;
3、此消息每秒一次, 和輸入光標同步;
4、此消息一旦達到目的立即就返回了, 所以用消息方法和 WndProc、Dispatch 甚至 DefaultHandler 都不能響應;
5、因為 Application.ProcessMessages 拿到消息後首先是給 Application.OnMessage, 所以 OnMessage 能收到.
本例效果圖:
代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, AppEvnts;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ApplicationEvents1: TApplicationEvents;
procedure Button1Click(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if Msg.message = $0118 then
begin
Text := TimeToStr(Now);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
W = 150;
H = 50;
L = 10;
var
T: Integer;
begin
T := L;
with TMemo.Create(Self) do
begin
Parent := Self;
Text := ClassName;
Width := W;
Height := H;
Left := L;
Top := T;
Inc(T, H+L);
end;
with TRichEdit.Create(Self) do
begin
Parent := Self;
Text := ClassName;
Width := W;
Height := H;
Left := L;
Top := T;
Inc(T, H+L);
end;
with TEdit.Create(Self) do
begin
Parent := Self;
Text := ClassName;
Width := W;
//Height := H;
Left := L;
Top := T;
//Inc(T, H+L);
end;
Text := '當前文本輸入控件沒有焦點';
Button1.Enabled := False;
end;
end.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 165
ClIEntWidth = 320
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 176
Top = 32
Width = 136
Height = 25
Caption = #24314#31435#20960#20010#25991#26412#36755#20837#25511#20214
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 176
Top = 83
Width = 136
Height = 25
Caption = #25226#28966#28857#36716#31227#21040#27492
TabOrder = 1
end
object ApplicationEvents1: TApplicationEvents
OnMessage = ApplicationEvents1Message
Left = 136
Top = 64
end
end