views:

185

answers:

3

Hi everyone

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

+1  A: 

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.

Pondidum
Thank you very much for your response - i have given this a shot and it almost works :), however it seems to be ignoring the height of the top and bottom borders (i.e. its about 4 pixels out) - i am going to try and use GetSystemMetrics(SM_CYBORDER) to see if that works..
fusi
you might find `SystemInformation.Border3DSize` combined with the `delta` might work then.
Pondidum
ended up going with this:Dim clientRectDelta As Integer = Me.Height - Me.ClientRectangle.Height - (SystemInformation.Border3DSize.Height * 2)and just giving a bit more space around things so that the few pixels difference isnt that noticeable - really disappointed that something that should be simple is anything but :( thank you very much for your help on this matter, you have all helped me greatly with this - a definitive answer doesnt seem to exist as it seems to be a thorny issue and i think ive got as close as im going to. thanks again!
fusi
A: 

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.

tommieb75
Thanks for your response - sorry i wasnt very clear, allow me to clarify:i need to move/resize controls based on the ClientSize and MinimumSize of a form - im going to try GetSystemMetrics now and se if it helps.im a bit disappointed something that should be so simple is turning out to be rather fiddly. why on earth is the MinimumSize property based on the total height of the window and not its client height?! argh!
fusi
A: 

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).

Cheers

Dave

fusi
you can accept an answer though! (the tick to the left of all of our replies, you can only accept one)
Pondidum