Delphi - Get Windows' default non-unicode character set

瘦欲@ 提交于 2021-02-07 20:11:04

问题


I have a Delphi 7 application. I need to be able to get the default Windows character set for non-unicode programs. I know DEFAULT_CHARSET sets it, but I need to know exactly which charset it is, so that I could compare it to other character sets. Is this possible and how?

Thanks!


回答1:


GetFontData is calling GetObject and using LogFont.lfCharSet to determine the charset

GetObject called with HFONT will fill LogFont Definition here is

DEFAULT_CHARSET is set to a value based on the current system locale. For example, when the system locale is English (United States), it is set as ANSI_CHARSET.

GetCPInfoEx with CP_ACP delivering a CPINFOEX structure will deliver the system default Windows ANSI code page.

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;



回答2:


Here is full example for 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.


来源:https://stackoverflow.com/questions/15729116/delphi-get-windows-default-non-unicode-character-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!