目前对 $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

posted on 2008-10-31 12:04  万一  阅读(2628)  评论(0编辑  收藏  举报