来先看原代码:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, AxCtrls, OleCtrls, DbOleCtl, RichTextLib_TLB;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    RichTextBox1: TRichTextBox;
    RichTextBox2: TRichTextBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
    RichTextBox1.Text:='可http://';
    RichTextBox1.SelStart := 5;
    RichTextBox1.SelLength := 10;
    RichTextBox1.SelColor := clBlue;
    RichTextBox1.SelUnderline := fsUnderline;end;procedure TForm1.Button2Click(Sender: TObject);
begin
    RichTextBox2.Text:='12http://';
    RichTextBox2.SelStart := 5;
    RichTextBox2.SelLength := 10;
    RichTextBox2.SelColor := clBlue;
    RichTextBox2.SelUnderline := fsUnderline;
end;end.
我界面上有两个BUTTON和两个RichTextBox控件(Windows自带的OCX控件)
分别做了一个测试.第一个BUTTON的CLICK事件中.
得到的结果是:(://)为以蓝色方式显示。主要就是有一个汉字.第二个BUTTON的CLICK事件中.得到的结果是:(p://)为以蓝色方式显示。想问的问题就是为什么有汉字的会少显示一个字符.一个汉字=两个英文字符啊.
我想了N次都想不明白。希望各位高手进来帮我解答。做这个测试的目的。
我是想在richtextbox中实现超连接方式显示文本的.如果把richtextbox换成richedit就能通过.
但是richtextbox可以贴图片,并且可以保存的内容超过64K。但是RICHEDIT就没有以上功能啊。

解决方案 »

  1.   

    第一个BUTTON的CLICK事件中.
    得到的结果是:(://)为以蓝色方式显示。主要就是有一个汉字.第二个BUTTON的CLICK事件中.得到的结果是:(p://)为以蓝色方式显示。如果这样那汉字不还是算一个字符的?
      

  2.   

    在Delphi中,通过什么函数取'我们123'此字符串的长度为5,而Length('我们123')=7编一个下面的函数就可以实现你的要求:
    Function TForm1.GetStrLength(const S: string): integer;
    var
    p: PChar;
    Len: integer;
    begin
    Len:= 0;
    p := PChar(S);
    while P^ <> #0 do
    begin
    if P^ in LeadBytes then Inc(p);
    Inc(p);
    Inc(Len);
    end;
    result:= Len;
    end; 
      

  3.   

    在delphi中操作双字节编码的字符集的字符串时需要注意:除了英语的二十六个字母及键盘上的那些符号是ASCII编码,采用的单字节(1~255)编码方式,其它字符集采用的是双字节编码,而delphi中的很多对象属性是string类型的,虽然用于显示及赋值操作时含双字节字符的字符串没有什么问题,但如果要取该字符串其中一部分内容时就会有问题了。建议采用以下方法来解决:定义一个局部变量为widestring类型,然后将要操作的内容赋给该变量再进行操作。这样就不会出现问题了。length等函数的运行结果也才是我们想要的结果。
      

  4.   


    改变RxRiched.Pas中
     RxRichEditString的Get方法中最后一句为
      SetString(Result, Text, SizeOf(Text));
      

  5.   

    对不起,看错了
    不过RxRichEdit的类似问提是这样解决的
      

  6.   

    gegangqiao,menliwxj(有缘) 
    谢谢你们测试通过.现在发现我输入了汉字。
    写上HTTP:// 正常显示了。
    不过显示后就不能输入汉字了。
    请看代码。
    谢谢。还有一点就是比如 "不可能啊 http://"
    这个居然不显示超连接.看一下代码有没有错的地方。
    答对了再给分!
    谢谢!RichLearn是RichTextBox
    procedure TMainForm.RichLearnChange(Sender: TObject);
    var
      S: widestring;
      T: string;
      I, J, L: Integer;
      vSelStart: Integer;
      vSelLength: Integer;
    const vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.+!*''(),{}|\^~[]`<>#%|<>;/?:@&=';
      begin
      RtfTxtModi:=True;
      SaveMenu.Enabled:=RtfTxtModi;
      SaveTB.Enabled:=RtfTxtModi;
      RichLearn.Perform(EM_HIDESELECTION, Longint(True), 0);  S := RichLearn.Text;
      L := Length(S);
      J := 1;
      vSelStart := RichLearn.SelStart;
      vSelLength := RichLearn.SelLength;
      RichLearn.SelStart := 0;
      RichLearn.SelLength := L;
      RichLearn.SelColor := clWindowText;  while J <= L do begin
        T := '';
        for I := J to L do
          if Pos(Char(S[I]), vChars)>0   then
            T := T + S[I]
          else Break;
        if T <> '' then begin
          I := Pos('MAILTO:', UpperCase(T));
          if I <= 0 then I := Pos('HTTP://', UpperCase(T));
          if I <= 0 then I := Pos('FTP://', UpperCase(T));
          if I <= 0 then I := Pos('WWW.', UpperCase(T));
          if I = 1 then begin
            RichLearn.SelStart := J - 1;
            RichLearn.SelLength :=Length(T);
            RichLearn.SelColor := clBlue;
            RichLearn.SelUnderline := fsUnderline;
          end;
          Inc(J, Length(T));
        end;
        Inc(J);
      end;  RichLearn.SelStart := vSelStart;
      RichLearn.SelLength := vSelLength;
      RichLearn.Perform(EM_HIDESELECTION, Longint(False), 0);end;
      

  7.   

    richtextbox是WINDOWS自带的控件啊!
      

  8.   

    在WINDOWS,不知是98还是2000开始,汉字已经是用一个字节来表示了也就是说 一个汉字=一个英文字符好象在控制面可以改,不过我忘记了,找到再告诉你
      

  9.   

    还有一点就是比如 "不可能啊 http://"
    这个居然不显示超连接.
    对于这一点。
    我测试过了。
    “啊”
    var
      S: widestring;
    begin
      S:='啊';
      IF Char(s[1])='-' then
        Showmessage('ok');
      他已经不是汉字了啊。所以程序要出错。
      
      

  10.   

    现在贴出正确笨方法代码。
    以提供给要学习的朋友们!
    如果有更好的方法请给我发短消息。
    谢谢:)
    //程序功能,在RichTextBOX中加入超连接的方法//
    //这里只将超连接显示出来,程序未完。还有打开超连接地址等//
    //也就是还有ONMOUSEMOVE和ONMOUSEDOWN的事件还有没写//
    //因为遇到了问题。//
    //问题贴子在://
    //http://expert.csdn.net/Expert/topic/2050/2050197.xml?temp=.7418177//
    //希望大家都来观注,并解决本问题。谢谢!!//unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      AxCtrls, OleCtrls, DbOleCtl, RichTextLib_TLB, StdCtrls,RichEdit;type
      TForm1 = class(TForm)
        Button1: TButton;
        RichLearn: TRichTextBox;
        procedure RichLearnChange(Sender: TObject);
        procedure RichLearnMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.RichLearnChange(Sender: TObject);
    var
      S: string;
      T: string;
      I, J, L,P: Integer;
      vSelStart: Integer;
      vSelLength: Integer;
      HZCount: integer;
    begin
      RichLearn.Perform(EM_HIDESELECTION, Longint(True), 0);  S := RichLearn.Text;
      L := Length(S);
      J := 1;
      P :=0 ;
      vSelStart := RichLearn.SelStart;
      vSelLength := RichLearn.SelLength;
      RichLearn.SelStart := 0;
      RichLearn.SelLength := L;
      RichLearn.SelColor := clWindowText;  while J <= L do begin
        T := '';
        for I := J to L do begin
          IF (Char(S[I]) in LeadBytes) then    P:=P+1;
            IF Char(S[I]) in [#33..#172] then
              T := T + S[I]
            else
             Break;
         End;
        if T <> '' then begin
          I := Pos('MAILTO:', UpperCase(T));
          if I <= 0 then I := Pos('HTTP://', UpperCase(T));
          if I <= 0 then I := Pos('FTP://', UpperCase(T));
          if I <= 0 then I := Pos('WWW.', UpperCase(T));
          if I = 1 then begin
            HZCount:=Strtoint(FloattoStr(p/2));
            RichLearn.SelStart := J-1-HZCount;
            RichLearn.SelLength :=Length(T);
            RichLearn.SelColor := clBlue;
            RichLearn.SelUnderline := fsUnderline;
          end;
          Inc(J, Length(T));
        end;
        Inc(J);
      end;  RichLearn.SelStart := vSelStart;
      RichLearn.SelLength := vSelLength;
      RichLearn.Perform(EM_HIDESELECTION, Longint(False), 0);
    End;
      

  11.   

    你在 FOR 的循环中有改变基本量的值不知道说的对不对:)