MOVECOMP.DPR
program MoveComp;
uses
Forms,
MoveForm in 'MoveForm.pas' {Form1},
HitForm in 'HitForm.pas' {Form2};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
MOVEFORM.PAS
unit MoveForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
RadioButton1: TRadioButton;
procedure ControlMouseDown(Sender: TObject
Button: TMouseButton;
Shift: TShiftState
X, Y: Integer);
procedure FormMouseDown(Sender: TObject
Button: TMouseButton;
Shift: TShiftState
X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
sc_DragMove: Longint = $F012;
procedure TForm1.ControlMouseDown(Sender: TObject
Button: TMouseButton;
Shift: TShiftState
X, Y: Integer);
begin
if ssCtrl in Shift then
begin
ReleaseCapture;
(Sender as TWinControl).Perform (
wm_SysCommand, sc_DragMove, 0);
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject
Button: TMouseButton;
Shift: TShiftState
X, Y: Integer);
begin
if ssCtrl in Shift then
begin
ReleaseCapture;
SendMessage (Handle, wm_SysCommand, sc_DragMove, 0);
end;
end;
end.
HITFORM.PAS
unit HitForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons;
type
TForm2 = class(TForm)
procedure FormResize(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
MoveRect, TransRect: TRect;
public
procedure WmNcHitTest (var Msg: TWmNcHitTest);
message wm_NcHitTest;
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.WmNcHitTest(var Msg: TWmNcHitTest);
var
Pt: TPoint;
begin
Pt := Point (Msg.XPos, Msg.YPos);
Pt := ScreenToClient (Pt);
if PtInRect (MoveRect, pt) then
Msg.Result := htCaption
else if PtInRect (TransRect, pt) then
Msg.Result := htTransparent
else
inherited;
end;
procedure TForm2.FormResize(Sender: TObject);
begin
MoveRect := Rect (20, 20,
ClientWidth - 20, ClientHeight div 2 - 10);
TransRect := Rect (20, ClientHeight div 2 + 10,
ClientWidth - 20, ClientHeight - 20);
Invalidate;
end;
procedure TForm2.FormPaint(Sender: TObject);
begin
// draw the rectangles
Canvas.Brush.Color:= Font.Color;
Canvas.FrameRect (MoveRect);
Canvas.FrameRect (TransRect);
// draw the text
Canvas.Brush.Color:= Color;
DrawText (Canvas.Handle, 'Move', 4, MoveRect,
dt_Center or dt_VCenter or dt_SingleLine);
DrawText (Canvas.Handle, 'Transparent', 11, TransRect,
dt_Center or dt_VCenter or dt_SingleLine);
end;
end.
MOVEFORM.DFM
object Form1: TForm1
Left = 194
Top = 108
Width = 278
Height = 213
Caption = 'MoveForm'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OnMouseDown = FormMouseDown
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 80
Top = 48
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnMouseDown = ControlMouseDown
end
object CheckBox1: TCheckBox
Left = 80
Top = 88
Width = 97
Height = 17
Caption = 'CheckBox1'
TabOrder = 1
OnMouseDown = ControlMouseDown
end
object RadioButton1: TRadioButton
Left = 80
Top = 120
Width = 113
Height = 17
Caption = 'RadioButton1'
TabOrder = 2
OnMouseDown = ControlMouseDown
end
end
HITFORM.DFM
object Form2: TForm2
Left = 477
Top = 120
Width = 234
Height = 207
Caption = 'HitForm'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Visible = True
OnPaint = FormPaint
OnResize = FormResize
PixelsPerInch = 96
TextHeight = 13
end
一个例子