In MFC CRicheditctrl, how to get the reactangle occupied by the character - mfc

In windows OS, in MFC CRichEditctrl, PosFromChar() gives top left position of a character, how to get the height of the character or a rectangle occupied by that character?

So - you have a top-left already. Getting a top-left for the next character will get you your top-right.
Now for the bottom. You can use CRichEditCtrl::LineFromChar to get your line index. Then use CRichEditCtrl::LineIndex to get the character on the next line. That character's top is your bottom.
If there is only one line in your control, you can call cricheditctrl::CharFromPos() repeatedly while incrementing vertical position until it will return the next character. If this is the last char, you can do that for the previous one. If there is only one char in a control - you can temporary add another one, get your coordinates and remove it.

Related

Qt draw outline for multiline text

QPainterPath can paint outline for the text
QPainter can draw multiline text
Is there any solution to make use of both of those features?
I also looked up for QLabel styles, and still didn't find anything outline-related.
Unfortunately, no such function exists in (at least, in Qt 5.15). What I did was wrote a function that takes a string and breaks it up into multiple lines, breaking on spaces or \r or \n. Loop over the string one word at a time, measuring it 'till it gets bigger than desired width (or you hit a line break), then back up one word, trunc any space, and call that one line. stick that into your vector of lines. repeat for each line. then in a separate function, you can draw each line one at a time, using stroke/fill from QPainterPath technique, advancing vertically by metrics.height() each line

setInputMask leads to thick cursor

When using setInputMask the text-cursor width changes. Due to that I can't place the cursor between two characters but it selects the whole character.
Is there a way to bypass this behaviour?

How to get enclosing area of a text drawn on an HDC in c++?

I am trying to figure out the ending point of a text drawn on an HDC. For this I am using DrawTextEx, which is giving me an enclosing rectangle. Is there any way I can get the location where last character of text is drawn ? For example :-
In case of above image I am trying to get the location of "." (which is the last character). Is there any mechanism by which I can do that ?

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 ?
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. 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. For single line one call is enough, and calculating the array of RECTs is simple. 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.

Cursor indicator in terminal console - out of buffer size

I'm writing a program that prints squares to the screen according to the user's entries, in windows terminal console.
When the cursor
indicator reaches the most bottom right corner of the console and prints a character, it forces a another line in the console, since it moves one place to the right but has no more space.
Any suggestions how to avoid it?