开发者

Delphi2010: Writing code to assign Caption containing Unicode literal values or load unicode symbols from text file?

How to make a Unicode program in Delphi 2010?

开发者_Go百科

I have English Windows and "Current language for non-Unicode programs" is English too. Static controls look good but if I try to change them (Label.Caption := 'unicode value' or Memo.LoadFromFile(textFilename) ) the text looks like: $^$&%*(#&#.

How to fix it?


Welcome to StackOverflow. Please post your code when you have such a problem. I will explain the most likely sources of the problem like the one you are seeing, but I can't help you fix it if you don't post your code. Also I have to make a lot of assumptions because you've asked me to guess almost everything about your question, which is why it got closed. I hope you give more detail in the future, and we can avoid closed questions.

Let me assume a bunch of things because you haven't given me very much data to go on.

  1. You have used Delphi before, and you know about the fundamental type names like String, Char, and so on.

  2. You may not be aware of the Unicode differences between Delphi 2007 (char=Ansichar/string=Ansistring) and Delphi 2009-or-later (including Delphi 2010 and XE) where Char=UnicodeChar, and String=UnicodeString.

  3. The most common reason you would see garbage (represented in your question as the text looks like $^$&%*(#&#.") is if you have tried to directly manipulate byte-size AnsiCharacter data and coerce it in a wrong way into a UnicodeString.

  4. MJN also noticed, from one of your comments that you are also having trouble with source code that contains unicode characters that was not saved as a UTF8 file... When I try to put Unicode characters into a source file Delphi automatically asks me this question, which I assume you also see, and answer correctly (the correct answer is yes)... But your question doesn't mention this at all, you really should try to update your question to specify the source of your problem.

Delphi2010: Writing code to assign Caption containing Unicode literal values or load unicode symbols from text file?

Here is the right-click file format menu, from which you can change the encoding at any time, the recommended value is UTF8 as shown here:

Delphi2010: Writing code to assign Caption containing Unicode literal values or load unicode symbols from text file?

You should definitely post the affected code that generates the incorrect string values. You should start, not with a giant application that you are trying to port to Unicode Delphi (which is the fourth and largest assumption I'm making here) and rather, start with some small sample code.

here's an example of "badly written code", that happens to still work in Delphi 7, because each character is one byte in size, but this assumption does not travel upwards to 2009 and XE Delphi:

procedure Tform1.TestBad;
var
 x:PAnsiChar;
 s:String;
begin
  x := 'test';
  s := Copy(PChar(x),1,10);
  Self.Caption := s;
end;

here's the same contrived sample code "fixed" (more like not-intentionally broken) so it will at least work in delphi XE:

procedure Tform1.TestLessBad;
var
 x:PAnsiChar;
 s:String;
begin
  x := 'test';
  s := Copy(x,1,10);
  Self.Caption := s;
end;

The use of pointers above is contrived, and unnecessary, except that I am trying to teach with this example.

The first example will create unicode chinese characters in the caption of the form instead of showing the text 'test', because 2 bytes have become a single character because I have intentionally done something BAD to show you one easy way to generate this noise you speak of, by making mistakes in my code.

If you are having trouble with particular unicode code-points, let me suggest you try this notation:

c := Char($21CC);  // this is U+21CC (cool two arrows thingy used in chemistry to indicate a reversible reaction)

Alternatively you will see this, which is almost the same thing:

c := #$21CC; // U+21CC

Notice how you don't need a UTF8 encoded file to store things you write this way.


The comments

Why does Unicode Delphi save not in UTF-8?

and

Delphi 2010 has saved the project not in UTF-8, I have converted to UTF-8 and all have started to work

seem to be related to the encoding of the Delphi project (dpr and/or pas) source code files. If they are set to ANSI for example, characters which do not belong to the current code page might be saved incorrectly.

The IDE (in Delphi 2009 at least) creates new units with file format set to ANSI. For mixed-language source code, UTF-8 (or one of the UCS flavours) has to be activated manually, using the context menu in the text editor (File Format | UTF8).

The IDE (in Delphi 2009) also seems not to have an option to set the default text format to something other than ANSI. (See How can I set the default file format in the Delphi IDE to UTF8?)


The short answer:

Set the file format for your source code to UTF-8.

(The Delphi 2009 IDE does not use Unicode by default, I guess this is still the same in Delphi 2010)


If you migrated your project from an old Delphi version, check the font used in your dialogs. Not all fonts support all unicode characters.

I had a problem, which was apparently similar to yours: I had correct unicode strings in the debugger but in the application some of the special characters appeared as black squares. This was with an old program migrated from Delphi 6 where some captions where set through code. On new forms everything was ok (here the captions were set in the dialog editor, but this isn't actually relevant).

The problem was the font of all migrated forms. Delphi 6 used to use "MS Sans Serif" as font. But this font is missing most of the unicode characters. Switching to "Tahoma" solved my problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜