在界面设计中,可以拦截CMEraseBkgnd消息,促使生成事实不存在的窗体,在 Win2000 和 WinXP 中,用 SetLayeredWindowAttributes 产生半透明窗体,而用什么方法使得 Label 和 Panel 等也具有相同的效果?------------------------
方法无限制,确实可行及得!
------------------------

解决方案 »

  1.   

    在http://expert.csdn.net/Expert/topic/1403/1403169.xml?temp=.6380121中我已有解答
      

  2.   

    unit Glass;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls;type
      TGlassStyle = (
        gsBlackness, gsDstInvert, gsMergeCopy, gsMergePaint, gsNotSrcCopy,
        gsNotSrcErase, gsPatCopy, gsPatInvert, gsPatPaint, gsSrcAnd,
        gsSrcCopy, gsSrcErase, gsSrcInvert, gsSrcPaint, gsWhiteness);  TGlass = class(TCustomControl)
      private
        FColor: TColor;
        FStyle: TGlassStyle;
        FOnPaint: TNotifyEvent;    procedure SetColor(Value: TColor);
        procedure SetStyle(Value: TGlassStyle);
        procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
        procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
        procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
      protected
        Buffer: TBitmap;    procedure CreateParams(var Params: TCreateParams); override;
        procedure Paint; override;
        procedure Resize; override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        property Canvas;
      published
        property Align;
        property Anchors;
        property AutoSize;
        property BiDiMode;
        property BorderWidth;
        property Color: TColor read FColor write SetColor;
        property Ctl3D;
        property Enabled;
        property Style: TGlassStyle read FStyle write SetStyle default gsSrcAnd;
        property Visible;    property OnClick;
        property OnDblClick;
        property OnEnter;
        property OnExit;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property OnResize;
        property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Croco', [TGlass]);
    end;function GlassStyleToInt(gs: TGlassStyle): LongInt;
    begin
      case gs of
        gsBlackness  : Result := cmBlackness;
        gsDstInvert  : Result := cmDstInvert;
        gsMergeCopy  : Result := cmMergeCopy;
        gsMergePaint : Result := cmMergePaint;
        gsNotSrcCopy : Result := cmNotSrcCopy;
        gsNotSrcErase: Result := cmNotSrcErase;
        gsPatCopy    : Result := cmPatCopy;
        gsPatInvert  : Result := cmPatInvert;
        gsPatPaint   : Result := cmPatPaint;
        gsSrcAnd     : Result := cmSrcAnd;
        gsSrcCopy    : Result := cmSrcCopy;
        gsSrcErase   : Result := cmSrcErase;
        gsSrcInvert  : Result := cmSrcInvert;
        gsSrcPaint   : Result := cmSrcPaint;
        gsWhiteness  : Result := cmWhiteness;
        else           Assert(True, 'Error parameter in function GlassStyleToInt');
      end;
    end;constructor TGlass.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Buffer := TBitmap.Create;  ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
        csDoubleClicks, csReplicatable];
      Width := 100;
      Height := 100;
      FStyle := gsSrcAnd;
      ParentCtl3d := False;
      Ctl3D := False;
      ParentColor := False;
      FColor := clWhite;
    end;destructor TGlass.Destroy;
    begin
      Buffer.Free;
      inherited Destroy;
    end;procedure TGlass.Paint;
    var
      R: TRect;
      rop: LongInt;
    begin
      R := Rect(0, 0, Width, Height);
      Buffer.Width := Width;
      Buffer.Height := Height;
      Buffer.Canvas.Brush.Style := bsSolid;
      Buffer.Canvas.Brush.Color := FColor;
      Buffer.Canvas.FillRect(Rect(0, 0, Width, Height));
      rop := GlassStyleToInt(FStyle);
      StretchBlt(Buffer.Canvas.Handle, 0, 0, Width, Height,
                 Canvas.Handle, 0, 0, Width, Height, rop);
      if Ctl3D then DrawEdge(Buffer.Canvas.Handle, R, BDR_RAISEDINNER, BF_RECT);
      Buffer.Canvas.Pen.Mode := pmCopy;
      Buffer.Canvas.Pen.Style := psSolid;
      Canvas.Draw(0, 0, Buffer);
      if Assigned(FOnPaint) then FOnPaint(Self);
    end;
    procedure TGlass.SetColor(Value: TColor);
    begin
      if Value <> FColor then
      begin
        FColor := Value;
        RecreateWnd;
      end;
    end;procedure TGlass.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);  Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
    end;procedure TGlass.WMWindowPosChanging(var Message: TWMWindowPosChanging);
    begin
      Invalidate;  inherited;
    end;procedure TGlass.WMEraseBkgnd(var Message: TMessage);
    begin
      Message.Result := 0;
    end;procedure TGlass.Resize;
    begin
      Invalidate;  inherited;
    end;procedure TGlass.CMCtl3DChanged(var Message: TMessage);
    begin
      inherited;  RecreateWnd;
    end;procedure TGlass.SetStyle(Value: TGlassStyle);
    begin
      if Value <> FStyle then
      begin
        FStyle := Value;
        RecreateWnd;
      end;
    end;end.
      

  3.   

    Thanks cg1120(代码最优化-§新年祝福你,好运伴着你§) ,and thanks everybody to answer the question.The question will have many answers.    所有参加者,都会得到分值;希望大家踊跃参加,只要达到可行问题解决方案,对于所有参与者都是一次学习与积累的机会,谢谢!
      

  4.   

    学习,并希望搂主把实现form的代码也给大家看看。
      

  5.   

    只适应于 Win 2000/XP 窗口透明,SetLayeredWindowAttributes 为 2000/XP 新增 API :unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;const
      WS_EX_LAYERED = $80000;
      LWA_ALPHA = $2;type
      TForm1 = class(TForm)
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint;stdcall;external user32;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      l:Longint;
    begin
      l :=getWindowLong(Handle, GWL_EXSTYLE);
      l := l or WS_EX_LAYERED;
      SetWindowLong (handle, GWL_EXSTYLE, l);
      SetLayeredWindowAttributes (handle, 0, 180, LWA_ALPHA);
    end;end.
      

  6.   

    Win9x 下窗口可以抓图,填充窗体。