|
DELPHI中的消息处理机制(4) DragMouseMsg(TWMMouse(Message))
else
... { 处 理 其 他 鼠 标 消 息}
end;
Dispatch(Message);
{ 否 则 正 常 发 送 消 息}
end;
下 例 为 一 简 单 的 自 定 义 构 件 例 子 :
Tmyedit 类 是 从Tedit 类 派 生 出 的 一 个 新 类, 它 的 特 点 是 在 运 行 中 不 能 获 得 焦 点, 不 能 由 键 盘 输 入( 有 点 类 似Tlabel 构 件). 我 们 可 在 其wndproc 方 法 中 过 滤 出WM_setfocus,WM_mousemove 消 息 并 进 行 处 理 来 达 到 上 述 要 求, 源 程 序 如 下:
unit myedit;
interface
uses
Windows, Messages, SysUtils, Classes, GraphiCS,
Controls, Forms, Dialogs,
StdCtrls;
type
Tmyedit = class(TEdit)
private
{ Private declarations }
protected
{ Protected declarations }
{ other fields and methods}
procedure wndproc(var message:Tmessage);override;
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [Tmyedit]);
end;
procedure Tmyedit.wndproc(var message:tmessage);
begin
if message.msg=wm_mousemove then
begin
cursor:=crarrow;
{ 设 置 光 标 为crarrow, 而 不 是 缺 省 的crBeam 光 标}
exit;
end;
if message.msg=wm_SetFocus then exit;
{屏蔽掉WM_setfocus消息,不让Tmyedit控件获得输入焦点}
inherited wndproc(message);
{其他消息交父辈wndproc处理}
end;
end.
您 可 以 将Tmyedit 加 到Component Palette 中 检 验 其 性 能。 由 以 上 介 绍 可 以 看 出, 只 有 清 楚 了Delphi VCL 中 的 消 息 处 理 机 制, 掌 握 好 处 理 各 种 消 息 的 方 法 和 时 机( 必 要 时 要 借 助 各 种 工 具, 如winsight32,spy 等), 并 结 合OOP 语 言 的 特 点, 我 们 才 可 能 编 出 高 质 量 的 构 件。 这 当 然 要 靠 读 者 在 实 践 中 不 断 摸 索, 积 累 经 验。
南京理工大学自控系研96 马勇
|