字符串转换到指定格式的宽字符 - 回复 "厨师" 的问题

时间:2022-02-16 03:58:06
问题来源: http://www.cnblogs.com/del/archive/2008/12/13/1353193.html#1400641

本例效果图:

字符串转换到指定格式的宽字符 - 回复 "厨师" 的问题

代码文件:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Memo1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
  pcw: PWideChar;
  cw: WideChar;
  i: Integer;
begin
  Memo1.Clear;

  str := Edit1.Text;
  pcw := PWideChar(str);

  for i := 0 to StrLen(pcw) - 1 do
  begin
    cw := (pcw+i)^;
    Memo1.Lines.Add(Format('$%.4x', [Ord(cw)]));
    //Memo1.Lines.Add(Format('$%.4x', [Ord(str[i+1])])); {也可以用这句替换上面两行}
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Clear;
  Memo1.Align := alLeft;
  Memo1.ScrollBars := ssVertical;
end;

procedure TForm1.Memo1Click(Sender: TObject);
var
  str: string;
begin
  str := Memo1.Lines[Memo1.CaretPos.Y];
  Text := WideChar(StrToIntDef(str, 0));
end;

end.

 
 
 
窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 190
  ClientWidth = 260
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 8
    Top = 8
    Width = 89
    Height = 185
    Lines.Strings = (
      'Memo1')
    TabOrder = 0
    OnClick = Memo1Click
  end
  object Button1: TButton
    Left = 136
    Top = 120
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 95
    Top = 16
    Width = 157
    Height = 21
    TabOrder = 2
    Text = 'Edit1'
  end
end