c++ create text fits edit box - c++

Well.. I know that title is not that clear, I couldn't think of better one.
I wanna know how to do this...
when you have edit box and it only can show 10 characters.
Something like this
ssssssssss
let just say i have more than 10 characters. Some of them will go in the back.
Like we have this string "123456789010" it will show just these ones "3456789010".
My problem is that some characters are small and don't take that much space and some do.
So i can't find a way to break the string and get some characters in the back.
any idea?

Try this in Style type in edit box use ES_MULTILINE for use multiple lines.
edit1=CreateWindowA("edit","edit box",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE,120,160,200,200,hWnd,(HMENU)IDI_EDIT,hInstance,0);

You can calculate the display-length of the string in your control (there are several function for that) and adjust the size of the control accordingly.

you only want to see the far left or far right?
here is your string "0123456789"
you can only display 5 values due to pixel size of box....
do you want it to be "...56789" more like "56789"
or "01234..." more like "01234"?

Related

How to set up word wrap for an stc.StyledTextCtrl() in wxPython

I was wondering about this, so I did quite a bit of google searches, and came up with the SetWrapMode(self, mode) function. However, it was never really detailed, and there was nothing that really said how to use it. I ended up figuring it out, so I thought I'd post a thread here and answer my own question for anyone else who is wondering how to make an stc.StyledTextCtrl() have word wrap.
Ok, so first you need to have your Styled Text Control already defined, of course. If you don't know how to do this, then go watch some tutorials on wxPython. I recommend a youtuber called sentdex http://youtube.com/sentdex, who has a complete series on wxPython, as well as Zach King, who has a 4 episode series on making a text editor. Anyways, my definition of my text control looks like this: self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE). Yours could look a little different, but the overall idea is the same.
self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE)
Many places will tell you that it will need to be SetWrapMode(self, mode), but if you have self.CONTROLNAME at the beginning like I do, you will get an error if you also put self as an argument because self. at the beginning counts as the argument. However, if your control is defined with self.CONTROLNAME and you don't put the self.CONTROLNAME at the beginning of your SetWordWrap()function, you'll also get an error, so be careful with that. Mode just has to be 0 or 1-3. So for example, mine looks like this: self.control.SetWrapMode(mode=1). Word wrap mode options:
0: None |
1: Word Wrap |
2: Character Wrap |
3: White Space Wrap
My final definition and word wrap setup looks like this:
self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE)
self.control.SetWrapMode(mode=1)
And that's it! Hope this helped.
Thanks to #Chris Beaulieu for correcting me on an issue with the mode options.
I see you answered your own question, and you are right in every way except for one small detail. There are actually several different wrap modes. The types and values corresponding to them are as follows:
0: None
1: Word Wrap
2: Character Wrap
3: White Space Wrap
So you cannot enter any value above 0 to get word wrap. In fact if you enter a value outside of the 0-3 you should just end up getting no wrap as the value shouldn't be recognized by Scintilla, which is what the stc library is.
It would be more maintainable to use the constants stc.WRAP_NONE, stc.WRAP_WORD, stc.WRAP_CHAR and stc.WRAP_WHITESPACE instead of their numerical values.

QListWidget very long text

I'm asking this question because i dont know how to google it, i dont find the right keywords.
I have a QListWidget with strings inside. The strings are very long and if i disable horizontal scrolling, which is what i want, then the text ends with ... because it is too long.
I would like to have the text to display the end of the text and the beginning like:
This is very long text to This is ... text instead of This is very long ...
Is there any simple way to achieve this without having to manipulate the string? I need the full string afterwards and i dont want to store extra data. Any help is appreciated.
There is indeed a very easy solution: Lock for TextElideMode and Qt::ElideMiddle (setTextElideMode ( Qt::TextElideMode mode )).

Visual Studio C++ removing last character from string

I need a little help with my calculator program. I have created the code for the main buttons like the numbers 0-9 and the arithmetic operators to make it perform simple calculations.
What I'm having problems with right now is making the CE button work, after clicking the CE button I need the last entered character to be removed from the display label.
I have tried to adapt this code somehow, but it doesn't work:
lblResult->substr(0, lblResult->size()-1);
I know I'm doing somehting wrong here, can you please help me?
Thanks in advance
...Now that we know that lblResult is a System.Windows.Forms.Label, we can look at the documentation.
A Label has a Text Property, which is a String^ (i.e. a string reference).
For what you want to do, the Remove Method of String is appropriate. But note in the documentation it says that it "Returns a new string in which a specified number of characters from the current string are deleted." This means that it does not modify the string, but returns a modified copy.
So to change the label's text, we need to assign to its Text property what we want: the current string with all of the characters except the last:
lblResult->Text = lblResult->Text->Remove(lblResult->Text->Length - 1);
lblResult->resize(lblResult->size() - 1);
In this case you can use components Remove and Length methods.
Use the following code to access the components text:
component->Text
Than remove the last character of string by accessing Remove and component Length method
= component->Text->Remove(component->Text->Length - 1)
I hope you find this useful.
Just asking the obvious -- the whole statement is
*lblResult = lblResult->substr(0, lblResult->size()-1);
right?

Custom Textbox: Highlighting and Selection

I posted a question similar to this earlier, however, after thinking about it and testing the answers, I believe I misinterpreted the answers and the answerer(s) misinterpreted me. The original question is here. I think people believed that I just wanted to highlight strings, I didn't state my exact purpose. So, I will now:
What I've been trying to do lately is create a 100% from scratch text box in C++ CLR using GDI+. I've gotten to the challange of placing the caret when the user clicks in the textbox. Doing simple math (Where they clicked divided by line width) I can figure out which line they clicked. But in order to get the character clicked, I need (unless there are better ways) to compare the bounding rectangles of all the characters in the line and place the caret before the one the mouse fits into. In order to do this, I need to get the exact bounds of each individual character, not an entire string.
I've already tried a few things, none of which seemed to work:
Graphics::MeasureString is not recommended by anyone, nor does
it give what I want
TextRenderer::MeasureText is more accurate, but for this not accurate enough
Graphics::MeasureCharacterRanges has a 32 character
cap, and I'm expecting lines to be over 32 characters long in some
cases
I believe I can't use these methods, unless there are ways around their limitations. I hope I made my problem and expected solution a lot more clear than I previously did.
Because of the way text is kerned and anti-aliased, the boundary of a character depends on all of the characters to the left of it. However you don't need to know every character boundary, only the ones on either side of your click point. You can find those with a binary search - split your string in half, measure that (using TextRenderer::MeasureText), and determine if it's to the left or right of your click point. Keep narrowing down the size of the string until there's only one possibility remaining.

Irritating Text Wrapping with GDI & Direct3D

If I use Direct3D's DrawText, which behaves like the GDI counterpart, and I enable the DT_WORDBREAK formatting flag, it'll cut the text on apostrophes whenever the situation arises, which leaves many titles and text displays looking like a mess. For instance, it will appear as:
The Horseman'
s Head
When it should say:
The
Horseman's
Head
It seems unlikely considering the restrictive nature of the font handling functions, but is there any way of specifying it so that it only cuts text on white-space or when there's absolutely nothing that can be done?
You can wrap manually with the help of the GetTextExtentPoint32 function.
I've run in to this before, and the only solution is to write your own word wrapper, using DT_CALCRECT to measure the width of a given string. It's annoying and it does suck, but I've never seen an alternative.
I don't think so (i may be wrong). But you can set a "virtual" limit on a Width. Calculate line length (char_count*font_width) and make the jumps yourself. You can even add special char "\n", to insert your own jumps...