Can you get the size of the visible area of ​​the cropped control?

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.

+6
source share
1 answer

For this you can use Control.ClientRectangle. What you need to do is get the borders of the screen of each control when walking through the tree and calculate the intersection.

it will be something like this:

 Control c = myControl; var rect = c.RectangleToScreen(c.ClientRectangle); while (c != null) { rect = Rectangle.Intersect(rect, c.RectangleToScreen(c.ClientRectangle)); c = c.Parent; } rect = myControl.RectangleToClient(rect); 

Now this should work for you, but to solve your problem when deciding when to draw ellipses, I recommend introducing better control instead. As a rule, it is better if each control is selected accordingly, so that all the required information is its own size to decide how to make its contents.

The problem you are describing is typical of auto-size scripts. You want it to be automatically detected, but also limited when containers are too small. I try to avoid auto-sizing whenever possible, and instead use the Dock property and custom layout, overriding OnSizeChanged. It may or may not work what you need.

+4
source

Source: https://habr.com/ru/post/915245/


All Articles