• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

一个用UpdateLayeredWindow实现窗体半透明的delphi的代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

http://www.pudn.com/downloads171/sourcecode/windows/detail791686.html

 

unit Unit1;
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, gdipapi, gdipobj, Menus, StdCtrls; 
 
type 
  TForm1 = class(TForm) 
    PopupMenu1: TPopupMenu; 
    ChangeSkin1: TMenuItem; 
    N1: TMenuItem; 
    Close1: TMenuItem;
    Button1: TButton;
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
    procedure ShowPNGForm1(PNGFile:string; nTran: integer); 
    procedure ShowPNGForm2(PNGFile:string; nTran: integer); 
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; 
      Shift: TShiftState; X, Y: Integer); 
    procedure Close1Click(Sender: TObject); 
    procedure ChangeSkin1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
const 
  WS_EX_LAYERED = $80000; 
  LWA_COLORKEY  = 1; 
  LWA_ALPHA     = 2; 
  ULW_COLORKEY  = 1; 
  ULW_ALPHA     = 2; 
  ULW_OPAQUE    = 4; 
 
 
 
 
var 
  Form1: TForm1; 
  bmp, old_bmp : HBITMAP; 
  DC : HDC; 
  bitmap: tgpbitmap; 
  j:integer; 
 
 
 
 
  Function UpdateLayeredWindow(hWnd : HWND; 
                            hdcDst : HDC; pptDst : PPoint; psize : PSize; 
                            hdcSrc : HDC; pptSrc : PPoint; 
                            crKey  : COLORREF; 
                            pblend : PBlendFunction; 
                            dwFlags : DWORD): BOOL; stdcall; 
 
 
implementation 
 
{$R *.dfm} 
 
Function UpdateLayeredWindow; external 'user32.dll'; 
 
procedure Tform1.ShowPNGForm1(PNGFile:string; nTran: integer); 
var 
  graphics : TGPGraphics; 
  Image: TGPImage; 
 
begin 
   
 
  graphics := TGPGraphics.Create(DC); 
  Image:= TGPImage.Create(PNGFile); 
 
  graphics.DrawImage(image, 0, 0); 
  //UpdateLayeredWindow(Handle, Canvas.Handle, @pt1, @sz, DC, @pt2,0, @bf,ULW_ALPHA); 
end; 
 
procedure Tform1.ShowPNGForm2(PNGFile:string; nTran: integer); 
var 
  pt1, pt2 : TPoint; 
  sz : TSize; 
  bf : TBlendFunction; 
begin 
 
  bitmap:=tgpbitmap.Create(PNGFile); 
  pt1 := Point(left,top); //窗口做上角的坐标 
  pt2 := Point(0, 0); //这个就不用说了,一看见(0,0)就应该明白了 
  sz.cx := bitmap.GetWidth;  //尺寸不要超过图像大小,不然窗口就什么都没有了,连个影子都没有 
  sz.cy := bitmap.GetHeight;  //同上 
  bf.BlendOp := AC_SRC_OVER; //这些死记就行了 
  bf.BlendFlags := 0;                  //同上 
  if (nTran<0) or (nTran>255) then nTran:=255; 
  bf.SourceConstantAlpha := nTran;  //同上 
  bf.AlphaFormat := AC_SRC_ALPHA; //同上 


 
  DeleteObject(bmp); //前面就是在这里犯的错误,不然占用的内存会无限增大 
  bitmap.GetHBITMAP(0,bmp); // HBITMAP是windows标准位图格式,支持透明,这里是从tgpbitmap 转化成 HBITMAP 
  DeleteDC(DC); 
  DC := CreateCompatibleDC(Canvas.Handle); //API的含义我倒是明白,为什么写这句话我就不明白了,反正就是需要 
  old_bmp := SelectObject(DC, bmp); //同上 
  UpdateLayeredWindow(Handle, Canvas.Handle, @pt1, @sz, DC, @pt2,0, @bf,ULW_ALPHA); 
end; 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  if SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED) = 0 then ShowMessage(SysErrorMessage(GetLastError));

  ShowPNGForm2('test.png',255); 
  J:=1;
end; 
 
procedure TForm1.FormDestroy(Sender: TObject); 
begin 
  SelectObject(DC, old_bmp); 
  DeleteObject(bmp); 
  DeleteDC(DC); 
  bitmap.Free; 
end; 
 
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  if(Button = mbLeft) then 
    begin 
      ReleaseCapture(); 
      Perform(WM_SYSCOMMAND, SC_MOVE or HTCAPTION, 0); 
    end; 
end; 
 
procedure TForm1.Close1Click(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TForm1.ChangeSkin1Click(Sender: TObject); 
var 
  dlgOpen: TOpenDialog; 
begin 
  dlgOpen := TOpenDialog.Create(Self); 
  dlgOpen.Filter := 'PNG file(*.png)|*.png'; 
  if(dlgOpen.Execute()) then 
    begin 
      ShowPNGForm2(dlgOpen.FileName,255); 
    end; 
  dlgOpen.Free; 
end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('aaa');
end;

end.

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

object Form1: TForm1
  Left = 387
  Top = 124
  BorderStyle = bsNone
  Caption = 'Form1'
  ClientHeight = 165
  ClientWidth = 260
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PopupMenu = PopupMenu1
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnMouseDown = FormMouseDown
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 104
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object PopupMenu1: TPopupMenu
    Left = 16
    Top = 16
    object ChangeSkin1: TMenuItem
      Caption = 'ChangeSkin'
      OnClick = ChangeSkin1Click
    end
    object N1: TMenuItem
      Caption = '-'
    end
    object Close1: TMenuItem
      Caption = 'Close'
      OnClick = Close1Click
    end
  end
end

 

//背景穿透 (相当于锁定桌面)
procedure TForm1.N3Click(Sender: TObject);
begin
SetWindowLong(Self.Handle,
GWL_EXSTYLE,
GetWindowLong(Self.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT);
Application.MessageBox('你已关闭不了程序了,请从任务管理器里关闭',
'提示',MB_OK or MB_ICONINFORMATION);
end;

 

http://www.cnblogs.com/-clq/archive/2012/08/22/2650705.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DelphiTcxTreeListColumn的ImageComboBox用法发布时间:2022-07-18
下一篇:
Delphi正则表达式(TRegExpr)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap