我现在有个难题,就是说我做了一个滚动条,用LABEL做的,现在我要实现当我的鼠标点在Label的任何一个位置,我可以得到我点中这部分的词。不知道如何实现,请高手帮忙。重谢!11

解决方案 »

  1.   

    一种比较简单的方法(需要Label的字体为规则字体,只有英文字符,即以空格非a..z,A..Z,0..9,-,'为单词分隔符),那可以使用:
    var 
    tx:TPoint;
    iPos:Integer;
    iStart:Integer;
    iEnd:Integer;
    sWord:String;    GetCursorPos(tx);
        tx:=Label1.ScreenToClient(tx);
        iPos:=label1.Canvas.TextWidth(' ');
        iPos:=tx.x div iPos;
        if tx.X mod label1.Canvas.TextWidth(' ')>0 then inc(iPos);
        iStart:=BackwardSearchWord(mid(Copy(Label1.Caption,1,iPos));//搜索单词的第一字母位置
        iEnd:=iPos+ForwardSearchWord(Copy(Label1.Caption,iPos,Length(Label1.Caption)))-1;//搜索最后字母位置
        sWord=Copy(Label1.Caption,iStart,iEnd-iStart);
      

  2.   

    完整代码:
    Label1.Font.Name:='宋体';
    =============================
    function BackwardSearchWord(const SourceString:String):Integer;
    var
    i:integer;
    begin
       i:=Length(SourceString);
       Result:=i;
       while i>0 do
       begin
          if SourceString[i] in ['0'..'9','A'..'Z','a'..'z','-',''''] then
             Result:=i
          else
             exit;
          dec(i);
       end;
    end;
    function ForwardSearchWord(const SourceString:String):Integer;
    var
    i:integer;
    begin
       Result:=1;
       for i:=1 to Length(SourceString) do
       begin
          if SourceString[i] in ['0'..'9','A'..'Z','a'..'z','-',''''] then
             Result:=i
          else
             exit;
       end;
    end;
    {$R *.dfm}procedure TForm1.Label1Click(Sender: TObject);
    var
      tx:TPoint;
      iPos:Integer;
      iStart:Integer;
      iEnd:Integer;
    begin
        //TPoint tp,tx;
        GetCursorPos(tx);    tx:=Label1.ScreenToClient(tx);
        iPos:=label1.Canvas.TextWidth(' ');
        iPos:=tx.x div iPos;
        if tx.X mod label1.Canvas.TextWidth(' ')>0 then inc(iPos);
        iStart:=Backwardsearchword(Copy(Label1.Caption,1,iPos));
        iEnd:=ForwardSearchword(Copy(Label1.Caption,iPos,Length(Label1.Caption)));
        iEnd:=iPos+iEnd-1;
        ShowMessage(Copy(Label1.Caption,iStart,iEnd-iStart+1));end;
    ==========================================
      

  3.   

    加入简单简体中文支持
    =============================
    procedure TForm1.Label1Click(Sender: TObject);
    var
      tx:TPoint;
      iPos:Integer;
      iStart:Integer;
      iEnd:Integer;
    begin
        //TPoint tp,tx;
        GetCursorPos(tx);    tx:=Label1.ScreenToClient(tx);
        iPos:=label1.Canvas.TextWidth(' ');
        iPos:=tx.x div iPos;
        if tx.X mod label1.Canvas.TextWidth(' ')>0 then inc(iPos);    if byte(Label1.Caption[iPos])<161 then
        begin
           iStart:=Backwardsearchword(Copy(Label1.Caption,1,iPos));
           iEnd:=ForwardSearchword(Copy(Label1.Caption,iPos,Length(Label1.Caption)));
           iEnd:=iPos+iEnd-1;
        end
        else
        begin
           iStart:=iPos;
           iEnd:=iPos+1;
           if(Byte(Label1.Caption[iPos])>247) then  iStart:=iPos-1;
           if(byte(Label1.Caption[iStart+1])<161) then
           begin
              dec(iStart);
              dec(iEnd);
           end;
           if(byte(Label1.Caption[iStart])<161) then
           begin
              Inc(iStart);
              inc(iEnd);
           end;
           if iStart<1 then iStart:=1;
           if iEnd> Length(Label1.Caption) then iEnd:= Length(Label1.Caption);
        end;
        ShowMessage(Copy(Label1.Caption,iStart,iEnd-iStart+1));end;
    =============================