打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to write Unicode WIN32 applications with Delphi

How to write Unicode WIN32 applications with Delphi

This document describes the basic steps that you should perform if you want to change you application fully Unicode enabled.

You do not have to change you application to Unicode application if you plan to localize it to Asian languages. A standard Ansi Delphi application can be localized to Asian languages just fine. However if you want to run you application in Japanese on Western Windows it has to be a Unicode application. Otherwise all strings will show incorrectly as mojibake strings.

If you want to localize your application to a language that has not code page support such as Hindi you have to make you application Unicode enabled.

Before you start changing you application from Ansi to Unicode carefully think if you or your customers have a need to run your application on different languages than their Windows or you need to localize for language that has not code page support. Remember that most western languages use the same code page and are compatible to each other. This means that you can run German application on French or English Windows and vice versa.

Use inherited forms

When you write properly internationalized code you have to change the default behaviors of each form and frame. This is why you better derive an abstract form from TForm and derived all your forms from this form. Do same form frame. This makes it easy to initialize the form in the constructor of the base form.

Use an Unicode library

Current VCL components do not support Unicode. The reason is that every string property is type as String and not WideString. In order to make your application fully Unicode enabled you can not use the standard VCL components such as TLabel, TEdit, etc. Instead you have to use Unicode enabled controls. This will be changed in Delphi 2008. Currently there are two Unicode enabled component libraries. They are:

Library URL
TMS Unicode Component Pack http://www.tmssoftware.com/tmsuni.htm
LMD ElPack http://www.lmdinnovative.com/products/lmdelpack/

Both component libraries provide full set of Unicode enabled components. TMS components names and properties are almost identical to the original VCL components so it is easy to convert existing Ansi forms form Unicode forms. ElPack component names are a bit different so converting is little bit harder.

Convert you existing forms

In many cases you have an existing project that you want to convert to Unicode project. At first replacing all existing components may seem very complicated but it is actually very easy. This how it is done.

  1. Install the Unicode component library to Delphi's component palette.
  2. Open the form that you want to convert
  3. Change to text view. Right click the form and choose View as Text.
  4. Change the component type names from original VCL names to Unicode names. For example if you use TMS components change TLabel to TTntLabel, TEdit to TTntEdit and TButton to TTntButton.
  5. Change back to form view. Right click the form and choose View as Form.
  6. Move form a bit to make it dirty and save it. Delphi will show an error message telling the component type names in the form and source do not match.

    Press Yes to convert the type name in the source to match the form's type name. This error message is show once for each component you changed.

If you use TMS controls change the base form of each form from TForm to TTntForm. If you use ElPack add TElFormCaption to each form.

Change you internal string data from String to WideString

Keep all internal data in Unicode. This means use WideString instead of String whenever you handle strings.

Keep in mind that VCL automatically converts WideString to AnsiString. This will make some of all data to be corrupted if it contains above ASCII character and the system code page of your Windows does not match the default code page of the string data.

Sometimes you have to use Ansi string. Use AnsiString instead of String. This makes it easier to pinpoint the placed that have already been checked. Whenever you see String variable you have to decided if you change this to WideString or AnsiString.

Unicode file names

Handle all file names as WideString. Use the dialog components and system functions of your Unicode library instead of the standard VCL components and functions. For example if you had

if FileExists(fileName) thenbegin ...end;

Change it to (if you use TMS)

if WideFileExists(fileName) thenbegin ...end;

Use Unicode version WIN32 API

Whenever you call WIN32 API check if there is a Unicode version of the API. The Unicode version has the same name except its has W at the end of the function name. For example if you has the following code

function IsFileReadOnly(const fileName: String): Boolean;begin Result := FileExists(fileName) and ((GetFileAttributes(PChar(fileName)) and FILE_ATTRIBUTE_READONLY) <> 0);end;

Change it to

function IsFileReadOnly(const fileName: WideString): Boolean;begin Result := WideFileExists(fileName) and ((GetFileAttributesW(PWideChar(fileName)) and FILE_ATTRIBUTE_READONLY) <> 0);end;

Following these simple steps you can easily change you existing Ansi application into Unicode application.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用Delphi自带的TDockTabSet组件实现停靠功能(Jeremy North) ...
Delphi中安装和使用KOL和MCK
C#跨线程更改Form中控件的属性
文件上传自写代码实现servlet3.0,part
Delphi XE7实现的登录窗体的正确用法示例
delphi的UTF8相关函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服