Is there a way to get the size of the visible portion of a clipped control in WinForms?
A very simple example: place a control on a form and make the form width smaller than the control width. How do you get the width of the visible area of the control? In this case you can use the form's ClientSize, but it's not always that simple.
A more complex (and real) example: I have a label with AutoSize set to true, and it can to grow beyond the width of its containing control, causing it to be clipped. When this happens I need to know. It's not as simple as comparing the width of the label with the width of its container, since the container may also have AutoSize = true and also be clipped.
Currently my approach is to walk up the container tree until I find a container that has AutoSize = false, and get its width. I also have to take the padding of each container into account. But this only considers a control being clipped by its container, or its container's container, etc. It wouldn't work if the control in question, or any of the containers, are being clipped by a sibling control with a higher Z-order. And I suspect there are other ways to easily break this approach.
Changing the label AutoSize to false isn't an option. The label is on a UserControl which is set to AutoSize so that when the label grows, the UserControl grows with it. Getting this to work without using AutoSize is painful.
Things I've tried using, unsuccessfully:
Control.ClientSize
, Control.ClientRectangle
, Control.PreferredSize
, Control.CreateGraphics().VisibleClipBounds
.
I've played with the Graphics class a little but I'm in over my head there. Graphics.VisibleClipBounds
sounded promising but always seems to return the same as the other size properties mentioned.
This pertains specifically to WinForms. I would be be happy with a p/invoke solution if that's all there is.
I did do an exhaustive search before posting. None of the similar questions are helpful.