Find the x, y point of a string within a text box

2019-03-06 09:03发布

Is there a way to return a point for a string within a text box? I found a COM function GetTextExtentPoint that will return the length of a string, but I want to know the point where the string starts.

3条回答
不美不萌又怎样
2楼-- · 2019-03-06 09:39

You're looking for the GetPositionFromCharIndex method.

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-06 09:44

what comes to mi mind is to take a snapshot of both the form and text then do some fancy image comparing to find the starting point.. but for this you need to write/download a library that has theese comparing methods... thus becoming very complicated...

why do you need to do this?

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-06 09:53

First, figure out the index of the first character of the string.

int index = textBox1.Text.IndexOf(someString);

Then use GetPositionFromCharIndex.

Point stringPos = textBox1.GetPositionFromCharIndex(index);

(Code not tested, but something like this should work. Of course you will have to deal with the possibility of duplicate occurrences of your string in the textbox.)

查看更多
登录 后发表回答