已有56人关注
请教一个delphi转换成c#代码的方法
发表在C#图书答疑 2010-08-12
是否精华
版块置顶:
unit UWooolPublic;

interface

uses
  ScktComp,iniFiles,SysUtils,Windows,DecodeEncode,Controls,Sockets,jpeg,Classes,ExtCtrls,Forms;

const
  GameIniName   = 'Game.ini';
  ConfigIniname = 'Config.ini';

type
  TServerInfo = packed record
    ServerIP    : string;
    ServerPort  : String;
    GroupNum    : Integer;
    Group       : String;
    GroupNick   : String;
  end;

  TUserInfo = packed record
    UserID    : array[0..10] of char;
    PassWord  : array[0..10] of char;
    UserName  : array[0..20] of char;
    ManID     : array[0..14] of char;
    Email     : array[0..14] of char;
    Question1 : array[0..20] of char;
    Answer1   : array[0..12] of char;
    Linkname  : array[0..39] of char;

  end;

  TUserInfo1= packed record
    Question2   : array[0..20] of char;
    Answer2     : array[0..12] of char;
    ChrisDay    : array[0..10] of char;
    MobilePhone : array[0..13] of char;
    Memo1       : array[0..52] of char;
    Memo2       : array[0..52] of char;
  end;

  TEditPasswordInfo = record
    GameID      : array[0..10] of char;
    OldPassword : array[0..10] of char;
    NewPasswod  : array[0..10] of char;
  end;

  TGetPasswordinfo = record
    GameID      : array[0..9] of char;
    ChrisDay    : array[0..9] of char;
    Question1   : array[0..9] of char;
    Answer1     : array[0..9] of char;
    Question2   : array[0..9] of char;
    Answer2     : array[0..9] of char;
  end;

  TWoool = class
  private
    FFullScreen   : Boolean;    procedure SetFullScreen   (const Value: Boolean);

  protected

  public
    Flag : Char;
    property FullScreen   : Boolean       read FFullScreen   write SetFullScreen;

    {判断服务器是否在线}
    function CheckServerOnLine:Boolean;
    {登陆游戏}
    procedure LogGame;
    {注册号码}
    function RegeditWooolID:Boolean;
    {修改密码}
    function EditPassWord:Boolean;
    {取回密码}
    function GetPassWord:Boolean;
    {发送消息}
    procedure SendMsg(msg: TMsg;datasize:integer);
    {处理消息}
    procedure OnMsg(msg: TMsg;datasize:integer);    
  end;

  var
    ServerInfo       : TServerInfo ;
    ServerInfoList   : array of TServerInfo ;
    ClientSocket     : TTcpClient;
    UserInfo         : TUserInfo;
    UserInfo1        : TUserInfo1;
    Woool            : TWoool;
    EditPasswordInfo : TEditPasswordInfo;
    GetPasswordinfo  : TGetPasswordinfo;
    JpgImage         : TJpegImage ;
    JpgStream        : TResourceStream;

procedure LoadImageResource(Sender: TObject;ResourceName:String);
procedure ClearMemory; 

implementation

{ TWoool }
procedure LoadImageResource(Sender: TObject;ResourceName:String);
begin
  JpgStream := TResourceStream.Create (Hinstance,ResourceName,RT_RCDATA) ;
  try
  Jpgimage.LoadFromStream (JpgStream) ;
  (Sender as TImage).Picture.Bitmap.Assign (Jpgimage);
  finally
    FreeAndNil(JpgStream);
  end;
end;

function TWoool.CheckServerOnLine: Boolean;
begin
  Result:=False;
  try
    ClientSocket.Close;
    ClientSocket.RemoteHost := ServerInfo.ServerIP;
    ClientSocket.RemotePort := ServerInfo.ServerPort;

    if ClientSocket.Connect then
      Result:=True;
  except
  end;
end;

procedure TWoool.LogGame;
var
  GameIniFile:TIniFile;
  ConfigIniFile:TIniFile;
begin
  GameIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Game.Ini');
  ConfigIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Config.Ini');
  try
    GameIniFile.WriteString('Config','ServerIP',ServerInfo.ServerIP);
    GameIniFile.WriteString('Config','ServerPort',ServerInfo.ServerPort);
    GameIniFile.WriteInteger('Config','GroupNum',ServerInfo.GroupNum);
    GameIniFile.WriteString('Config','Group0',ServerInfo.Group);
    GameIniFile.WriteString('Config','GroupNick0',ServerInfo.GroupNick);
    
    if FullScreen then
      ConfigIniFile.WriteString('Config','FullScreen','Yes')
    else ConfigIniFile.WriteString('Config','FullScreen','No');

    WinExec( 'Data/woool.dat', SW_SHOW );

    Sleep (2000);
    Application.Terminate;
  finally
    FreeAndNil(GameIniFile);
    FreeAndNil(ConfigIniFile);
  end;
end;

function TWoool.RegeditWooolID: Boolean;
var
  codedbuffer:array[0..4095] of char;
  codedbuffer1:array[0..4095] of char;
  codedsize :integer;
  codedsize1 :integer;
  msg:TMsg;
  msg1:TMsg;
begin
  if ClientSocket.Active then
  begin

    ZeroMemory(@msg,sizeof(TMsg));
    ZeroMemory(@msg1,sizeof(TMsg));
    copyMemory(@msg.data,@UserInfo,sizeof(UserInfo));
    copyMemory(@msg1.data,@UserInfo1,sizeof(UserInfo1));

    msg.cmd := 2002;

    codedsize := Encode( @msg, sizeof(tmsgheader)+sizeof(UserInfo), @codedbuffer[2] );
    codedsize1 := Encode( @msg1.data, sizeof(UserInfo1), @codedbuffer1[2]);


    codedbuffer[0] := '#';
    codedbuffer[1] := flag;
    codedbuffer1[0] := ' ';
    codedbuffer1[1] := ' ';

    copymemory(@codedbuffer[codedsize+2],@codedbuffer1,codedsize1+2);
    codedbuffer[codedsize+codedsize1+4] := '!';

    
    ClientSocket.SendBuf(codedbuffer, codedsize+codedsize1+5 );
    Inc(Flag);
    if flag > '9' then flag := '1';
  end else MessageBox(0, '连接服务器失败!', nil, 0 );

  result:=True;

end;

function TWoool.EditPassWord: Boolean;
var
  msg:TMsg;
  codedbuffer:array[0..4095] of char;
  bufCount:Integer;
  codedsize :integer;
  
begin
  if ClientSocket.Active then
  begin
    ZeroMemory(@msg,sizeof(TMsg));
    fillchar(codedbuffer,sizeof(codedbuffer),#0);

    strlcopy( msg.data
      , EditPasswordInfo.GameID
      , Length(EditPasswordInfo.GameID) );

    msg.data[Length(Trim(EditPasswordInfo.GameID))] := #9 ;

    strlcopy( msg.data+Length(Trim(Trim(EditPasswordInfo.GameID)))+1
      , EditPasswordInfo.OldPassword
      ,Length(Trim(EditPasswordInfo.OldPassword)) );

    msg.data[Length(Trim(EditPasswordInfo.GameID))
      +Length(Trim(EditPasswordInfo.OldPassword))+1]
      := #9;

    strlcopy(
      msg.data+Length(Trim(EditPasswordInfo.GameID))
      +Length(Trim(EditPasswordInfo.OldPassword))
      +2
      , EditPasswordInfo.NewPasswod
      ,Length(Trim(EditPasswordInfo.NewPasswod)) );

    msg.data[Length(trim(EditPasswordInfo.GameID))
      +Length(trim(EditPasswordInfo.OldPassword))+2+Length(Trim(EditPasswordInfo.NewPasswod))]
      := #0;

    bufCount:=
      Length(trim(EditPasswordInfo.GameID))
      +1
      +Length(trim(EditPasswordInfo.OldPassword))
      +1
      +Length(Trim(EditPasswordInfo.NewPasswod));

    msg.cmd := 2003;
    codedsize := Encode( @msg
                        , sizeof(tmsgheader)+bufCount
                        , @codedbuffer[2] );
    codedbuffer[0] := '#';
    codedbuffer[1] := flag;
    codedbuffer[codedsize+2] := '!';
    ClientSocket.SendBuf(codedbuffer, codedsize+3 );
    Inc(Flag);
    if flag > '9' then flag := '1';
  end else MessageBox(0, '连接服务器失败!', nil, 0 );

  Result:=True;
end;

function TWoool.GetPassWord: boolean;
var
  codedbuffer:array[0..4095] of char;
  codedsize :integer;
  msg:TMsg;
  LengthStr:Integer;
begin
  if ClientSocket.Active then
  begin
    ZeroMemory(@msg,sizeof(TMsg));
    fillchar(codedbuffer,sizeof(codedbuffer),#0);
    LengthStr:=0;
    strlcopy( msg.data
      , GetPasswordinfo.GameID
      , Length(GetPasswordinfo.GameID) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.GameID));

    msg.data[LengthStr] := #9;

    strlcopy(
      msg.data+LengthStr+1
      , GetPasswordinfo.Question1
      ,Length(GetPasswordinfo.Question1) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question1))+1;

    msg.data[LengthStr] := #9;

    strlcopy(
      msg.data+LengthStr+1
      , GetPasswordinfo.Answer1
      ,Length(GetPasswordinfo.Answer1) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer1))+1;

    msg.data[LengthStr] := #9;


    strlcopy(
      msg.data+LengthStr+1
      , GetPasswordinfo.Question2
      ,Length(GetPasswordinfo.Question2) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question2))+1;

    msg.data[LengthStr] := #9;

    strlcopy(
      msg.data+LengthStr+1
      , GetPasswordinfo.Answer2
      ,Length(GetPasswordinfo.Answer2) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer2))+1;

    msg.data[LengthStr] := #9;

    strlcopy(
      msg.data+LengthStr+1
      , GetPasswordinfo.ChrisDay
      ,Length(GetPasswordinfo.ChrisDay) );

    LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.ChrisDay))+1;

    msg.cmd := 3042;

    codedsize := Encode( @msg, sizeof(tmsgheader)+LengthStr, @codedbuffer[2] );
    codedbuffer[0] := '#';
    codedbuffer[1] := flag;
    codedbuffer[codedsize+2] := '!';
    ClientSocket.SendBuf(codedbuffer, codedsize+3 );
    Inc(Flag);
    if flag > '9' then flag := '1';
  end else MessageBox(0, '连接服务器失败!', nil, 0 );
  Result:=True;
end;

procedure TWoool.SendMsg(msg: TMsg; datasize: integer);
var
  codedbuffer:array[0..4095] of char;
  codedsize :integer;
begin
  if ClientSocket.Active then
  begin
    codedsize := Encode( @msg, sizeof(tmsgheader)+datasize, @codedbuffer[2] );
    codedbuffer[0] := '#';
    codedbuffer[1] := flag;
    codedbuffer[codedsize+2] := '!';
    ClientSocket.SendBuf(codedbuffer, codedsize+3 );
    Inc(Flag);
    if flag > '9' then flag := '1';
  end else MessageBox(0, '连接服务器失败!', nil, 0 );
end;

procedure TWoool.OnMsg(msg: TMsg; datasize: integer);
begin
  GetTickCount();
  case msg.cmd of
  $1fc:
    begin
      if msg.flag = 1 then
        MessageBox(0, '该帐号已经有人用过了,请选择其他帐号!', '注意!', 0 )
      else
        MessageBox(0, '该帐号可以使用!', '注意!', 0 );
    end;

  $1f8:
    begin
      MessageBox( 0, '帐号注册成功!', '注意!', 0 );
    end;

  $1f9:
    begin
      MessageBox( 0, '帐号注册失败!', '注意!', 0 );
    end;

  $1fa:
    begin
      MessageBox( 0, '密码修改成功!', '注意!', 0 );
    end;

  $1fb:
    begin
      MessageBox( 0, '原始密码有错误!', '注意!', 0 );
    end;

  3004:
    begin
      MessageBox( 0, PChar('取回密码成功'+'['+msg.data+']'+'!'), '注意!', 0 );
    end;

  3005:
    begin
      MessageBox( 0, '取回密码失败!', '注意!', 0 );
    end;  

  else
    MessageBox( 0, '未知消息~~', nil, 0 );
  end;
end;

procedure ClearMemory;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
  end;
end;

procedure TWoool.SetFullScreen(const Value: Boolean);
begin
  FFullScreen := Value;
end;


end.
分享到:
精彩评论 3
小科_mrkj
学分:43 LV2
2010-08-12
沙发
读者朋友:
    您好,按C#的语法把相应的变量、方法重写写一下就行,用到的API函数用DllImport属性重写一下。
wgejydy
学分:0 LV1
TA的每日心情
开心
2023-09-13 18:37:36
2010-08-12
板凳
还请坛主 帮忙改一下  谢谢 麻烦了
小科_mrkj
学分:43 LV2
2010-08-13
地板
读者朋友:
    您好,我们时间也很紧,不能一一给你转换,请自己根据我们的提示自己动手修改一下。
首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照