3

我有一个 Delphi 7 应用程序。我需要能够获得非 unicode 程序的默认 Windows 字符集。我知道 DEFAULT_CHARSET 设置它,但我需要确切知道它是哪个字符集,以便我可以将它与其他字符集进行比较。这可能吗?如何?

谢谢!

4

2 回答 2

5

GetFontData 正在调用GetObjectLogFont.lfCharSet用于确定字符集

用 HFONT 调用的 GetObject将在这里填写LogFont 定义

DEFAULT_CHARSET 设置为基于当前系统区域设置的值。例如,当系统区域设置为英语(美国)时,它被设置为 ANSI_CHARSET。

GetCPInfoEx 与 CP_ACP提供CPINFOEX结构将提供系统默认的Windows ANSI 代码页。

var
 CPInfoEx:TCPInfoEx;
 CD:Cardinal;
 CharsetInfo:TCharSetInfo;
 CSN:String;
begin
 If GetCPInfoEx(CP_ACP,0,CPInfoEx) then
  begin
    CD := CPInfoEx.Codepage;
    if TranslateCharsetInfo(CD,CharsetInfo,TCI_SRCCODEPAGE) then
      begin
      CharsetToIdent(CharsetInfo.ciCharset,CSN);
      Showmessage(CPInfoEx.CodePageName+' - '+IntToStr(CharsetInfo.ciCharset)+' - '+CSN);
     end;
  end;
end;
于 2013-03-31T16:38:19.480 回答
0

这是 Delphi 7 的完整示例。

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  _cpinfoex = record
    MaxCharSize: UINT; { max length (bytes) of a char }
    DefaultChar: array[0..MAX_DEFAULTCHAR - 1] of Byte; { default character }
    LeadByte: array[0..MAX_LEADBYTES - 1] of Byte; { lead byte ranges }
    UnicodeDefaultChar : WCHAR;
    CodePage : UINT;
    CodePageName : array[0..MAX_PATH] of char;
  end;
  TCPInfoEx = _cpinfoex;
  {$EXTERNALSYM CPINFOEX}
  CPINFOEX = _cpinfoex;
  {$EXTERNALSYM GetCPInfoEx}

  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    procedure Check;
  public

  end;

  function GetCPInfoEx(CodePage: UINT; dwFlags : DWORD; var lpCPInfoEx: TCPInfoEx): BOOL; stdcall;
  function GetCPInfoEx; external kernel32 name 'GetCPInfoExA';

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Check;
var
 v_CPInfoEx : TCPInfoEx;
 v_CD : Cardinal;
 v_CharsetInfo : TCharSetInfo;
 v_CSN,
 v_CodePageName,
 v_s, v_info : String;
 v_i : Integer;
begin
 If GetCPInfoEx(CP_ACP, 0, v_CPInfoEx) then
  begin
    v_info := 'CodePage: '+IntToStr(v_CPInfoEx.CodePage)+#13;

    v_CodePageName := '';
    v_i := 0;
    repeat
      v_CodePageName := v_CodePageName + v_CPInfoEx.CodePageName[v_i];
      inc(v_i);
    until v_CPInfoEx.CodePageName[v_i] = #0;
    v_info := v_info + 'CodePageName: '+v_CodePageName+#10#13;

    v_info := v_info + 'MaxCharSize: '+IntToStr(v_CPInfoEx.MaxCharSize)+' bytes.'+#13;

    v_s := '';
    for v_i := 0 to MAX_DEFAULTCHAR - 1 do
      v_s := v_s + IntToStr(v_CPInfoEx.DefaultChar[v_i])+' ';
    v_info := v_info + 'DefaultChar: '+v_s+#13;

    v_s := '';
    for v_i := 0 to MAX_LEADBYTES - 1 - 1 do
      v_s := v_s + IntToStr(v_CPInfoEx.LeadByte[v_i])+' ';
    v_info := v_info + 'LeadByte: '+v_s+#13;

    v_info := v_info + 'UnicodeDefaultChar: '+v_CPInfoEx.UnicodeDefaultChar;

    ShowMessage(v_info);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Check;
end;

end.
于 2016-01-06T12:40:48.813 回答