PS:
1.form2是主窗体,form1是子窗体,我当时安装的是XE8,新建第一个窗体就是叫form2。
2.事件处理用到了控件(ApplicationEvents1)。
3.源代码下载地址:“https://download.csdn.net/download/zhujianqiangqq/7551317”。
1 unit Unit2; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, unit1, Vcl.StdCtrls, Vcl.ExtCtrls, 8 Vcl.AppEvnts; 9 10 type 11 TForm2 = class(TForm) 12 ApplicationEvents1: TApplicationEvents; 13 Label1: TLabel; 14 procedure MYHideMessage(var Msg: tagMSG; var Handled: Boolean); 15 private 16 { Private declarations } 17 18 public 19 { Public declarations } 20 21 end; 22 23 var 24 Form2: TForm2; 25 implementation 26 27 {$R *.dfm} 28 29 procedure TForm2.MYHideMessage(var Msg: tagMSG; var Handled: Boolean); 30 var 31 pt:TPoint; 32 bit: TBitmap; 33 begin 34 case Msg.message of 35 WM_LBUTTONDOWN: //鼠标左键按下显示虚窗体 36 begin 37 //复制一个主窗体的图片 38 bit := TBitmap.Create; 39 bit.Width := Width; 40 bit.Height := Height; 41 BitBlt(bit.Canvas.Handle, 0, 0, Width, Height, GetWindowDC(Handle), 0, 0, SRCCOPY); 42 //虚窗体加载图片 43 Form1.Image1.Picture.Assign(bit); 44 //我这里为了方便大家看代码没有用TRY,大家写时注意了 45 bit.Free; 46 //设置虚窗体的大小 47 Form1.Height:=Form2.Height; 48 Form1.Width:=Form2.Width; 49 Form1.Image1.Align:=alClient; 50 Form1.BorderStyle:=bsNone; 51 //显示虚窗体 52 Form1.Show; 53 //设置透明度 54 Form1.AlphaBlend:=True; 55 Form1.AlphaBlendValue:=100; 56 end; 57 WM_MOUSEMOVE: //鼠标移动虚窗体根着移动 58 begin 59 //取出鼠标的位置 60 GetCursorPos(pt); 61 Caption:='X坐标: '+inttostr(pt.X)+' Y坐标: '+inttostr(pt.Y); 62 //设置虚窗体的位置 63 Form1.Top:=pt.Y; 64 Form1.Left:=pt.X; 65 end; 66 WM_LBUTTONUP: //鼠标左键跳起虚窗体隐藏 67 begin 68 Form1.Hide; 69 end; 70 end; 71 end; 72 73 end.