How to shield Chinese Character Input VCL Component Development and Application
Http://www.delphi2007.net/DelphiVCL/html/delphi_20061223175511162.html
Is there a way to make tedit unable to input Chinese characters or filter the input method when this tedit is selected.
// Including and pasting will also be filtered out
Procedure tform1.edit1change (Sender: tobject );
VaR
S: widestring;
I, J: integer;
Vselstart: integer;
Begin
Vselstart: = tedit (sender). selstart;
S: = tedit (sender). text;
J: = 0;
For I: = length (s) downto 1 do
If length (string (s [I])> = 2 then
Begin
If vselstart <= length (string (copy (s, 1, I) Then Inc (J, 2 );
Delete (S, I, 1 );
End;
Tedit (sender). Text: = s;
Tedit (sender). selstart: = vselstart-J;
End;
Thank you, zswang.
I learned delphie only a few days ago because of my work. I used to use C. I didn't quite understand tedit (sender.
Is it a temporary object?>? Or is the tedit of the trigger event. Do I not need to change the function (such as delete) that declares s as wstring to support wstring?
In fact, I think so. c. if the editchange event is triggered, you can determine whether the newly added character is a byte, because the numbers and letters are both one byte. (What do you think)
sender is the trigger of the event
that is, the tedit of the triggered event
tedit (sender) is equivalent to the (tedit *) in C *) sender
is actually vselstart: = edit1.selstart;
this method is more common and not limited by the Control name
length (), delete () the string type in the parameter is determined.
for I: = length (s) downto 1 do
If s [I]> #255 then // this judgment is more concise
begin
If vselstart <= length (string (copy (S, 1, I) Then Inc (J, 2);
Delete (S, I, 1);
end;
It is too troublesome to determine whether your input box is a Chinese character. It can be used to determine the string type, the placeholder of string and widestring. One is in the unit of 1 byte, 1 is measured in 2 bytes.
Function checkinputstrischinese (var s: string): Boolean;
VaR
STR: widestring;
Begin
STR: = s;
If length (STR) = 2 * length (s) then
Result: = false // No Chinese Characters
Else result: = true; // yes!
End;
If the length value obtained by string and widestring are the same, you can determine whether there are full-angle characters in the text. There is no guarantee that there are Chinese characters (full-angle ,. Japanese, Korean ).
However, the range of the ASCII code for each word check can be used to determine whether it is a man, but it is slightly less efficient.
Mark
Thanks to zswang, and thanks to the others who think zswang is pretty good.
In winxkm, if length (STR) = 2 * length (s) then should not meet my requirements. At least paste it.
I close the post.
If you don't have any new ideas tomorrow, I will close them! Thank you.
Type
Tform1 = Class (tform)
Edit1: tedit;
Procedure formcreate (Sender: tobject );
Private
{Private Declarations}
Oldwndproc: twndmethod;
Procedure editwndproc (VAR message: tmessage );
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.editwndproc (VAR message: tmessage );
VaR
Ch: word;
Begin
If (message. MSG = wm_paste) or (message. MSG = wm_cut)
Or (message. MSG = wm_char) and (message. wparam> 127) then
Exit;
Oldwndproc (Message );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Oldwndproc: = edit1.windowproc;
Edit1.windowproc: = editwndproc;
End;
Type
Tform1 = Class (tform)
Edit1: tedit;
Procedure formcreate (Sender: tobject );
Private
{Private Declarations}
Oldwndproc: twndmethod;
Procedure editwndproc (VAR message: tmessage );
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.editwndproc (VAR message: tmessage );
Begin
If (message. MSG = wm_paste) or (message. MSG = wm_cut)
Or (message. MSG = wm_char) and (message. wparam> 127) then
Exit;
Oldwndproc (Message );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Oldwndproc: = edit1.windowproc;
Edit1.windowproc: = editwndproc;
End;
Really good. maozefa (a fa) subclass of the window process, I use C to do Window Programming.
Not enough. Please feel wronged.