Link to home
Start Free TrialLog in
Avatar of cjinsocal581
cjinsocal581Flag for United States of America

asked on

Resize of Form based on Font size in VB.NET

What is the best way to set the form size dynamically at initialization time based on the DPI to text size? Any ideas?

I have a form that is NOT re-sizable. It is a fixed form. But I need it to size dynamically based on the DPI settings.

In other words, if I was to change the desktop to 1280 x 1024 and the DPI settings to 120, my form should resize accordingly based on that information.

Any solutions?
SOLUTION
Avatar of lordicarus
lordicarus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cjinsocal581

ASKER

Perfect.
On my computer even with that on, it doesnt scale the form to display properly in different resolutions... it scales along with the resolution and gets bigger when i make my resolution smaller... what i mean by that is bringing my resolution from 1280x1024 to 800x600, the size of my test form remains 300 by 300, when the way I thought this question was to be interpreted was that in 1280x1024 the size should be 300 by 300 but in 800 by 600 it should be 187.5 by 175.78125(the same screen to form size ratio), and the fonts should also appropriately scale.  With Autoscale turned both on or off, this is not the behavior of my form.  Is this what you are saying happens Joe or am I misinterpreting you.
A little further investigation seems that this only changes if you goto the Display settings in windows, goto the appearance tab, and change the font size from normal to extra large... at least this is what the documentation tells me... because this doesnt happen either.
Avatar of Joe_Griffith
Joe_Griffith

The screen resolution is not the issue.  If you change the resolution the form will appear bigger or smaller but it will still have the same size in pixels.

What causes the problem is changing the DPI setting (Display Properties, Advanced, General).

Most systems are set at 96DPI.  AutoScale will accomodate most other settings (120 is the one some people use) by changing the size of the form (although the change isn't perfect).  What doesn't work is if you explicitly set the size of the form in pixels in your code.  The autoscale feature then doesn't work and, typically going from 96 to 120 will result in part of the form being chopped off.
I see what you are saying, but part of the changing the form size explicitly in code, you can use

Dim g As Graphics = Me.CreateGraphics
g.DpiX

where g.DpiX is the dpi setting of the monitor.  With this value you can create that formula to automatically handle the DPI, but at the same time, you have to take into account the fact that that DPI isnt truly dots per inch.  True dots per inch is calculating how many pixels are on your screen and dividing it by the visible size of the screen in inches.  Using the AutoScale property only handles the "virtual" dpi.  You have to take into account true dpi as well.  Based on the way the question was asked I thought the solution needed was for the true dpi.  I agree with you completely, I think I just lost something in my interpretation of the question.
hi,
can anybody have a sample code to locate form based on the true dpi?
please provides it urgently if possible.

thanks.