making form compatible with all screen resolutions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: making form compatible with all screen resolutions

  1. #1
    Join Date
    Apr 2005
    Posts
    32

    making form compatible with all screen resolutions

    I ve been breaking head for this trouble for a week. I used 2 type of methods to adjust screen resolution :
    I designed my form in 800*600 resolution and has given form.windowstate =2 for displaying full form. I ve viewing this form in other resolutions please help me.

    1) This I put up in module and called it in form_Resize and form_load:

    Dim ctl As Control

    For Each ctl In Me.Controls

    ctl.Height = ctl.Height * 100 / 78.125 ' (1024:100 = 800:x)
    ctl.Width = ctl.Width * 100 / 78.125 ' (768:100 = 600:x)

    Next ctl

    Me.Height = Me.Height * 100 / 78.125
    Me.Width = Me.Width * 100 / 78.125


    End Sub

    Above code gives error at the point ctl.height .Run time error: 438 Object does not support this property or method. I Can't figure out how to
    straighten out this.

    I tried this api method too:

    Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
    Public Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long


    Dim a As Boolean
    Dim i&
    i = 0

    Do
    a = EnumDisplaySettings(0&, i&, DevChg)
    i = i + 1
    Loop Until (a = False)

    Dim b&
    DevChg.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
    DevChg.dmPelsWidth = iWidth
    DevChg.dmPelsHeight = iHeight
    b = ChangeDisplaySettings(DevChg, 0)

    where iwidth and iHeight are:800 and 600 respectively in which I designed my forms.

    Running from 640*480 display, though resolution becomes 800*600, the place where taskbar appears becomes hollow and displays green of desktop. At the same time
    taskbar appears 1.5 inches above its original place, hiding some buttons in the application, with its length shortened by 2 inches.

    Running from 1024*768 resolution, display becomes 800*600, the place where taskbar appears becomes hollow and displays green of desktop. The tagline of icons in the last row
    become a bit hidden, and task bar completely hidden(I suppose it goes below the screen). I could only access every application using Alt+tab shortcut. Apart from this trouble, whatever
    vbmodal form I display, becomes left aligned despite giving the line of code:

    Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2

    Later I found out the screen.width here becomes 12000 rather than 9000 which is in original 800*600 execution of program.

    I slightly changed the code to work for all kinds of resolution and also while exiting the application, the original resolution to be restored. That's working fine.

    I require that, taskbar must come in its original space irrespective of the resolution from which program runs, not disturbing the alignment of vbmodal forms I display. How to modify the code
    with API please anybody let me know?

    If there are other better ways too let me know.


    THis I used finally but this too is giving me lot of headache.

    'This is in module
    Public Xtwips As Integer, Ytwips As Integer
    Public Xtwips As Integer, Ytwips As Integer
    Public Xpixels As Integer, Ypixels As Integer

    Type FRMSIZE
    Height As Long
    Width As Long
    End Type

    Public RePosForm As Boolean
    Public DoResize As Boolean
    Dim MyForm As FRMSIZE
    Dim DesignX As Integer
    Dim DesignY As Integer


    Sub Resize_For_Resolution(ByVal SFX As Single, _
    ByVal SFY As Single, MyForm As Form)
    Dim i As Integer
    Dim SFFont As Single

    SFFont = (SFX + SFY) / 2 ' average scale
    ' Size the Controls for the new resolution
    On Error Resume Next ' for read-only or nonexistent properties

    With MyForm
    For i = 0 To .Count - 1
    If TypeOf .Controls(i) Is ComboBox Then ' cannot change Height
    .Controls(i).Left = .Controls(i).Left * SFX
    .Controls(i).Top = .Controls(i).Top * SFY
    .Controls(i).Width = .Controls(i).Width * SFX
    Else
    .Controls(i).Move .Controls(i).Left * SFX, _
    .Controls(i).Top * SFY, _
    .Controls(i).Width * SFX, _
    .Controls(i).Height * SFY
    End If
    ' Be sure to resize and reposition before changing the FontSize
    .Controls(i).FontSize = .Controls(i).FontSize * SFFont
    Next i

    If RePosForm Then
    ' Now size the Form
    .Move .Left * SFX, .Top * SFY, .Width * SFX, .Height * SFY
    End If
    End With
    End Sub

    'This is in form_load event, also form_resize event.

    Sub get_form_screen_parameters(frm As Form)
    Dim ScaleFactorX As Single, ScaleFactorY As Single ' Scaling factors
    ' Size of Form in Pixels at design resolution
    DesignX = 800
    DesignY = 600
    RePosForm = True ' Flag for positioning Form
    DoResize = False ' Flag for Resize Event
    ' Set up the screen values
    Xtwips = Screen.TwipsPerPixelX
    Ytwips = Screen.TwipsPerPixelY
    Ypixels = Screen.Height / Ytwips ' Y Pixel Resolution
    Xpixels = Screen.Width / Xtwips ' X Pixel Resolution

    ' Determine scaling factors
    ScaleFactorX = (Xpixels / DesignX)
    ScaleFactorY = (Ypixels / DesignY)
    ScaleMode = 1 ' twips
    'Exit Sub ' uncomment to see how Form1 looks without resizing
    Resize_For_Resolution ScaleFactorX, ScaleFactorY, frm
    'Label1.Caption = "Current resolution is " & str$(Xpixels) + _
    ' " by " + str$(Ypixels)
    MyForm.Height = frm.Height ' Remember the current size
    MyForm.Width = frm.Width
    End Sub






    I slightly changed the variation of this code for 1st method such that it scales things. But still the performance is unsatisfactory. If I minimize the form, every character becomes more than 500 font size. Not only that during normal execution, though 1/3rd part of a form which is unused in 1024*768 disappears, form becomes big and I could not view many controls in its maximized size. I think many things go off screen.

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    there are two problems in these lines:

    ctl.Height = ctl.Height * 100 / 78.125 ' (1024:100 = 800:x)
    ctl.Width = ctl.Width * 100 / 78.125 ' (768:100 = 600:x)

    (I haven't check the next)

    First, it is very well possible that not the all components allows to change the Height and Width, you must put error handling.
    Second, most important, you continue to change the size with a scale of 100 / 78.125, that is wrong. You have to remember the original size (the one by 800*600) and scale according to that:

    ctl.Width = ctlOriginalWidth / 800 * me.width
    ctl.Height = ctlOriginalHeight / 800 * me.height

    The problem is to store the original size. One solution is to create a collection, and in the first Resize event (or anytime when we are sure that all controls are initialized) save the size using the control name as the key of the collection:

    for each ctl in Controls
    dim c as new Size ' a class to hold the control properties, to be defined
    c.top = ctl.top
    c.left...
    c.witdh...
    c.height...
    c.font.size...
    myCollection.Add c, ctl.name

    I save all the basic properties that needs to be changed. In the Resize event:

    dim c a Size
    for each ctl in Controls
    set c = myCOllection(ctl.name)
    ''error checking!!
    ctl.width = c.width / 800 * me.width
    etc etc

    A lot of work? yes, this is why people prefer to use third party components...

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Apr 2005
    Posts
    32
    Hi Marco

    I ve tried collection as u specified. But still I m having trouble plz check out my bas module in this attachment and tell me how to improve it.
    Attached Files Attached Files

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    sorry, but the code you sent is too long, I really do not have time to debug it. Especially because I do not know what is wrong with it! As I said before, "this is why people prefer to use third party components..."
    First, I would not use a bas module. I would write the code in a class, and each form that needs a resizer will create an instance of it. That makes the code a little neater. And I would not use an array of UDT, but a collection of classes (see my previous post), because in that way we can get an element directly by key (instead of iterating through an array), see my previous post.
    And still the snippets I sent are not complete. The position of a control are not relative to the form, but to the control's container. If a control is in a Frame, or a PictureBox for example, left and right are relative to the Frame, not to the form. When you resize you have to keep that in mind. And sometimes there are control that needs only to be resized, but not moved, or viceversa. And what about font size? Probably you need to change that as well... as you see, there is a lot of work to do.

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

Similar Threads

  1. Replies: 1
    Last Post: 06-14-2002, 04:49 PM
  2. Replies: 0
    Last Post: 04-11-2002, 07:48 PM
  3. Height of the Taskbar
    By James Brown in forum VB Classic
    Replies: 3
    Last Post: 03-26-2002, 10:35 AM
  4. Form sizes and screen resolution settings
    By Jim Burak in forum Architecture and Design
    Replies: 1
    Last Post: 07-20-2001, 01:43 PM
  5. Setting form as MDIChild form at runtime
    By Setya in forum VB Classic
    Replies: 0
    Last Post: 01-08-2001, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center