一个使TEdit透明的例子

该博客主要围绕TBKTransparentEdit类展开,涉及该类的接口定义、构造函数、窗口创建、消息处理等方法的实现。通过对不同消息的处理,实现了透明编辑框的功能,如处理背景擦除、移动等消息,还包含注册组件的过程。

unit BKTransparentEdit;

interface

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

const
  TWM_BKInvalidate=WM_USER+1;

type
  TBKTransparentEdit = class(TEdit)
  private
    { Private declarations }
    procedure BKInvalidate(var Message:TMessage); message
              TWM_BKInvalidate;
    procedure CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT); message
              CN_CTLCOLOREDIT;
    procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
    procedure WMMove(var Message: TMessage); message WM_MOVE;

  protected
    { Protected declarations }
    FTransparent: boolean;
    procedure CreateWnd; override;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure DoExit; override;
    procedure DoEnter; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    procedure Invalidate; override;

  published
    { Published declarations }
  end;

procedure Register;

implementation

constructor TBKTransparentEdit.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  FTransparent:=true;
end;

procedure TBKTransparentEdit.CreateWnd;
begin
  inherited CreateWnd;
  if FTransparent then  begin
    SetWindowLong(Parent.Handle, GWL_STYLE,GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
  end;
end;

procedure TBKTransparentEdit.BKInvalidate(var Message:TMessage);
var
  r:TRect;
begin
  if (Parent<>nil) and FTransparent then  begin
    r:=ClientRect;
    r.TopLeft:=Parent.ScreenToClient(ClientToScreen(r.TopLeft));
    r.BottomRight:=Parent.ScreenToClient(ClientToScreen(r.BottomRight));
    RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE);
  end;
end;

procedure TBKTransparentEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
begin
  if FTransparent then
  with Message do
  begin
    SetBkMode(ChildDC,Windows.TRANSPARENT);
    Result:=GetStockObject(HOLLOW_BRUSH)
  end
  else
    inherited;
end;

procedure TBKTransparentEdit.WMEraseBkgnd(var Message:TWMERASEBKGND);
begin
  if FTransparent and not (csDesigning in ComponentState) then
    PostMessage(Handle,TWM_BKInvalidate,0,0)
  else
    inherited;
end;

procedure TBKTransparentEdit.WMMove(var message:TMessage);
begin
  inherited;
  if FTransparent then
    SendMessage(Handle,TWM_BKInvalidate,0,0)
  else
    Invalidate;
end;

procedure TBKTransparentEdit.CreateParams(var Params:TCreateParams);
begin
inherited CreateParams(Params);
  if (CsDesigning in ComponentState) then exit;
  with Params do
  begin
    ExStyle:=ExStyle or WS_EX_TRANSPARENT;
  end;
end;

procedure TBKTransparentEdit.DoExit;
begin
inherited;
   FTransparent:=true;
   SetCursor(0);
   RecreateWnd;
end;

procedure TBKTransparentEdit.DoEnter;
var
  exstyle,stdstyle:longint;
begin
  inherited;
  FTransparent:=false;
  StdStyle:= Windows.GetWindowLong(handle, GWL_EXSTYLE);
  exStyle:= StdStyle and not WS_EX_TRANSPARENT;
  Windows.SetWindowLong(handle, GWL_EXSTYLE, exStyle);
  invalidate;
end;

procedure TBKTransparentEdit.Invalidate;
begin
  if FTransparent then
    SendMessage(Handle,TWM_BKInvalidate,0,0)
  else
    inherited;
end;

procedure Register;
begin
  RegisterComponents('BKTransparentComps', [TBKTransparentEdit]);
end;

end.

### 回答1: 好的,这是一个使用 Delphi 编写的基本程序的例子: ``` program HelloWorld; uses SysUtils; begin WriteLn('Hello, World!'); end. ``` 这是一个简单的控制台应用程序,它将在屏幕上显示 "Hello, World!"。 程序开头使用 `program` 关键字声明了程序的名称。接下来是 `uses` 关键字,后面跟着所需要使用的单元列表。在这个例子中,只使用了 `SysUtils` 单元,它包含了一些有用的工具函数。 `begin` 和 `end` 关键字用于标记程序的主体部分,所有的程序代码都应该写在这之间。在这个例子中,只有一行代码:`WriteLn('Hello, World!')`。`WriteLn` 函数会在屏幕上输出一行文本,并在末尾换行。 最后,使用 `end.` 来结束程序。 希望这对你有帮助! ### 回答2: 下面是一个使用Delphi编写的简单基础例子: 首先,创建一个新的Delphi工程。然后,在主窗体上放置一个按钮和一个标签控件。 通过双击按钮控件,在OnClick事件处理程序中添加以下代码: ```pascal procedure TForm1.Button1Click(Sender: TObject); var name: string; begin name := 'Delphi'; ShowMessage('欢迎使用' + name + '!'); // 显示消息框 Label1.Caption := '你好,' + name + '!'; // 更新标签控件的显示文本 end; ``` 保存并编译运行程序,单击按钮时,将弹出一个显示欢迎消息的消息框,并在标签控件上显示“你好,Delphi!”的文本。 这个例子展示了Delphi中的一些基础知识。使用变量name来存储字符串值,ShowMessage函数显示消息框,Label1.Caption属性用于设置标签控件的显示文本。 通过这个例子,你可以了解如何在Delphi中处理按钮的点击事件,并对控件进行操作。你还可以尝试添加其他控件和事件处理程序,以进一步扩展这个例子。 ### 回答3: 好的,下面是一个基于Delphi的简单示例代码: ```delphi unit MainForm; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TFormMain = class(TForm) lblQuestion: TLabel; edtAnswer: TEdit; btnCheck: TButton; lblResult: TLabel; procedure btnCheckClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var FormMain: TFormMain; implementation {$R *.dfm} procedure TFormMain.btnCheckClick(Sender: TObject); var answer: Integer; begin // 读取用户输入的答案 answer := StrToIntDef(edtAnswer.Text, 0); // 判断答案是否正确 if answer = 42 then lblResult.Caption := '答案正确!' else lblResult.Caption := '答案错误,请重新尝试。'; // 清空输入框 edtAnswer.Text := ''; end; end. ``` 上面的代码是一个简单的Delphi应用程序,包括一个窗体(Form)和三个控件:一个标签(Label),一个编辑框(Edit)和一个按钮(Button)。 窗体中的`lblQuestion`是一个标签,用于显示一个问题; `edtAnswer`是一个编辑框,用户可以在这里输入答案; `btnCheck`是一个按钮,用户点击它以检查答案; `lblResult`是一个标签,用于显示检查结果。 点击`btnCheck`按钮时,程序将获取用户输入的答案,并与预设的答案比较。 如果答案正确,程序将在`lblResult`标签中显示"答案正确!"; 否则,程序将在`lblResult`标签中显示"答案错误,请重新尝试。"。 之后,输入框将被清空,用户可以尝试输入新的答案。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值