在线等待,如何将如下代码(C++ Builder)转成DELPHI的?(50分)

L

lfpsoft

Unregistered / Unconfirmed
GUEST, unregistred user!
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;
}
};

 
const
AS_NONE = 0; // 没有粘贴
AS_TOP = 1;
AS_BOTTOM = 2;
AS_T_TOP = 3;
AS_LEFT = 1;
AS_RIGHT = 2;
AS_L_LEFT = 3;


type
TFormAttachStyle = Class
public
Enabled: bool ;
AttachTo: TForm ;
XStyle,
YStyle,
xPos,
yPos: int;

constructor Create;
end;
implementation

constructor TFormAttachStyle.Create;
begin
XStyle:=AS_NONE;
YStyle:=AS_NONE;
Enabled:=true;
AttachTo:=NULL;
end;
 
唉,有了上面的转换还是不行,对C++很不了解。
帮我完全转换下吧,再加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)

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; // 不能循环粘贴
}
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)
{
}
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)
{
}
}
}
void Do_WM_MOVING(TForm *My,TMessage &Msg)
{
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//主要是这里不明白

if(MyStyle && MyStyle->Enabled)
{
}
Msg.Result=1; // 通知系统,消息已经处理
}
 
这么长?只好200分也不要了,你自己看看,
把->改成点号,
!=改成<>,
=改成:=
==改成=
TForm *Form 改成 Form: TForm;
{改成begin, }改成 end
||改成and,&&改成or
别的还不明白的,你就慢慢看看书吧,
 
其实也不长。
关键是以下的代码我不明白。
既然MyStyle 已定义为TFormAttachStyle类,My为TForm,那为什么
MyStyle可以等于My.Tag呢?
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//主要是这里不明白
 
我已经将我明白的代码都已删去,哪位帮帮忙吧,分不够还可以再加!
 
Form的 tag 是一个和 CFormAttachStyle 的指针 兼容的值。
你可以查看一下 tag 的具体赋值。
 
CFormAttachStyle *MyStyle=(CFormAttachStyle *)My->Tag;;//这里应如何写?
它是这样赋值的
CFormAttachStyle *AttachStyle=new CFormAttachStyle;
Tag=(int)AttachStyle;
 
这是对的,在赋值时将CFormAttachStyle的指针作为整型值。

可以声明一个指针类型
type
PFormAttachStyle=^CFormAttachStyle

然后这样来处理:
var
MyStyle:pFormAttachStyle;
begin
MyStyle:=PFormAttachStyle(My.Tag);
end;

原程序中的处理,只是可能困为 tag 只能为整型值!!!
 
那下面这句又应如何转呢?
if(MyStyle && MyStyle->Enabled)


另外在赋值时
CFormAttachStyle *AttachStyle=new CFormAttachStyle;
Tag=(int)AttachStyle;
转为:
var
AttachStyle : PFormAttachStyle;

new(AttachStyle);
tag := Integer(AttachStyle);

是这样转吗?


 
哦错了,呵呵。看错。
 
不好意思,我上边打错了。
再看看我的代码错在哪里?
 
c++中一个指针就是int
上面代码的意思就是用一个 tag 来保存一的指针 (Tag=(int)AttachStyle)
 
if MyStyle or (MyStyle.Enabled) then
此语句逻辑上不通,是不是 if (MyStyle and MyStyle->Enabled) then


第二个问题:我猜想你的意思可能是
先把一个指针临时存储在 tag 中 。。。过一会儿再从 tag 把他取出来
tag = (int)AttachStyle;//存储
......
AttachStyle = (AttachStyle*)tag;//取出
 
if(MyStyle && MyStyle->Enabled)

应该是转成:if Assigned(MyStyle) and MyStyle.Enabled then

而 另外在赋值时
CFormAttachStyle *AttachStyle=new CFormAttachStyle;
Tag=(int)AttachStyle;
应该是转为:
new(AttachStyle
Tag:=Integer(AttachStyle) ;

都没有出错了,可是就是不能实现要的功能
何解????
 
这一句:if(MyStyle && MyStyle->Enabled)
pascal 是:
if(MyStyle)and(MyStyle.Enable)then
但是我不知道你的MyStyle是什么类型的,如果是指针类型的,c++可以这样写:if(MyStyle && MyStyle->Enabled)
但是,pascal不能这样写:if(MyStyle)and(MyStyle.Enable)因为类型不对,如果MyStyl是指针类型的,除了变量的声明要改变外,
这一句可改为:
if(MyStyle<>Nil)and(MyStyle.Enable)then

 
改成这样应该行吧?
if Assigned(MyStyle) and MyStyle.Enabled then
 
多人接受答案了。
 
顶部