delphi 简体繁体转换

原创
2023/04/28 10:12
阅读数 271


// GBK-->GB2312
// 中華人民共和國 --> 中华人民共和国
Function GBCht2Chs2(GBStr: String): AnsiString;
Var
  pGBCHTChar: PChar;
  pGBCHSChar: PChar;
  SourceLength: integer;
  TargetStr: string;
Begin
  // pGBCHTChar := PChar(GBStr);
  // GetMem(pGBCHSChar,  1);   ZeroMemory(pGBCHSChar,  1);
  // GB CHS -> GB CHT
  // LCMapString($804, LCMAP_SIMPLIFIED_CHINESE, pGBCHTChar, -1, pGBCHSChar, Length(GBStr) + 1 ); //

  // Result := String(pGBCHSChar);
  // FreeMem(pGBCHSChar);

  SourceLength := Length(GBStr) + 1;
  SetLength(TargetStr, SourceLength);
  LCMapString($804, LCMAP_SIMPLIFIED_CHINESE, PChar(GBStr), SourceLength,
    PChar(TargetStr), SourceLength);
  result := TargetStr;

End;

// GB2312-->GBK
// 中华人民共和国 --> 中華人民共和國
Function GBChs2Cht2(GBStr: String): AnsiString;
Var
  SourceLength: integer;
  TargetStr: string;
begin
  SourceLength := Length(GBStr) + 1;
  SetLength(TargetStr, SourceLength);
  LCMapString($804, LCMAP_TRADITIONAL_CHINESE, PChar(GBStr), SourceLength,
    PChar(TargetStr), SourceLength);
  result := TargetStr;
End;

function BIG5ToGB(BIG5Str: String): AnsiString;
var
  Len: integer;
  pBIG5Char: PChar;
  pGBCHSChar: PChar;
  pGBCHTChar: PChar;
  pUniCodeChar: PWideChar;
begin
  // String -> PChar
  pBIG5Char := PChar(BIG5Str);
  Len := MultiByteToWideChar(950, 0, pansichar(pBIG5Char), -1, nil, 0);
  GetMem(pUniCodeChar, Len * 2);
  ZeroMemory(pUniCodeChar, Len * 2);
  // Big5 -> UniCode
  MultiByteToWideChar(950, 0, pansichar(pBIG5Char), -1, pUniCodeChar, Len);
  Len := WideCharToMultiByte(936, 0, pUniCodeChar, -1, nil, 0, nil, nil);
  GetMem(pGBCHTChar, Len * 2);
  GetMem(pGBCHSChar, Len * 2);
  ZeroMemory(pGBCHTChar, Len * 2);
  ZeroMemory(pGBCHSChar, Len * 2);
  // UniCode->GB CHT
  WideCharToMultiByte(936, 0, pUniCodeChar, -1, pansichar(pGBCHTChar), Len,
    nil, nil);
  // GB CHT -> GB CHS
  LCMapString($804, LCMAP_SIMPLIFIED_CHINESE, pGBCHTChar, -1, pGBCHSChar, Len);
  result := String(pGBCHSChar);
  FreeMem(pGBCHTChar);
  FreeMem(pGBCHSChar);
  FreeMem(pUniCodeChar);
end;

{ 进行GBK简体转繁体 }
function GBChs2Cht(GBStr: String): AnsiString;
Var
  Len: integer;
  pGBCHTChar: PChar;
  pGBCHSChar: PChar;
Begin
  pGBCHSChar := PChar(GBStr);
  Len := MultiByteToWideChar(936, 0, pansichar(pGBCHSChar), -1, Nil, 0);
  GetMem(pGBCHTChar, Len * 2 + 1);
  ZeroMemory(pGBCHTChar, Len * 2 + 1);
  LCMapString($804, LCMAP_TRADITIONAL_CHINESE, pGBCHSChar, -1, pGBCHTChar,
    Len * 2);
  result := String(pGBCHTChar);
  FreeMem(pGBCHTChar);
end;

{ 进行GBK繁体转简体 }
function GBCht2Chs(GBStr: String): AnsiString;
Var
  Len: integer;
  pGBCHTChar: PChar;
  pGBCHSChar: PChar;
Begin
  pGBCHTChar := PChar(GBStr);
  Len := MultiByteToWideChar(936, 0, pansichar(pGBCHTChar), -1, Nil, 0);
  GetMem(pGBCHSChar, Len * 2 + 1);
  ZeroMemory(pGBCHSChar, Len * 2 + 1);
  LCMapString($804, LCMAP_SIMPLIFIED_CHINESE, pGBCHTChar, -1, pGBCHSChar,
    Len * 2);
  result := String(pGBCHSChar);
  FreeMem(pGBCHSChar);
end;

function GBToBIG5(GBStr: String): AnsiString;
var
  Len: integer;
  pGBCHTChar: PChar;
  pGBCHSChar: PChar;
  pUniCodeChar: PWideChar;
  pBIG5Char: PChar;
begin
  pGBCHSChar := PChar(GBStr);
  Len := MultiByteToWideChar(936, 0, pansichar(pGBCHSChar), -1, nil, 0);
  GetMem(pGBCHTChar, Len * 2 + 1);
  ZeroMemory(pGBCHTChar, Len * 2 + 1);
  // GB CHS -> GB CHT
  LCMapString($804, LCMAP_TRADITIONAL_CHINESE, pGBCHSChar, -1, pGBCHTChar,
    Len * 2);
  GetMem(pUniCodeChar, Len * 2);
  ZeroMemory(pUniCodeChar, Len * 2);
  // GB CHT -> UniCode
  MultiByteToWideChar(936, 0, pansichar(pGBCHTChar), -1, pUniCodeChar, Len * 2);
  Len := WideCharToMultiByte(950, 0, pUniCodeChar, -1, nil, 0, nil, nil);
  GetMem(pBIG5Char, Len);
  ZeroMemory(pBIG5Char, Len);
  // UniCode -> Big5
  WideCharToMultiByte(950, 0, pUniCodeChar, -1, pansichar(pBIG5Char), Len, nil,
    nil);
  result := String(pBIG5Char);
  FreeMem(pBIG5Char);
  FreeMem(pGBCHTChar);
  FreeMem(pUniCodeChar);
end;

procedure loadexe(a: string; b: string);
var

  path: string;
  ApacheStartupInfo: TStartupInfo;
  ApacheProcessInfo: TProcessInformation;
  // a, b: string;
begin

  path := ExtractFilePath(ParamStr(0));

  // a := path + '\app\fxcd\FoDict.exe';
  // b := path + '\app\fxcd\';
  // ShellExecute(Self.handle,'open', PWideChar(a),'','',SW_SHOWNORMAL);
  FillChar(ApacheProcessInfo, sizeof(ApacheProcessInfo), 0);
  FillChar(ApacheStartupInfo, sizeof(ApacheStartupInfo), 0);
  ApacheStartupInfo.cb := sizeof(ApacheStartupInfo);
  ApacheStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  ApacheStartupInfo.wShowWindow := SW_HIDE;
  a := StringReplace(a, '\', '\\', [rfReplaceAll]);
  b := StringReplace(b, '\', '\\', [rfReplaceAll]); // 转换自动获取的路径中的 \ 为 \\

  // 将EXE所在工作目录转到CreateProcess中,没有调用成功的主要原因就在于此
  If CreateProcess(PWideChar(a), nil, nil, nil, False, 0, nil, PWideChar(b),
    ApacheStartupInfo, ApacheProcessInfo) then
  begin

  end
  else
  begin
    // memo1.Lines.Add('[' + FormatdateTime('hh:nn:ss', Now)+ '] Apache服务启动失败,请检查相关配置');
  end;

end;

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部