Strange regional character / date-time with Delphi7 program under Windows7

My Delphi7 program is in non-English and makes extensive use of some Win1250 ANSI characters. (Aáíóőű)

I noticed that under the English language Windows XP / Vista / 7, many characters with an accent appear without an accent (i.e.: o instead of ő, u instead of ű) in the captions of the form and inscriptions.

There are no problems on localized Windows XP / Vista systems. But on some Windows 7 systems (maybe only on 64-bit versions, I'm not sure), even if the Windows instance is localized, accented characters do not display normally.

What I also noticed is that on these systems, DateToStr will output 11/17/2010 instead of 2010.11.17 - this is the standard localized format. The funny thing is that I checked it in the regional settings, and the default date format is set to yyyy.mm.dd - why does DateToStr return dd / mm / yyyy to me instead?

I noticed the same behavior with currencies ("," that appear as a decimal separator, not ".", Etc.).

Any ideas?

Thanks!

+4
source share
4 answers

I noticed that in English the Windows XP / Vista / 7 system appears a lot of accented characters without an accent (i.e.: o instead of ő, u instead of ű) in the form and label Titles.

Delphi 7 applications are not Unicode (unless you use components such as TNT components), so you need to check the active CodePage for non-Unicode applications on Windows. In Win7 go to the control panel | Region and language | Administrative | Change the system language and change the default ANSI CodePage for Windows.

What I also noticed is that these DateToStr systems will output 11/17/2010 instead of 2010/11/17 - which is the standard localized format. The funny thing is that I checked it as part of the regional setting and the default date format is set to yyyy.mm.dd - why does DateToStr give me back dd / mm / yyyy instead?

This is similar to the following report: http://social.technet.microsoft.com/Forums/en-US/w7itprogeneral/thread/b4f90f07-206c-494b-8d14-ee17bfa689e0

This seems to be a bug in Windows. I had the same problem in one of my projects written for a Slovenian client. I told the client to set the current date and time settings to something else, save the changes, then go back to the Date / Time settings and return them to the desired format. This will fix the problem.

Also in the above link, a programming workaround is mentioned:

From S.B. Christensen:

Hi Tim

I just had the same release as you (also doing Delphi development).

If you want your users not to do a workaround, use the following block as the first in your project:

Win7 unit

interface

uses SysUtils, Windows;

implementation

initialization SetThreadLocale (LOCALE_USER_DEFAULT); GetFormatSettings; end.

I myself have not tested the code because the previous workaround worked perfectly for me.

+4
source

We also observed this with Delphi applications. When we examined the Windows API calls, which actually returned what turned out to be incorrect values.

Just try changing the language and locale under the regional settings to another, and then return to the one you are using. It works every time for us.

EDIT: → It’s just worth mentioning that the word MS used the wrong language with us, like when we asked him to insert the current date and time when he used the wrong format.

+2
source

You can set a default code page for applications that do not support Unicode in regional versions of Windows.

In Delphi, you can read (but not change) this parameter using the Windows API GetACP .

http://msdn.microsoft.com/en-us/library/ms905215.aspx

Versions of Delphi prior to Delphi 2009 will always use this ANSI code page to display the lines that you use to set label labels, etc. (at least for default components)

The locale settings for some Delphi functions, such as FloatToStr , can take a second parameter of type TFormatSettings to control the format used for the conversion.

http://docwiki.embarcadero.com/VCL/en/SysUtils.TFormatSettings

This also applies to newer versions of Delphi.

A set of standard TFormatSettings parameters is installed in the SysUtils module, which at startup is initialized with the standard Windows default values, see the Variables section of the SysUtils documentation:

http://docwiki.embarcadero.com/VCL/en/SysUtils

0
source

The problem with date formatting in Delphi is that Delphi assumes that the date separator character is only one character, for example:

  • "/" (e.g. English - United States)
  • "." (e.g. French - Canada)

This does not work in locales that use more than one character for a date separator:

  • ". " (e.g. Slovak - Slovakia)

In this case, Delphi when prompted by Windows for a date separator character:

 GetLocaleInfo(LOCALE_SDATE, ...) 

crashes and instead hardcodes the use of / .

To correctly convert a date to a string in Delphi, you must use the Windows API function designed to convert the date to a string, GetDateFormat :

 function DateToStrW(const Value: TDateTime; const Locale: LCID=LOCALE_USER_DEFAULT): WideString; var pf: PWideChar; cch: Integer; TheDate: SYSTEMTIME; DateStr: WideString; begin //Code is released into the public domain. No attribution required. SysUtils.DateTimeToSystemTime(Value, TheDate); cch := Windows.GetDateFormatW(Locale, DATE_SHORTDATE, @TheDate, nil, nil, 0); if cch = 0 then RaiseLastWin32Error; SetLength(DateStr, cch); cch := Windows.GetDateFormatW(Locale, DATE_SHORTDATE, @TheDate, nil, PWideChar(DateStr), Length(DateStr)); if (cch = 0) then RaiseLastWin32Error; SetLength(DateStr, cch-1); //they include the null terminator /facepalm Result := DateStr; end; 
0
source

All Articles