How to detect was EllipsisWord applied or not? - winforms

In winforms app I use Graphics.DrawString(String, Font, Brush, RectangleF, StringFormat) with StringTrimming.EllipsisWord for the last parameter. How can I know was the String ellipsed by the method or not?

You can't tell directly. You'll have to measure first to see if it fits, Graphics.MeasureString().

Related

How to detect whether a winforms DataGridViewCell.Value is entirely visible in its cell?

I'm looking for an easy way of identifying whether a WinForms DataGridViewCell.Value is entirely visible in its cell.
If the column is too narrow, only part of the value will be visible, and I need to detect that situation in code.
So far I'm thinking that I could compare the width of the content (with Graphics.MeasureString) with the width of the cell, but that seems a little clunky.
Looking for something a bit more elegant if possible.
Thanks
From looking at the source code for DataGridViewCell, it looks like the .NET team decided to use the TextRenderer MeasureText function rather than the Graphics MeasureString function (see line 2924). It's not exactly what you were looking for, but it looks like it'd be a little less clunky then having to retrieve a Graphics object.
Beyond that, I do not believe there is a way to say for certain whether the Text cell is truncating a value or not. This probably has to do with performance. The DataGrid does not store a separate cell object for each cell... that would be far too memory intensive for large data sets. Rather, it stores style information as needed (usually for a whole column, though you can override the style data for a particular cell as necessary) and the the cell value (in a giant object array). When it comes time to render the cell, it reuses the same cell object for each cell in a column (calling Paint over and over with different cellBounds and value, etc.). Only during rendering would it know if the content is too long, but it throws this information away almost immediately (having nowhere to store it and no need for it after rendering).
I suppose the .NET team could have created a function to do all the measuring, etc., for you, but then again there are a lot of features that could have been implemented. This one wasn't.
Just an idea ..
Function IsFit() as Boolean
Dim szDummy As New SizeF
dim picDummy as New Picturebox '--> or refer to your picbox
gDummy = Me.picDummy.CreateGraphics
szDummy = gDummy.MeasureString(column value , New Font(FontName, _
FontSize, FontStyle, graphicUnitPixel))
if szDummy.Width >= MyDataGrid.Columns(column name).Width then return True
End Function

In wpf how to calculate pixel length of string?

I have a string to draw in a custom dialog box. How can i get the required length of string in pixels using WPF?
If you want to show it afterwards within a TextBlock, create the TextBlock and call Measure and Arrange. Make sure that the TextBlock has set the right font size before calling Measure.
Another way is to go via FormattedText, if you want to do your calculations on a low level.
You might not need the (pixel) size.
It might be better to automatically size the dialog to its content.

Increasing the size of a WPF application

I've just created my first WPF application (3 calculators inside 3 different tabs).
The entire application has been built using widths/margins/paddings as static values, since I originally didn't know that dynamic values can be used by just putting an asterix after the value.
The client has come back to me though and has asked me to increase the size of the app, that includes form fields, tabs, font-sizes, grids etc...
What would be the easiest (and/or quickest) way to do this? I'd hate to go value by value resizing every single element since there are quite a few.
I can provide code but there is lots of it and I'm not sure of how much help it would be.
Appreciate your help,
Marko
Put it all in one ViewBox, play with viewbox size to change the app size
Write an XSLT transform to take your XAML as input and spit out appropriate modified XAML, which you put back in your app.

Check for overlapping shapes in WPF

I have a set of shapes which need to be drawn on top of each other. I need to re-order them such that the smallest shape gets drawn last (i.e. it will be closest to the user). Is there any way to check whether a shape overlappes (encloses and/or intersects) another shape. I know there is a method in Rect structure called Contains which checks whether there is an object within it. Is there a similar method or a way to simulate it on Shapes in WPF? Thanks in advance for any help.
Cheers,
Nilu
You could probably use the Geometry.FillContainsWithDetail method. Its name is ill-chosen IMHO, but the description is clear :
Returns a value that describes the intersection between the current geometry and the specified geometry.
I have successfully used it for collision testing before, so it should work for you too...

Best way to write a custom gauge control in WPF?

I need to write a gauge control in WPF for a project at work. By gauge control, I mean something akin to a traditional car speedometer: a circular face with numbers and ticks around the circumference, and a needle pointing to the current value. Although I could get my employer to purchase a third-party control, or even find a free one somewhere, I’d still like to write it myself, for curiosity’s sake more than anything else.
My first thought was to ‘skin’ an existing control using a template, something like a ProgressBar for example as it has Minimum, Maximum and Value properties. However, I don’t think this will offer me the flexibility that I need.
I’d like to animate the needle that points to the current value, so that when the gauge’s value changes the needle moves from the old value to the new value. I figured the easiest way to do this would be to have some kind of shape or geometry representing the needle, and then apply a RotateTransform of the relevant number of degrees to get the needle to point to the correct value. The animation’s To property would be set to the ‘value degrees’ property via binding.
It seems to me that there are three basic approaches I could take:
Use one custom FrameworkElement to represent the entire gauge
I could derive from FrameworkElement, override its OnRender method, then use the DrawingContext to draw both the gauge face and the needle. This would mean that I could no longer animate the needle directly using a RotateTransform, but would instead have to redraw the entire gauge every time the value changes. This seems inefficient to me, as the gauge face represents the bulk of the drawing code but would change very rarely. However, this approach is the most lightweight in terms of the number of elements used.
Use two custom FrameworkElements, one for the face and one for the needle
I’m currently leaning towards this approach. One element would represent the face, the other the needle. This would allow me to target the needle element with the RotateTransform. The gauge control would then consist of three elements: the face, the needle, and a Panel container to hold both of them (ie. Canvas, Grid, whatever). So three elements instead of one, hence not as lightweight as the first approach.
Use DrawingVisuals to draw the face and needle
I’ve also read about the DrawingVisual class, but I’m a bit confused as to why anyone would use this as opposed to deriving from FrameworkElement and overriding OnRender, given that DrawingVisual must be hosted in a custom FrameworkElement anyway. It would seem that this approach doesn’t offer any advantages over the second approach, and would require more code, but maybe I’m missing something.
Any thoughts or advice regarding which approach people think is best and why would be most welcome!
Personally I'd recommend making a CustomControl using the second approach. Unless you are going to be showing more than 1000 of the gauges in view at the same time you aren't going to notice the extra element in your visual tree and I think you'll find it's much easier to make and maintain.
You could just style a slider control and feed values into it. You should be able to make a slider look like any kind of gauge you need.

Resources