也可以不用WM_NCHITTEST,直接用mousedown,mousemove,mouseup这几个事件来.
先在Form1的Private节中声明几个临时变量:
private
ox,oy: Integer; //记录鼠标位置的临时变量
Captured: Boolean; //判断鼠标是否处于按下状态的标志
{ Private declarations }
然后在Form1的Mouse事件中加入如下代码:
//
// 鼠标按下时执行
//
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
p: TPoint;
begin
GetCursorPos(p); //取得当前鼠标位置
ox := p.x; //保存当前鼠标位置
oy := p.y;
SetCapture(Handle); //捕获鼠标
Captured := True; //设置按下标志
end;
//
// 鼠标移动时执行
//
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
p: TPoint;
begin
if Captured then //如果鼠标处于按下状态才执行
begin
GetCursorPos(p); //取得当前鼠标位置
left := left + p.x - ox; //移动窗体
top := top + p.y -oy;
ox := p.x; //保存当前鼠标位置
oy := p.y;
end;
end;
//
// 鼠标键松开时执行
//
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture(); //释放鼠标
Captured := False; //重置按下标志
end;
///-----------------------------下面是用WM_NCHITTEST的列子
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
procedure aa(var m: TMessage); Message WM_NCHITTEST;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.aa(var m: TMessage);
begin
inherited;
if m.Result = htClient then
m.Result := htCaption;
end;
end.
只需把form改成你的控件名字即可,如
anel1