如何在不没有升级的 DELPHI 5 中用SetLayeredWindowAttributes();(100分)

  • 主题发起人 主题发起人 varphone
  • 开始时间 开始时间
V

varphone

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在不没有升级的 DELPHI 5 中用SetLayeredWindowAttributes();
 
与Delphi5升级不升级没有关系吧?关键是要Win2K才可以。
 
声明一下这个API就可以了!<br>问题是如果你得程序需要在Win9x和2000中同时使用,那么必须采用动态的方法!<br>函数原型如下:<br>function SetLayeredWindowAttributes(Handle : HWND; COLORKEY : COLORREF; Alpha : BYTE; Flags : DWORD) :Boolean;stdcall; external 'USER32.DLL';<br><br>
 
给你看看我以前写的一个FORM里的代码, 在WIN2K下有渐显的效果,在WIN9X下也不会出错:<br>type<br>&nbsp; TSetLayeredWindowAttributes = function(Handle: THandle; crey, bAlpha: Byte; dwFlags: Integer): LongInt; stdcall;<br><br>&nbsp; private<br>&nbsp; &nbsp; bTrans, FInterval: Byte;<br>&nbsp; &nbsp; FormClosing: Boolean;<br>&nbsp; &nbsp; LibHandle: THandle;<br>&nbsp; &nbsp; SetLayeredWindowAttributes: TSetLayeredWindowAttributes;<br><br>implementation<br><br>{$R *.DFM}<br>const<br>&nbsp; WS_EX_LAYERED = $80000;<br>&nbsp; LWA_ALPHA = $2;<br><br>procedure TForm1.TimerTimer(Sender: TObject);<br>begin<br>&nbsp; if CanTransparent then<br>&nbsp; begin<br>&nbsp; &nbsp; if FormClosing then<br>&nbsp; &nbsp; &nbsp; Dec(bTrans, FInterval)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Inc(bTrans, FInterval);<br>&nbsp; &nbsp; if @SetLayeredWindowAttributes &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; SetLayeredWindowAttributes(Handle, 0, bTrans, LWA_ALPHA);<br>&nbsp; &nbsp; Timer.Enabled := (bTrans &lt;= 255 - FInterval) and (bTrans &gt;= FInterval);<br>&nbsp; &nbsp; if not Timer.Enabled and not FormClosing then<br>&nbsp; &nbsp; &nbsp; if @SetLayeredWindowAttributes &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; SetLayeredWindowAttributes(Handle, 0, 255, LWA_ALPHA);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; LibHandle := LoadLibrary(user32);<br>&nbsp; if LibHandle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; @SetLayeredWindowAttributes := GetProcAddress(LibHandle, 'SetLayeredWindowAttributes');<br>&nbsp; end;<br>&nbsp; FInterval := 8;<br>&nbsp; if CanTransparent then<br>&nbsp; begin<br>&nbsp; &nbsp; SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);<br>&nbsp; &nbsp; if @SetLayeredWindowAttributes &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; SetLayeredWindowAttributes(Handle, 0, 0, LWA_ALPHA);<br>&nbsp; &nbsp; Timer.Enabled := True;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject;<br>&nbsp; var Action: TCloseAction);<br>var<br>&nbsp; C: DWord;<br>begin<br>&nbsp; if CanTransparent then<br>&nbsp; begin<br>&nbsp; &nbsp; FormClosing := True;<br>&nbsp; &nbsp; FInterval := FInterval * 2;<br>&nbsp; &nbsp; Timer.Enabled := True;<br>&nbsp; &nbsp; C := GetTickCount;<br>&nbsp; &nbsp; while Timer.Enabled and ((GetTickCount - C) &lt; 5000) do<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; end;<br>&nbsp; if LibHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; FreeLibrary(LibHandle);<br>end;<br><br>function TForm1.CanTransparent: Boolean;<br>begin<br>&nbsp; Result := (LibHandle &lt;&gt; 0) and (@SetLayeredWindowAttributes &lt;&gt; nil);<br>end;<br>
 
Thanks a lot of you!
 
后退
顶部