Is there a way to get the size of the visible portion of a cropped control in WinForms?
A very simple example: place a control on a form and make the width of the form less than the width of the control. How do you get the width of the visible area of ββthe control? In this case, you can use the ClientSize form, but it is not always so simple.
A more complex (and real) example: I have a label with the AutoSize parameter set to true, and it can grow beyond the width of the control containing it, as a result of which it will be trimmed. When this happens, I need to know. This is not as simple as comparing the width of the label with the width of its container, since the container can also have AutoSize = true, as well as be cropped.
My current approach is to go up to the container tree until I find a container that has AutoSize = false and gets its width. I must also consider the contents of each container. But this only applies to the fact that the control is cropped by its container or container container, etc. This will not work if the control in question or any of the containers is cut off by a control element with a higher Z-order. And I suspect there are other ways to easily disrupt this approach.
Changing the AutoSize label to false is not an option. The shortcut is in the UserControl, which is set to AutoSize, so when the label grows, the UserControl grows with it. Using this to work without using AutoSize is painful.
Things I tried to use unsuccessfully:
Control.ClientSize
, Control.ClientRectangle
, Control.PreferredSize
, Control.CreateGraphics().VisibleClipBounds
.
I played a little with the Graphics class, but I have it over my head. Graphics.VisibleClipBounds
sounds promising, but always seems to return just like the other size properties mentioned.
This applies to WinForms. I would be pleased with the p / invoke solution if all of this is there.
I did an exhaustive search before posting. None of these questions are helpful.