Recent

Author Topic: Form Caption encoding for russian characters with CallWindowProc  (Read 3059 times)

mortalis

  • Newbie
  • Posts: 3
Form Caption encoding for russian characters with CallWindowProc
« on: December 18, 2017, 04:53:32 pm »
Hi.
Have a problem with setting a form caption with russian characters.
The sample code for a Form application:

Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. function MyWndProc(hWnd: HWND; uiMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  4. begin
  5.   Result := CallWindowProc(OldWProc, hWnd, uiMsg, wParam, lParam);
  6. end;
  7.  
  8. procedure SetMyWndProc(Handle : HWND);
  9. begin
  10.   OldWProc := WNDPROC(SetWindowLongPtr(Handle, GWLP_WNDPROC, LONG_PTR(@MyWndProc)));
  11. end;
  12.  
  13. procedure TForm1.Button1Click(Sender: TObject);
  14. var s:string;
  15. begin
  16.   SetMyWndProc(Handle);
  17.   s := 'абв';
  18.   Form1.Caption := s;
  19. end;
  20.  

On the button click I set a window procedure to handle some Win32 messages. Then I change the form Caption to a string containing russian characters. But at the end it shows '???' in the title bar of the form.
If I comment the SetMyWndProc() part the title updates without problems.

I guess it's the same for other multibyte Unicode characters (chinese for example). Other diacritic characters (like áéíó) are showed fine.

If I dump the caption after I change it (using the "Showing codepoints with UTF8CharacterToUnicode" from the UTF8 strings and characters page, http://wiki.freepascal.org/UTF8_strings_and_characters) it gives me Unicode multibyte values for those 3 characters:

1072
1073
1074

If I set that string to a TLabel or TEdit it renders ok.

So could anyone give me an advice on why it could happen? Maybe there's another param for CallWindowProc() or SetWindowLongPtr() to set proper encoding. Or I should convert the caption string before assignment.

Thanks.
« Last Edit: December 18, 2017, 06:17:07 pm by mortalis »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Form Caption encoding for russian characters with CallWindowProc
« Reply #1 on: December 18, 2017, 05:08:32 pm »
CallWindowProcW. I have to check but I think CallWindowProc is still aliased to CallWindowProcA.
Maybe {$mode delphiunicode} will help?

And, yes, I checked, the W goes through all unicode functions EVEN IF there is no obvious string involved here.
« Last Edit: December 18, 2017, 05:26:32 pm by Thaddy »
Specialize a type, not a var.

mortalis

  • Newbie
  • Posts: 3
Re: Form Caption encoding for russian characters with CallWindowProc
« Reply #2 on: December 18, 2017, 06:29:33 pm »
Using CallWindowProcW() gives me an asian character and a '?' (IMG1).

And the {$mode delphiunicode} directive gives me IMG2 (label and edit showed the string well before and the title seems to be converted to some other encoding). The default directive created by Lazarus is {$mode objfpc}{$H+}.

Anyway it's a little better than plain ?'s.
Maybe it lacks some encoding convertion, from/to Unicode/UTF8/CP1251/...

balazsszekely

  • Guest
Re: Form Caption encoding for russian characters with CallWindowProc
« Reply #3 on: December 18, 2017, 06:36:42 pm »
You should also change SetWindowLongPtr to SetWindowLongPtrW.

mortalis

  • Newbie
  • Posts: 3
Re: Form Caption encoding for russian characters with CallWindowProc
« Reply #4 on: December 18, 2017, 07:00:33 pm »
That's right.
So CallWindowProcW and SetWindowLongPtrW.
The problem solved.
Thanks a lot.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Form Caption encoding for russian characters with CallWindowProc
« Reply #5 on: December 19, 2017, 03:03:17 am »
So CallWindowProcW and SetWindowLongPtrW.
The problem solved.
It turns out also the text in wiki too it is required to update.

 

TinyPortal © 2005-2018