在一个自定义函数里能不能调用RichEditMouseMove事件里的变量?(15分)

  • 主题发起人 bingxueshanren
  • 开始时间
B

bingxueshanren

Unregistered / Unconfirmed
GUEST, unregistred user!
//i,j是全局变量<br>procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X,<br> &nbsp;Y: Integer);<br>var<br> &nbsp;WordInRE: string;<br> &nbsp;iCharIndex, iLineIndex, iCharOffSet : integer;<br> &nbsp;pt : TPoint;<br> &nbsp;s : string;<br>begin<br> &nbsp;with TRichEdit(sender) do<br> &nbsp;begin<br> &nbsp; &nbsp;pt := Point(X,Y);<br> &nbsp; &nbsp;iCharIndex := Perform(EM_CHARFROMPOS, 0, Integer(@Pt));<br> &nbsp; &nbsp; //获得光标所在字符的字符索引值(从开始算起直至光标所在字符)<br> &nbsp; &nbsp;if iCharIndex &lt; 0 then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;iLineIndex := Perform(EM_EXLINEFROMCHAR,0,iCharIndex);<br> &nbsp; &nbsp; //获得光标所在字符的行索引值<br> &nbsp; &nbsp;iCharOffSet := iCharIndex - Perform(EM_LineIndex,iLineIndex,0);<br> &nbsp; &nbsp; &nbsp;//光标所在字符偏离其所在行的第一个字符的位置 ,也就是说光标所在字符是该行 的第几个字符<br> &nbsp; &nbsp;if Lines.Count -1 &lt; iLineIndex then<br> &nbsp; &nbsp; &nbsp; Exit;<br> &nbsp; &nbsp;s := Lines[iLineIndex];<br> &nbsp; &nbsp;Inc(iCharOffSet); &nbsp; &nbsp;//将光标所在字符向后移一位<br> &nbsp; &nbsp;i := iCharOffSet;<br> &nbsp; &nbsp;while (i&gt;0) and (s&lt;&gt; ' ') do<br> &nbsp; &nbsp; &nbsp;i := i-1; &nbsp; &nbsp;//向前查找直至找到光标所在单词的第一个字母的位置<br> &nbsp; &nbsp; &nbsp;Inc(iCharOffSet);<br> &nbsp; &nbsp;j := iCharOffSet;<br> &nbsp; &nbsp;while (j &lt;= Length(s)) and (s[j] &lt;&gt; ' ') do<br> &nbsp; &nbsp; &nbsp;Inc(j); &nbsp;//从单词的第一个字母循环向后找直至找到最后一个字母的位置<br> &nbsp; &nbsp;WordInRE := Copy(s,i,j - i); //获得光标所在的单词<br> &nbsp;end;<br>end;<br>我想在下面的自定义函数中调用上面时间中的变量i,j的值,不知该如何调用?<br>procedure TForm1.SetWordProp(i, j: Integer);<br>begin<br> &nbsp;//i :=<br> &nbsp;//j :=<br> &nbsp;RichEdit1.SelStart := i;<br> &nbsp;RichEdit1.SelLength := j-i;<br> &nbsp;RichEdit1.SelAttributes.Color := clRed;<br> &nbsp;RichEdit1.SelAttributes.Style := [fsUnderline];<br><br>end;
 
既然是“全局变量”,就表示在整个单元中哪里都可以用啦(除了不能做子程序里面的循环控制变量之外),那还有什么担心的。你的自定义过程里面不用定义(i,j)参数,直接在过程执行体里面引用i,j就是那两个全局变量了(注意,你的过程里面不能再定义名字是i和j的局部变量,否则你在过程执行体中引用i和j时就是你这个过程自己的i和j而不是全局变量i和j了)。
 
你说的我已经试过了,可调用后并不起作用啊。<br>请各位专家再帮帮我了,在此先谢谢各位。
 
如果你上面贴出来的代码不改的话,当然用不了啦。你那个自定义过程里面是不用定义参数的。
 
问题已经解决了,谢谢您的参与。
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
637
import
I
I
回复
0
查看
485
import
I
顶部