Question

I am trying to programmatically position certain controls within a form, based on the difference between the forms minimum size and its client size - this unfortunately produces different results depending on what theme the user has loaded (mostly the problem seems to be due to the fact that the title bar and form border have different heights/widths in different themes). I have tried using the height of the entire window (including the title bar etc) but this doesn't seem to work as expected :(

this unfortunately causes contained controls to misalign. in this particular usage scenario, use of automatic layout controls (such as the flow layout panel) isn't a viable solution.

I must be missing something really obvious - is there a better way of doing this?

Apologies if this question sounds dumb

Many Thanks Dave

Was it helpful?

Solution

The only way i have found of accurately working it out is to do something like:

int delta = this.Height - this.ClientRectangle.Height;

and then use that when ever i need to base something off the client are of the form (I used it when i wanted a form to auto size to some buttons and have an equal border around them).

So for you:

int delta = this.Height - this.ClientRectangle.Height;
int actualMinHeight = this.MinimumSize.Height - delta;

HTH

Edit: I did try using the SystemInformation.Border3DSize and SystemInformation.BorderSize properties but they also did not give the correct widths for me.

OTHER TIPS

I am not 100% sure if you are asking for a means to be able to resize controls dynamically at runtime based on the form, thickness of border, icon spacing etc, however, if this gets downvoted, I have myself to blame for misunderstanding your question, the article here on CodeProject. Since you mentioned different border sizes etc, you might want to look at system metrics that controls the border sizes by using GetSystemMetrics pinvokes, have a look here for such a thing, and here. Look here also at the pinvoke.net website for the GetSystemMetrics.

Hope this helps, Best regards, Tom.

For anyone who stumbles on this problem as well, the best solution the good people here could find was to use something along the lines of:

Dim clientRectDelta As Integer = Me.Height - Me.ClientRectangle.Height - (SystemInformation.Border3DSize.Height * 2)
Dim actualMinimumHeight As Integer = Me.MinimumSize.Height - clientRectDelta
Dim deltaHeight As Integer = Me.ClientRectangle.Height - actualMinimumHeight

However, this ignores any control specific theme dependant heights (i.e. the height of column headers in listviews change, which seem to alter the overall height of the listview, which can cause overlaps etc) - but it seems to mostly work.

A big Thank You to Pondidum, tommieb75 and nobugz who helped with this problem! (sorry to all but i dont currently have enough reputation to mark all your answers up +1).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top