简体   繁体   English

如何在std :: string中获得正确的位置?

[英]How to get correct position in the std::string?

I am creating a custom single line edit control, with a custom font in win32 api on windows 7, the font is not a fixed width font, and I need to move caret according to the mouse click, The edit control is not empty and if I know the horizontal position of the mouse click within the window, how do I calculate the number of characters after which I need to move caret to ? 我正在创建一个自定义单行编辑控件,并在Windows 7的win32 api中使用自定义字体,该字体不是固定宽度的字体,我需要根据鼠标单击来移动插入符号,该编辑控件不为空,如果我知道鼠标在窗口内单击的水平位置,如何计算插入符号后需要移动的字符数?

I really am out of ideas, if it was a fixed width font, I would have divided the horizontal mouse click position with average character width, that would have been simpler, doing the same with not a fixed width font, is prone to errors. 我真的没有主意,如果是固定宽度的字体,我会将水平鼠标单击位置除以平均字符宽度,那样会更简单,如果不使用固定宽度的字体,则容易出错。

Given that it's a single-line control, you probably don't plan on working with immensely long input (at least normally). 鉴于它是单行控件,您可能不打算使用非常长的输入(至少正常情况下)。 That being the case, one possibility would be to just store the character positions in an array (or vector, etc.) Then you can use (for example) a binary search in that array to find character positions. 在这种情况下,一种可能性就是仅将字符位置存储在数组(或向量等)中。然后,您可以在该数组中使用(例如)二进制搜索来查找字符位置。 Of course, you can do the same even for longer strings--though it can increase storage requirements quite a bit. 当然,即使对于更长的字符串,您也可以执行相同的操作-尽管这可能会大大增加存储需求。

This is a familiar problem. 这是一个熟悉的问题。 You are in essence trying to do hit testing on text and for that you need the location on the screen of each character of the text. 本质上,您是在尝试对文本进行命中测试,为此,您需要在屏幕上找到每个文本字符的位置。

My preferred strategy is to calculate an array of RECT, one for each character of displayed text. 我的首选策略是计算一个RECT数组,其中每个字符用于显示的文本。 The array needs to be updated when text is added or deleted, but it easily handles single or multiple lines. 添加或删除文本时需要更新该数组,但是它可以轻松处理单行或多行。 The function GetCharWidth32 retrieves all the widths for a string of text in a particular font selected into a DC. 函数GetCharWidth32检索选择到DC中的特定字体的文本字符串的所有宽度。 For single line one call is enough, and calculating the array of RECTs is simple. 对于单线电话,一个电话就足够了,并且计算RECT的数组很简单。 It's not much harder to do multiline. 进行多行并不难。

Handle the mouse down message, loop through the array and find the right character. 处理鼠标向下的消息,遍历数组并找到正确的字符。 A brute force search is plenty fast enough. 蛮力搜索足够快。

This method is simple and easily generalises to a range of similar problems. 这种方法很简单,并且很容易推广到一系列类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM