enum enumAttachStyle
{
AS_NONE = 0, // 没有粘贴 AS_TOP = 1,
AS_BOTTOM = 2,
AS_T_TOP = 3, AS_LEFT = 1,
AS_RIGHT = 2,
AS_L_LEFT = 3
};
//---------------------------------------------------------------------------
class CFormAttachStyle
{
public:
bool Enabled;
TForm *AttachTo;
int XStyle;
int YStyle;
int xPos;
int yPos; CFormAttachStyle()
{
XStyle=AS_NONE;
YStyle=AS_NONE;
Enabled=true;
AttachTo=NULL;
}
};
{
AS_NONE = 0, // 没有粘贴 AS_TOP = 1,
AS_BOTTOM = 2,
AS_T_TOP = 3, AS_LEFT = 1,
AS_RIGHT = 2,
AS_L_LEFT = 3
};
//---------------------------------------------------------------------------
class CFormAttachStyle
{
public:
bool Enabled;
TForm *AttachTo;
int XStyle;
int YStyle;
int xPos;
int yPos; CFormAttachStyle()
{
XStyle=AS_NONE;
YStyle=AS_NONE;
Enabled=true;
AttachTo=NULL;
}
};
能帮手看看么?
enumAttachStyle=(
AS_NONE , // 没有粘贴 AS_TOP ,
AS_BOTTOM ,
AS_T_TOP , AS_LEFT ,
AS_RIGHT ,
AS_L_LEFT
);
//---------------------------------------------------------------------------
TFormAttachStyle = class(TObject) public:
Enabled: boolean;
AttachTo: TForm;
XStyle: enumAttachStyle;
YStyle: enumAttachStyle;
xPos: Integer;
yPos: Integer; contructor Create();
end;contructor TFormAttachStyle.Create();
begin XStyle := AS_NONE;
YStyle := AS_NONE;
Enabled := true;
AttachTo:=nil;
end;
帮我完全转换下吧,再加200分!
//CDFinfes.h文件
//---------------------------------------------------------------------------#ifndef CDefinesH
#define CDefinesH
//---------------------------------------------------------------------------
// 吸附的方式
enum enumAttachStyle
{
AS_NONE = 0, // 没有粘贴 AS_TOP = 1,
AS_BOTTOM = 2,
AS_T_TOP = 3, AS_LEFT = 1,
AS_RIGHT = 2,
AS_L_LEFT = 3
};
//---------------------------------------------------------------------------
class CFormAttachStyle
{
public:
bool Enabled;
TForm *AttachTo;
int XStyle;
int YStyle;
int xPos;
int yPos; CFormAttachStyle()
{
XStyle=AS_NONE;
YStyle=AS_NONE;
Enabled=true;
AttachTo=NULL;
}
};
//---------------------------------------------------------------------------
void AdjuctFormPos(TForm *My,RECT *r);
void Do_WM_MOVING(TForm *My,TMessage &Msg);
void Do_WM_MOVE(TForm *My,TMessage &Msg);
//---------------------------------------------------------------------------
#endif
//CDefines.CPP---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop#include "CDefines.h"
//---------------------------------------------------------------------------//---------------------------------------------------------------------------
#pragma package(smart_init)//---------------------------------------------------------------------------
// 整数i1和i2的差的绝对值小于i3
bool DistanceIn(unsigned int i1,unsigned int i2,unsigned int i3)
{
if(i1>i2)
{ // 确保i2>=i1;
int t=i1;
i1=i2;
i2=t;
}
return i2-i1<=i3;
}
//---------------------------------------------------------------------------
// i1<=i2<i3
bool Mid(unsigned int i1,unsigned int i2,unsigned int i3)
{
return ((i1<=i2) && (i2<i3));
}
//---------------------------------------------------------------------------
// 把窗体My粘到Form窗体
bool AttachToForm(TForm *My,TForm *Form,RECT *r,int space)
{
// 只能粘贴到主窗体
// 可以在这里处理为可以粘贴到任意窗口
Form = Application->MainForm; CFormAttachStyle *FormStyle=(CFormAttachStyle *)Form->Tag;//主要是这里不明白
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//主要是这里不明白 if(MyStyle==NULL)return false; // 这个不能粘贴
// 检查Form是否粘贴到了My
if(FormStyle)
{
if(FormStyle->AttachTo==My)return false; // 不能循环粘贴
}
// 主窗体的位置
RECT rMain;
GetWindowRect(Form->Handle,&rMain); MyStyle->AttachTo=NULL;
MyStyle->yPos=r->top;
MyStyle->xPos=r->left;
// 上下方向判断
MyStyle->YStyle=AS_NONE;
if(Mid(rMain.left,r->left,rMain.right) || Mid(r->left,rMain.left,r->right) || (MyStyle->XStyle!=AS_NONE))
{
if(DistanceIn(r->top,rMain.bottom,space))
{
MyStyle->YStyle=AS_BOTTOM;
MyStyle->yPos=rMain.bottom;
}else if(DistanceIn(r->top,rMain.top,space))
{
MyStyle->YStyle=AS_TOP;
MyStyle->yPos=rMain.top;
}else if(DistanceIn(r->bottom,rMain.top,space))
{
MyStyle->YStyle=AS_T_TOP;
MyStyle->yPos=rMain.top-(r->bottom-r->top);
}
}
// 左右方向判断
MyStyle->XStyle=AS_NONE;
if(Mid(rMain.top,r->top,rMain.bottom) || Mid(r->top,rMain.top,r->bottom) || (MyStyle->YStyle!=AS_NONE))
{
if(DistanceIn(r->left,rMain.left,space))
{
MyStyle->XStyle=AS_LEFT;
MyStyle->xPos=rMain.left;
}else if(DistanceIn(r->left,rMain.right,space))
{
MyStyle->XStyle=AS_RIGHT;
MyStyle->xPos=rMain.right;
}else if(DistanceIn(r->right,rMain.left,space))
{
MyStyle->XStyle=AS_L_LEFT;
MyStyle->xPos=rMain.left-(r->right-r->left);
}
}
My->Left=MyStyle->xPos;
My->Top=MyStyle->yPos;
if(MyStyle->XStyle!=AS_NONE || MyStyle->YStyle!=AS_NONE)
{ // 吸附完成
MyStyle->AttachTo=Form;
}
return bool(MyStyle->AttachTo);
}
//---------------------------------------------------------------------------
// 实现窗体粘贴
// space 可以粘贴的距离
bool AttachForm(TForm *My,RECT *r,int space)
{
CFormAttachStyle *MyStyle=(CFormAttachStyle *)(My->Tag);;//主要是这里不明白 if(MyStyle)
{
for(int i=0;i<Screen->FormCount;i++)
{
TForm *Form=Screen->Forms[i];
if(Form!=My) // 不同窗体
{
if(AttachToForm(My,Form,r,space))return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------
// 移动被粘贴在一起的其它窗体
void UnionOtherForm(TForm *My,TForm *Form,int dx,int dy)
{
if(Form==NULL)return;
CFormAttachStyle *MyStyle=(CFormAttachStyle *)(Form->Tag);;//主要是这里不明白 if(MyStyle)
{
if(MyStyle->Enabled && MyStyle->AttachTo==My)
{
MyStyle->Enabled=false;
int X1=Form->Left;
int Y1=Form->Top;
SetWindowPos(Form->Handle,My->Handle,X1+dx,Y1+dy,Form->Width,Form->Height,SWP_NOSIZE|SWP_NOACTIVATE);
MyStyle->Enabled=true;
}
}
}
//---------------------------------------------------------------------------
// 移动被粘贴在一起的其它窗体
void AdjuctFormPos(TForm *My,RECT *r)
{
// 调整窗口位置
int dy=r->top-My->Top;
int dx=r->left-My->Left;
My->Top=r->top;
My->Left=r->left;
// 调整被吸附的窗口位置
for(int i=0;i<Screen->FormCount;i++)
{
TForm *Form=Screen->Forms[i];
if(Form!=My)
{
UnionOtherForm(My,Form,dx,dy);
}
}
}
//---------------------------------------------------------------------------
// 处理WM_MOVING事件
void Do_WM_MOVING(TForm *My,TMessage &Msg)
{
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//主要是这里不明白 if(MyStyle && MyStyle->Enabled)
{
MyStyle->Enabled=false;
RECT *r=(RECT *)Msg.LParam ;
// 处理粘贴
AttachForm(My,r,12); MyStyle->Enabled=true;
}
Msg.Result=1; // 通知系统,消息已经处理
}
//---------------------------------------------------------------------------
// 处理WM_MOVE事件
void Do_WM_MOVE(TForm *My,TMessage &Msg)
{
// 处理粘贴成功后的位置调整
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//主要是这里不明白 if(MyStyle && MyStyle->Enabled)
{
if(MyStyle->Enabled && MyStyle->AttachTo)
{ // 粘贴成功
My->Left=MyStyle->xPos;
My->Top=MyStyle->yPos;
}
}
Msg.Result=1; // 通知系统,消息已经处理
}
//---------------------------------------------------------------------------
Tag为TComponent一个保留的不知名的东东,为Integer类型,一般用处很多,最多做标识,我也一般用它这样做。
因为SizeOf(Integer)为4,也就是32位(和你的OS有关),在其它类型中,除了byte, boolean, word之类的,一般
的Pointer, String, TObject一般是占用4个字节,即32位,所以一般可以用Tag也保存它的地址,然后再访问,你的程序就是一个例子。你看一下它是怎么保存的,可能是这样:
BCB:
Style = CFormAttachStyle();
Style...
Form->Tag = (int*)(void*)Style;
CFormAttachStyle *Style = (CFormAttachStyle*)Form->Tag;Delphi:
Style := TFormAttachStyle.Create;
Style...
Form.Tag := Integer(Style); // or Integer(Pointer(Style));
Style := TFormAttachStyle(Form.Tag);其它类似:
PData = ^TData;
TData = record
....
end;
var
Data: PData;
begin New(Data);
Data^.....;
Form.Tag := Integer(Data); Data := PData(Form.Tag);
Data^....
end;
unit CDefines;interfaceuses Windows, Messages, Classes, Forms;type
TEnumAttachStyle = (AS_NONE, AS_TOP, AS_BOTTOM, AS_T_TOP, AS_LEFT, AS_RIGHT, AS_L_LEFT); TFormAttachStyle = class
public
constructor Create;
Enabled: Boolean;
AttachTo: TForm;
XStyle: Integer;
YStyle: Integer;
XPos: Integer;
YPos: Integer;
end;implementationprocedure AdjustFormPos(Form: TForm; var Rect: TRect);
procedure Do_WM_MOVEING(Form: TForm; var msg: TMessage);
procedure Do_WM_MOVE(Form: TForm; var msg: TMessage);// 整数i1和i2的差的绝对值小于i3
function DistnaceIn(i1, i2, i3: DWORD): Boolean;
var
Temp: DWORD;
begin
if i1 > i2 then
begin
Temp := i1;
i1 := i2;
i2 := Temp;
end;
Result := i2 - i1 <= i3;
end;// i1<=i2<i3
function Mid(i1, i2, i3: DWORD): Boolean;
begin
Result := (i1 <= i2) and (i2 < i3);
end;// 把窗体My粘到Form窗体
function AdjustToForm(My, Form: TForm; var Rect: TRect; space: Integer): Boolean;
var
MainForm: TForm;
rMain: TRect;
MyStyle, FormStyle: TFormAttachStyle;
begin
// 只能粘贴到主窗体
// 可以在这里处理为可以粘贴到任意窗口
MainForm := Application.MainForm;
//Tag 是一个Integer,它可以保存Pointer类型,Tag := Integer(Pointer);
//即所有的TObject的传换(Integer(Pointer(Object: TObject));
MyStyle := TFormAttachStyle(My.Tag);
FormStyle := TFormAttachStyle(Form.Tag);
if Assigned(MyStyle) then
begin
Result := False;
Exit;
end;
if Assigned(FormStyle) and (FormStyle.AttachTo = My) then // 不能循环粘贴
begin
Result := False;
Exit;
end;
GetWindowRect(Form.Handle, rMain);
MyStyle.AttachTo := nil;
MyStyle.XPos := Rect.Left; //MyStyle->XPos := r.Left <-- r.Left 是rMain.Left吧
MyStyle.YPos := Rect.Top;
// 上下方向判断
MyStyle.YStyle := AS_NONE;
if Mid(rMain.Left, Rect.Left, rMain.Right) or (Mid(Rect.Left, rMain.Left, Rect.Right) or (MyStyle.XStyle <> Ord(AS_NONE))
begin
if DistanceIn(Rect.Top, rMain.Bottom, space) then
begin
MyStyle.YStyle := AS_BOTTOM;
MyStyle.YPos := rMain.Bottom;
end else
if DistanceIn(Rect.Top, rMain.Top, space) then
begin
MyStyle.YStyle := AS_TOP;
MyStyle.YPos := rMain.Top;
end else
if DistanceIn(Rect.Bottom, rMain.Top, space) then
begin
MyStyle.YStyle := AS_T_TOP;
MyStyle.YPos := rMain.Top - (Rect.Bottom - Rect.Top);
end;
end; // 左右方向判断
MyStyle.XStyle := AS_NONE;
if (Mid(rMain.Top, Rect.Top, rMain.Bottom) or (Mid(Rect.Top, rMain.Top, Rect.Bottom) or (MyStyle.YStyle <> Ord(AS_NONE)) then
begin
if DistanceIn(Rect.left, rMain.Left, space) then
begin
MyStyle.XStyle := AS_LEFT;
MyStyle.xPos := rMain.left;
end else
if DistanceIn(Rect.Left, rMain.Right, space) then
begin
MyStyle.XStyle := AS_RIGHT;
MyStyle.XPos := rMain.Right;
end else
if DistanceIn(Rect.Right, rMain.Left, space) then
begin
MyStyle.XStyle := AS_L_LEFT;
MyStyle.XPos := rMain.Left - (Rect.Right - Rect.Left);
end;
end;
My.Left := MyStyle.XPos;
My.Top := MyStyle.YPos; if (MyStyle.XStyle <> Ord(AS_NONE)) or (MyStyle.YStyle <> Ord(AS_NONE))
MyStyle.AttatchTo := Form;
Result := Assigned(MyStyle.AttachTo);
end;// 实现窗体粘贴
// space 可以粘贴的距离
function AttachForm(My: TForm; var Rect: TRect, space: Integer): Boolean;
var
I: Integer;
Form: TForm;
MyStyle: TFormAttachStyle;
begin
Result := False;
MyStyle := TFormAttachStyle(My.Tag);
if Assigned(MyStyle) then
begin
for I := 0 to Screen.FormCount - 1 do
begin
Form := Screen.Forms[I];
if Form <> My then
if AttachToForm(My, Form, Rect, space) then
begin
Result := True;
Exit;
end;
end;
end;
end;// 移动被粘贴在一起的其它窗体
procedure UnionOtherForm(My, Form: TForm; dx, dy: Integer);
var
X1, Y1: Integer;
MyStyle: TFormAttachStyle;
begin
if not Assigned(Form) then Exit;
MyStyle := TFormAttachStyle(Form.Tag);
if Assigned(MyStyle) and MyStyle.Enabled and (MyStyle.AttachTo = My) then
begin
MyStyle.Enabled := False;
X1 := Form.Left;
Y1 := Form.Top;
SetWindowPos(Form.Handle, My.Handle, X1 + dx, Y1 + dy,
Form.Width, Form.Height, SWP_NOSIZE or SWP_NOACTIVATE);
MyStyle.Enabled := False;
end;
end;// 移动被粘贴在一起的其它窗体
procedure AdjustFormPos(My: TForm; var R: TRect);
var
Form: TForm;
I, dx, dy: Integer;
begin
dx := R.Top - My.Top;
dy := R.Left - My.Left;
My.TOp := R.Top;
My.Left := R.Left;
for I := 0 to Screen.FormCount - 1 do
begin
Form := Screen.Forms[I];
if Form <> My then
UnionOther(My, Form, dx, dy);
end;
end;// 处理WM_MOVING事件
procedure DO_WM_MOVEING(My: TForm; var msg: TMessage);
var
Rect: TRect;
MyStyle: TFormAttachStyle;
begin
MyStyle := TFormAttachStyle(My.Tag);
if Assigned(MyStyle) and MyStyle.Enabled then
begin
MyStyle.Enabled := False;
Rect := PRect(msg.LParam)^;
AttachForm(My, Rect, 12);
MyStyle.Enabled := True;
end;
msg.Result := 1;
end;// 处理WM_MOVE事件
procedure Do_WM_MOVE(My: TForm; var msg: TMessage);
var
MyStyle: TFormAttachStyle;
begin
MyStyle := TFormAttachStyle(My.Tag);
if Assigned(MyStyle) and MyStyle.Enabled and Assigned(MyStyle.AttachTo) then
begin
My.Left := MyStyle.XPos;
My.Top := MyStyle.YPos;
end;
msg.Result := 1;
end;end.
// 建立磁性特性类
CFormAttachStyle *AttachStyle=new CFormAttachStyle;
Tag=(int)AttachStyle;
那么DELPHI是不是这样呢?
new(AttachStyle);
Tag:=Integer(AttachStyle) ;
AttachStyle : ^TFormAttachStyle;
BCB代码为:
void __fastcall TfmMain::WndProc(TMessage &Msg)
{
TForm::WndProc(Msg);
switch(Msg.Msg)
{
case WM_MOVING: // 处理移动事件
{
AdjuctFormPos(this,(RECT *)(Msg.LParam));
// Do_WM_MOVING(this,Msg);
break;
}
}
}
//---------------------------------------------------------------------------
__fastcall TfmMain::TfmMain(TComponent* Owner)
: TForm(Owner)
{
// 建立磁性特性类
CFormAttachStyle *AttachStyle=new CFormAttachStyle;
Tag=(int)AttachStyle;
}DELPHI为:
var
AttachStyle : ^TFormAttachStyle;
...
procedure OnMoveing(var Msg: TMessage); message WM_MOVING;
....
procedure TForm1.OnMoveing(var Msg: TMessage);
begin
AdjustFormPos(Form1,PRect(Msg.LParam)^);
Label1.Caption := inttostr(AttachStyle.yPos);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
new(AttachStyle);
Tag:=Integer(Pointer(AttachStyle)) ;
end;
procedure aaa(P: Pointer);
begin
end;procedure TForm1.OnCreate;
begin
aaa(Self);
aaa(Form1);
end;所以:
var
AttachStyle: TFormAttachStyle; //AttachStyle其实就是一个指针procedure TForm1.FormCreate(Sender: TObject);
begin
// new(AttachStyle);
AttachStyle := TFormAttachStyle.Create;
//Tag := Integer(AttachStyle);
Tag:=Integer(Pointer(AttachStyle));
end;