Delphi不规则窗体实现

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dxGDIPlusClasses, StdCtrls, Menus, ExtCtrls;
type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure N1Click(Sender: TObject);
//    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
{$R Background.RES}
procedure SetCtrlBlendGraphic(ACtrl : TWinControl; AGraphic : TGraphic);
var
  ptDst, ptSrc: TPoint;
  Size: TSize;
  BlendFunction: TBlendFunction;
  bmp : TBitmap;
  oDC: Cardinal;
begin
  bmp := TBitmap.Create;
  bmp.Assign(AGraphic);
  ptDst := Point(ACtrl.Left, ACtrl.Top);
  ptSrc := Point(0, 0);
  Size.cx := AGraphic.Width;
  Size.cy := AGraphic.Height;
  BlendFunction.BlendOp := AC_SRC_OVER;//AC_SRC_ALPHA;
  BlendFunction.BlendFlags := 0;
  BlendFunction.SourceConstantAlpha := 255; // 透明度
  BlendFunction.AlphaFormat := AC_SRC_ALPHA;
  SetWindowLong(ACtrl.Handle, GWL_EXSTYLE, GetWindowLong(ACtrl.Handle,
      GWL_EXSTYLE) or WS_EX_LAYERED);
  oDC := GetDC(ACtrl.Handle);
  UpdateLayeredWindow(ACtrl.Handle,
     oDC,
     @ptDst,
     @Size,
     bmp.Canvas.Handle,
     @ptSrc,
     0,
     @BlendFunction,
     ULW_ALPHA);
  bmp.Free();
  ReleaseDC(ACtrl.Handle, oDC);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
  oPng: TdxPNGImage;
begin
  inherited;                    
  oPng := TdxPNGImage.Create;
  try
    oPng.LoadFromResource(HInstance, 'Background', RT_RCDATA);
    SetCtrlBlendGraphic(Self, oPng);
    SetWindowPos(Handle, 0, Screen.Width - oPng.Width, 0, oPng.Width, oPng.Height, SWP_SHOWWINDOW);
  finally
    oPng.free;
  end;
end;
var
  oPnt: TPoint;
  bMove: Boolean = False;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bMove := True;
  GetCursorPos(oPnt);
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  oPnt2: TPoint;
begin
  if not bMove then Exit;
  GetCursorPos(oPnt2);
  Left := Left + (oPnt2.X - oPnt.X);
  Top := Top + (oPnt2.Y - oPnt.Y);
  GetCursorPos(oPnt);
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bMove := False;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
  Close;
end;
end.
相关文章
相关标签/搜索