最近有網友問道如何用 Delphi 實現
Delphi 的 TForm
Form
這只要捕獲窗口的 WM_NCHITTEST 消息
Windows
property AlphaBlend default False; // 是否使用半透明效果
property AlphaBlendValue default
property TransparentColor default False; // 是否使用穿透色
property TransparentColorValue default
(*此功能僅 Windows
這將使用到 ActiveX 單元的 IDropTarget 接口
TForm
end;
並在窗口擁有句柄後
以下是實現的代碼:
unit DropBin;
interface
uses
Windows
Dialogs
type
TfrmDropBin = class(TForm
private
procedure WMNCHitTest(var Msg:TWMNCHitTest); message WM_NCHITTEST;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure DoClose(var Action: TCloseAction); override;
// DragDrop 支持
function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
function IDropTarget_DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
function IDropTarget
function DragLeave: HResult; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
public
constructor Create(AOwner: TComponent); override;
end;
var
frmDropBin: TfrmDropBin;
procedure ShowDropBin(Sender: TMenuItem);
implementation
{$R *
type
// 雖然 Delphi 的 Windows 單元定義了 SetLayeredWindowAttributes(); ( external
// 但為了兼容 Win
TSetLayeredWindowAttributes = function (Hwnd: THandle; crKey: COLORREF; bAlpha: Byte; dwFlags: DWORD): Boolean; stdcall;
var
User
SetLayeredWindowAttributes: TSetLayeredWindowAttributes = nil;
procedure ShowDropBin(Sender: TMenuItem);
begin
if Assigned(frmDropBin) then frmDropBin
else begin
frmDropBin := TfrmDropBin
end;
end;
constructor TfrmDropBin
begin
inherited Create(AOwner);
Width :=
Height :=
end;
procedure TfrmDropBin
begin
inherited CreateParams(Params);
with Params do begin
Style := WS_POPUP or WS_CLIPSIBLINGS {or WS_BORDER};
ExStyle := WS_EX_TOOLWINDOW or WS_EX_TOPMOST;
end;
end;
procedure TfrmDropBin
begin
inherited CreateWnd;
Visible := True;
// 為
if Assigned(SetLayeredWindowAttributes) then begin
SetWindowLong(Handle
SetLayeredWindowAttributes(Handle
end;
// 設置為接受拖拽
OleCheck(RegisterDragDrop(Handle
end;
procedure TfrmDropBin
begin
if HandleAllocated then RevokeDragDrop(Handle);
inherited DestroyWnd;
end;
function TfrmDropBin
begin
//
// 也可以在此判斷是否接受拖拽
//
dwEffect := DROPEFFECT_COPY;
Result := S_OK;
end;
function TfrmDropBin
begin
dwEffect := DROPEFFECT_COPY;
Result := S_OK;
end;
function TfrmDropBin
begin
Result := S_OK;
end;
function TfrmDropBin
begin
//
// 處理 dataObj 中包含的拖拽內容
//
dwEffect := DROPEFFECT_NONE;
Result := S_OK;
end;
procedure TfrmDropBin
begin
Action := caFree;
frmDropBin := nil;
end;
procedure TfrmDropBin
begin
// 通過 Client 區拖動窗口
DefaultHandler(Msg);
if Msg
Msg
end;
initialization
OleInitialize(nil);
// 為兼容 Win
User
if User
finalization
OleUninitialize;
end
From:http://tw.wingwit.com/Article/program/Delphi/201311/25040.html