zoukankan      html  css  js  c++  java
  • Delphi中透明Edit的实现

    -----------D7没有问题,XE有问题

    这个TranComp是别人写的,拿来用,

    测试(D7下)的时候正常,真正到了项目(XE)中就不行了,

    ---------------XE中---

    不行的原因不是这个TranComp没有用,有用!

    没有效果的原因是在窗体Show的开始还是透明的,快Show完了这个透明又被覆盖了,郁闷!

    ---------------XE中---

    最好放一张图片作为背景,否则看不出效果又说这个没有用

    ----------------------2021-05-19在XE下勉强可以用,但是做了一点修改------开始

    procedure TTransEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);
    begin
    //inherited;//这里注释掉
    if FTransparent then
    SetBkMode(Message.ChildDC,1);
    end;

    procedure TTransEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);
    begin
    //inherited;//这里注释掉
    if FTransparent then
    SetBkMode(Message.ChildDC, 1);
    end;

    就是这两处改了就勉强能用,不管了,借着今天有时间改了下

    -----------------------2021-05-19在XE下勉强可以用,但是做了一点修改------结束

    膜拜大神的TranCom

    -----------------

    --------------TranComp开始--

    {*******************************************************
    项目:
    模块:
    描述: 本单元为透明的Edit控件,用来将集装箱数据居中显示

    版本: 1.0
    日期: 2006-3-3
    作者: killer-lijun
    更新:
    todo:
    *******************************************************}
    unit TranComp;

    interface

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

    type
    TCtrl = class(TWinControl);

    TTransEdit = class(TEdit)
    private
    FAlignText: TAlignment;
    FTransparent: Boolean;
    FPainting: Boolean;
    procedure SetAlignText(Value: TAlignment);
    procedure SetTransparent(Value: Boolean);
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;
    procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
    procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
    procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
    procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
    procedure WMSize(var Message: TWMSize); message WM_SIZE;
    procedure WMMove(var Message: TWMMove); message WM_MOVE;
    protected
    procedure RepaintWindow;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure Change; override;
    procedure SetParent(AParent: TWinControl); override;
    public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    published
    property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;
    property Transparent: Boolean read FTransparent write SetTransparent default false;
    end;

    //procedure Register;

    implementation

    const
    BorderRec: array[TBorderStyle] of Integer = (1, -1);

    {procedure Register;
    begin
    RegisterComponents('OurComponent', [TTransEdit]);
    end; }

    function GetScreenClient(Control: TControl): TPoint;
    var
    p: TPoint;
    begin
    p := Control.ClientOrigin;
    ScreenToClient(Control.Parent.Handle, p);
    Result := p;
    end;

    constructor TTransEdit.Create(AOwner: TComponent);
    begin
    inherited Create(AOwner);
    FAlignText := taLeftJustify;
    FTransparent := false;
    FPainting := false;
    end;

    destructor TTransEdit.Destroy;
    begin
    inherited Destroy;
    end;

    procedure TTransEdit.SetAlignText(Value: TAlignment);
    begin
    if FAlignText <> Value then
    begin
    FAlignText := Value;
    RecreateWnd;
    Invalidate;
    end;
    end;

    procedure TTransEdit.SetTransparent(Value: Boolean);
    begin
    if FTransparent <> Value then
    begin
    FTransparent := Value;
    Invalidate;
    end;
    end;

    procedure TTransEdit.WMEraseBkGnd(var Message: TWMEraseBkGnd);
    var
    DC: hDC;
    i: integer;
    p: TPoint;
    begin
    if FTransparent then
    begin
    if Assigned(Parent) then
    begin
    DC := Message.DC;
    i := SaveDC(DC);
    p := GetScreenClient(self);
    p.x := -p.x;
    p.y := -p.y;
    MoveWindowOrg(DC, p.x, p.y);
    SendMessage(Parent.Handle, $0014, DC, 0);
    TCtrl(Parent).PaintControls(DC, nil);
    RestoreDC(DC, i);
    end;
    end
    else
    inherited;
    end;

    procedure TTransEdit.WMPaint(var Message: TWMPaint);
    begin
    inherited;
    if FTransparent then
    if not FPainting then
    RepaintWindow;
    end;

    procedure TTransEdit.WMNCPaint(var Message: TMessage);
    begin
    inherited;
    end;

    procedure TTransEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);
    begin
    inherited;
    if FTransparent then
    SetBkMode(Message.ChildDC, 1);
    end;

    procedure TTransEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);
    begin
    inherited;
    if FTransparent then
    SetBkMode(Message.ChildDC, 1);
    end;

    procedure TTransEdit.CMParentColorChanged(var Message: TMessage);
    begin
    inherited;
    if FTransparent then
    Invalidate;
    end;

    procedure TTransEdit.WMSize(var Message: TWMSize);
    begin
    inherited;
    Invalidate;
    end;

    procedure TTransEdit.WMMove(var Message: TWMMove);
    begin
    inherited;
    Invalidate;
    end;

    procedure TTransEdit.RepaintWindow;
    var
    DC,rDC: hDC;
    TmpBitmap, Bitmap: hBitmap;
    begin
    if FTransparent then
    begin
    FPainting := true;
    HideCaret(Handle);
    rDC := GetDC(Handle);
    DC := CreateCompatibleDC(rDC);
    TmpBitmap := CreateCompatibleBitmap(rDC, Succ(ClientWidth), Succ(ClientHeight));
    Bitmap := SelectObject(DC, TmpBitmap);
    PaintTo(DC, 0, 0);
    BitBlt(rDC, BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, 1, 1,
    SRCCOPY);
    SelectObject(DC, Bitmap);
    DeleteDC(DC);


    ReleaseDC(Handle, rDC);
    DeleteObject(TmpBitmap);
    ShowCaret(Handle);
    FPainting := false;
    end;
    end;

    procedure TTransEdit.CreateParams(var Params: TCreateParams);
    const
    Alignments: array[TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
    begin
    inherited CreateParams(Params);
    Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
    end;

    procedure TTransEdit.Change;
    begin
    RepaintWindow;
    inherited Change;
    end;

    procedure TTransEdit.SetParent(AParent: TWinControl);
    begin
    inherited SetParent(AParent);
    end;

    end.

    -------------TranComp结束---------

    --------测试单元开始-------

    unit Unit1;

    interface

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

    type
    TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation
    uses TranComp;

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    var
    ss:TTransEdit ;
    begin
    ss:=TTransEdit.create(self);
    ss.Parent:=Self;
    ss.Width:=200;
    ss.height:=20;
    ss.Left:=50;
    ss.Top:=80;
    ss.Text :='999999999999';
    ss.Transparent:=True;//这个才是关键
    end;

    end.

    --------测试单元结束-----------

  • 相关阅读:
    __cdecl, __stdcall, __fastcall,__pascal调用区别
    Windows Hook原理与实现
    C语言四大存储区域总结
    MFC DestroyWindow、OnDestroy、OnClose 程序关闭相关
    VC++动态链接库DLL编程深入浅出"
    windows 安全模型简介
    获取当前焦点窗口进程名
    获取IE URL
    DLL编写中extern “C”和__stdcall的作用
    Django2支持跨域方法
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14685225.html
Copyright © 2011-2022 走看看