zoukankan      html  css  js  c++  java
  • delphi中窗体半透明效果如何实现

    http://www.xuedelphi.cn/wenzhang/mrjq/jyjq/2008/03/200803192122.htm

    核心API函数就_是SetLayeredWindowAttributes。
       要实现_淡入淡出效果,就_需要实现窗口的可调整的透_明效果。传统的Windows应用程序_想实现透_明效果,一般来说需要处理自己的窗口的 WM_Paint消息,程_序员需要GetDC获取屏幕的HDC,调用BitBlt函数将屏幕将要被覆盖的区域拷贝到内存的TBitmap对象中,然后_ 对该Tbitmap的ScanLine二维数组逐象素的修改rgbtRed、rgbtGreen和rgbtBlue值。天哪,实在_是太麻烦了。
      Windows2000的API库中终于提供了半透明的窗体显示效果支持(虽然不是很完善)。要实现半透明的窗口效果,首先需要给创建的窗口添加WS_EX_LAYERED 扩展属_性,这是一个新的窗口扩展属性,Delphi5并没有定义相应的常量和SetLayeredWindowAttributes函数,我们需_要手工声明。
    function SetLayeredWindowAttributes(Handle: HWND;
      COLORKEY: COLORREF; Alpha: BYTE; Flags: DWORD): Boolean; stdcall; external 'USER32.DLL';

    Const
      WS_EX_LAYERED = $80000;
      LWA_ALPHA = 2;

      我们调用GetWindowLong函数获取当前窗口的扩展属性,并调用SetWindowLong函数将新的WS_EX_LAYERED窗口扩展属性添加进去。

    procedure TForm1.FormCreate(Sender: TObject);///////////
    begin
      SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE) or WS_EX_LAYERED);
    end;

      现在我们的窗口已经可以调用SetLayeredWindowAttributes函数,通过设置该函数的Alpha参数,我们就可以看到窗口的效果的变化。

    procedure TForm1.Button1Click(Sender: TObject);////////////
    begin
      SetLayeredWindowAttributes(Form1.Handle, 0, 180, LWA_ALPHA);
    end;

      以下的VCL控件代码封装了SetLayeredWindowAttributes函数,编程时动态改变AlphaValue值,您就可以看到窗口的透明效果了。控件屏蔽了设计期的显示效果,如果读者愿意可以改为设计期效果可见,不过那样的话,一不小心,您可能就会找不着你要设计的窗体了 8-)

    unit TranForm; {DragonPC 2001.2.21 }

    interface

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

    type
      TTranForm = class(TComponent)
      private
       FAlphaValue : integer ;////////////
       FParentFormHandle : HWND ;
      procedure SetFAlphaValue(Alpha:integer) ;
       protected
       procedure UpdateDisplay ;/////////////
    public
      constructor Create(AOwner: TComponent); override;
    published
      property AlphaValue : integer read FAlphaValue write SetFAlphaValue ;
    end;

    procedure Register;

    function SetLayeredWindowAttributes(Handle: HWND; COLORKEY: COLORREF;
      Alpha: BYTE; Flags: DWORD): Boolean; stdcall; external 'USER32.DLL';

    implementation

    procedure Register;
    begin
      RegisterComponents('Standard', [TTranForm]);
    end;

    procedure TTranForm.SetFAlphaValue(Alpha: integer);
      begin
       if (Alpha >= 0) and (Alpha < 256) then begin
       FAlphaValue := Alpha ;
       UpdateDisplay() ;//////////////
      end;
    end;

    procedure TTranForm.UpdateDisplay;
    begin
     if (csDesigning in ComponentState) then Exit ;
     SetLayeredWindowAttributes(FParentFormHandle, 0, FAlphaValue, 2);
    end;

    constructor TTranForm.Create(AOwner: TComponent);///////////////////////
    begin
      inherited;
      if (csDesigning in ComponentState) then Exit;
      FAlphaValue := 255 ;
      FParentFormHandle := TForm(AOwner).Handle ;
      SetWindowLong(FParentFormHandle,
             GWL_EXSTYLE,
             GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or WS_EX_LAYERED);
    end;

    end.

    //////////////////////////

  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/chulia20002001/p/2043733.html
Copyright © 2011-2022 走看看