Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

How to make program display independent of screen resolution ("resolution indepence")

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to make program display independent of screen resolution ("resolution indepence")

    ' how to make program display independent of screen resolution ("resolution indepence")
    '
    ' please make your comments here:
    ' http://www.powerbasic.com/support/pb...ad.php?t=14262
    '
    ' the programmer is faced with the challenge that a very large number of
    ' computer display resolutions are being used as seen in this link:
    '
    ' http://en.wikipedia.org/wiki/computer_display_standard
    '
    ' some of the more frequent resolutions are:
    '
    ' 640 x 480
    ' 800 x 600
    ' 1024 x 768
    ' 1280 x 800
    ' 1152 x 864
    ' 1440 x 900
    ' 1280 x 960
    ' 1280 x 1024
    ' 1400 x 1050
    ' 1680 x 1050
    ' 1600 x 1200
    ' 1920 x 1200
    ' 2048 x 1536
    '
    ' to optimize display of your program, you will need to take the end-user's
    ' screen resolution into account. this means that your program should include
    ' features to detect screen resolution and to modify the program display
    ' accordingly. ensuring resolution independence of your program will allow its
    ' elements to be drawn in sizes independent from the actual pixel grid.
    '
    ' this program illustrates some ideas to do just that.
    '
    ' first the program will display the dialog adapted to fill the
    ' whole desktop client area minus the system tray area. this would normally
    ' be the optimal screen display.
    '
    ' in this demonstration program you can increase and decrease the
    ' dialog size according to some of the more frequent screen sizes,
    ' and in this way you will be able to see if the display is
    ' satisfactory for any of these screen sizes.
    '
    ' the font size will change according to the dialog height. the font
    ' "arial" is being used instead of the "ms sans serif" font, which is
    ' normally used as the system font. however, ms sans serif is not
    ' suitable for resizing. you may test this out for yourself.
    '
    ' the initial value of fontheight has been set to 11. which value
    ' would be best would depend on the initial size of your dialog,
    ' when you design it. you may chose other initial fontheight values.
    ' the font height is changed according to the height (y) conversion
    ' factor ycf being reduced by an exponent value of around 0.45. you
    ' may find other exponent values to be better. you may even find
    ' that with larger screen sizes it may not be necessary to increase
    ' the font size. this is a matter of taste. however, for small
    ' screen sizes it will be important to reduce the font size.
    '
    ' best regards
    '
    ' erik christensen --------------- may 27, 2007
    '
    ' p.s. may 28, 2007: i deleted the %ws_thickframe style in the dialog.
    ' this allows you to see the whole upper part of the dialog at large
    ' sizes by moving the caption sideways.
    '
    ' later same day: i included facilities to move large dialogs using the
    ' arrow keys. in this way you can inspect every part of enlarged dialogs
    ' being larger than your screen. this part was inspired by code by borje
    ' hagsten. thanks.
    '
    ' remember: the key to full control over your program display is to dimen-
    ' sion dialogs and controls in pixels and to define font heights in pixels
    ' as described in this link:
    ' http://www.powerbasic.com/support/pb...ad.php?t=25005
    '
    Code:
    #compile exe
    #dim all
    '
    #include "win32api.inc"
    #include "commctrl.inc"
    '
    %idd_dialog1         =  101
    %idr_menu1           =  102
    %idm_file_exit       = 1001
    %idc_textbox1        = 1002
    %idc_listbox1        = 1003
    %idc_syslistview32_1 = 1004
    %idc_button1         = 1005
    %idc_button2         = 1006
    %idc_button3         = 1007
    %idc_label1          = 1008
    %idc_label2          = 1009
    %idc_label3          = 1010
    %idc_label4          = 1011
    '
    function attachmenu1(byval hdlg as dword) as dword
        local hmenu   as dword
        local hpopup1 as dword
        menu new bar to hmenu
        menu new popup to hpopup1
        menu add popup, hmenu, "&file", hpopup1, %mf_enabled
            menu add string, hpopup1, "e&xit", %idm_file_exit, %mf_enabled
        menu attach hmenu, hdlg
        function = hmenu
    end function
    '
    function samplelistbox(byval hdlg as dword, byval lid as long, byval lcount _
        as long) as long
        local i as long
        for i = 1 to lcount
            listbox add hdlg, lid, "test item"+str$(i)
        next i
    end function
    '
    function samplelistview(byval hdlg as dword, byval lid as long, byval lcolcnt _
        as long, byval lrowcnt as long) as long
        local lcol   as long
        local lrow   as long
        local hctl   as dword
        local tlvc   as lv_column
        local tlvi   as lv_item
        local szbuf  as asciiz * 32
        local lstyle as long
        control handle hdlg, lid to hctl
        lstyle = listview_getextendedlistviewstyle(hctl)
        listview_setextendedlistviewstyle(hctl, lstyle or %lvs_ex_fullrowselect _
            or %lvs_ex_gridlines)
        ' load column headers.
        tlvc.mask    = %lvcf_fmt or %lvcf_text or %lvcf_subitem
        tlvc.fmt     = %lvcfmt_left
        tlvc.psztext = varptr(szbuf)
        for lcol = 0 to lcolcnt - 1
            szbuf       = using$("column #", lcol)
            tlvc.iorder = lcol
            listview_insertcolumn(hctl, lcol, tlvc)
        next lcol
        ' load sample data.
        for lrow = 0 to lrowcnt - 1
            tlvi.statemask = %lvis_focused
            tlvi.psztext   = varptr(szbuf)
            tlvi.iitem     = lrow
            for lcol = 0 to lcolcnt - 1
                szbuf         = using$("column # row #", lcol, lrow)
                tlvi.isubitem = lcol
                tlvi.lparam   = lrow
                if lcol = 0 then
                    tlvi.mask = %lvif_text or %lvif_param or %lvif_state
                    listview_insertitem(hctl, tlvi)
                else
                    tlvi.mask = %lvif_text
                    listview_setitem(hctl, tlvi)
                end if
            next lcol
        next lrow
        ' auto size columns.
        for lcol = 0 to lcolcnt - 2
            listview_setcolumnwidth(hctl, lcol, %lvscw_autosize)
        next lcol
        listview_setcolumnwidth(hctl, lcolcnt - 1, %lvscw_autosize_useheader)
    end function
    '
    callback function showdialog1proc()
        static w as long, h as long
        static x as long, y as long
        static xi as long, yi as long
        static xcf as single, ycf as single
        static i as long, j as long, k as long, m as long
        static c as long
        static hfont as long, fontheight as long, fh as long, prfh as long
        static hdc as long, ps as paintstruct, paintflag as long
        dim sol(1 to datacount / 2, 1 to 2) as static long
        static stsize as long, prsi as long
        static t as string
        select case as long cbmsg
    
            case %wm_initdialog
                ' initialization handler
                t="how to make program display independent of screen resolution ("resolution indepence")"+$crlf+$crlf+ _
                "the programmer is faced with the challenge that a very large number of "+ _
                "computer display resolutions are being used as seen in this link: "+$crlf+$crlf+ _
                "http://en.wikipedia.org/wiki/computer_display_standard "+$crlf+$crlf+ _
                "some of the more frequent resolutions are: "+$crlf+ _
                "640 x 480 "+$crlf+ _
                "800 x 600 "+$crlf+ _
                "1024 x 768 "+$crlf+ _
                "1280 x 800 "+$crlf+ _
                "1152 x 864 "+$crlf+ _
                "1440 x 900 "+$crlf+ _
                "1280 x 960 "+$crlf+ _
                "1280 x 1024 "+$crlf+ _
                "1400 x 1050 "+$crlf+ _
                "1680 x 1050 "+$crlf+ _
                "1600 x 1200 "+$crlf+ _
                "1920 x 1200 "+$crlf+ _
                "2048 x 1536 "+$crlf+$crlf+ _
                "to optimize display of your program, you will need to take the end-user's "+ _
                "screen resolution into account. this means that your program should include "+ _
                "features to detect screen resolution and to modify the program display "+ _
                "accordingly. ensuring resolution independence of your program will allow its "+ _
                "elements to be drawn in sizes independent from the actual pixel grid. "+$crlf+$crlf+ _
                "this program illustrates some ideas to do just that. "+$crlf+$crlf+ _
                "first the program will display the dialog adapted to fill the "+ _
                "whole desktop client area minus the system tray area. this would normally "+ _
                "be the optimal screen display. "+$crlf+$crlf+ _
                "in this demonstration program you can increase and decrease the "+ _
                "dialog size according to some of the more frequent screen sizes, "+ _
                "and in this way you will be able to see if the display is "+ _
                "satisfactory for any of these screen sizes. "+$crlf+$crlf+ _
                "the font size will change according to the dialog height. the font "+ _
                ""arial" is being used instead of the "ms sans serif" font, which is "+ _
                "normally used as the system font. however, ms sans serif  is not "+ _
                "suitable for resizing. you may test this out for yourself. "+$crlf+$crlf+ _
                "the initial value of fontheight has been set to 11. which value "+ _
                "would be best would depend on the initial size of your dialog, "+ _
                "when you design it. you may chose other initial fontheight values. "+ _
                "the font height is changed according to the height (y) conversion "+ _
                "factor ycf being reduced  by an exponent value of around 0.45. you "+ _
                "may find other exponent values to be better. you may even find "+ _
                "that with larger screen sizes it may not be necessary to increase "+ _
                "the font size. this is a matter of taste. however, for small "+ _
                "screen sizes it will be important to reduce the font size. "+$crlf+$crlf+ _
                "best regards "+$crlf+$crlf+ _
                "erik christensen --------------- may 27, 2007 "
                '
                control set text cbhndl, %idc_textbox1, t
                '
                for i = 1 to datacount / 2
                    for j = i*2-1 to i*2
                        sol(i, ((j+1) mod 2) + 1) = val(read$(j))
                    next
                next
                data 640, 480
                data 800, 600
                data 1024, 768
                data 1280, 800
                data 1152, 864
                data 1440, 900
                data 1280, 960
                data 1280, 1024
                data 1400, 1050
                data 1680, 1050
                data 1600, 1200
                data 1920, 1200
                data 2048, 1536
                '
                desktop get size to w, h
                control set text cbhndl, %idc_label1, "screen size:"+str$(w)+" x"+str$(h)+" pixels"
                do
                    incr stsize
                loop until sol(stsize, 1) = w and sol(stsize, 2) = h   ' locate screen start size in size array
                '
                dialog get size cbhndl to xi, yi ' initial size of dialog
                '
                desktop get loc to x, y          ' get position of upper left corner of client area
                desktop get client to w, h       ' get client size on desktop
                '
                control set text cbhndl, %idc_label2, "screen client area:"+str$(x)+" to"+str$(x+w)+"  x "+str$(y)+" to"+str$(y+h)+" pixels"
                '
                c = getsystemmetrics(%sm_cycaption) + getsystemmetrics(%sm_cymenu) ' height of caption and menu
                '
                fontheight = 11 ' suitable start size for arial font. nb: "ms sans serif" is not suited for resizing.
                '               ' you may use another start font height dependent on your screen size
                prfh = fontheight
                '
                gosub resize
                '
                samplelistbox  cbhndl, %idc_listbox1, 25
                samplelistview cbhndl, %idc_syslistview32_1, 4, 30
                paintflag = 0
                setfocus cbhndl
                '
                function = 1 : exit function
                '
                '**********************************************************************************************************
                resize:
                '
                desktop get loc to x, y          ' get position of upper left corner of client area
    
                dialog set loc cbhndl, x, y      ' set upper left corner of dialog to upper left corner of client area
                dialog set size cbhndl, w, h     ' set dialog size
                '
                control set text cbhndl, %idc_label3, "dialog size:"+str$(w)+" x"+str$(h)+" pixels"
                '
                xcf = csng(w) / csng(xi)         ' x (horizontal) conversion factor
                ycf = csng(h-c) / csng(yi-c)     ' y (vertical) conversion factor (adjust for height of caption and menu)
                '
                fh = prfh * ycf^0.45             ' font height modification
                prfh = fh                        ' previous font height
                control set text cbhndl, %idc_label4, "font height:"+str$(fh)+" pixels"
                deleteobject hfont
                hfont = createfont(fh,0,0,0,400,0,0,0,0,3,2,1,82,"arial")
                '
                for i = 1001 to 1011 ' for all control ids
                    control get loc cbhndl, i to x, y
                    control get size cbhndl, i to w, h
                    control set loc cbhndl, i, round(csng(x) * xcf, 0), round(csng(y) * ycf, 0)  ' set new location
                    control set size cbhndl, i, round(csng(w) * xcf, 0), round(csng(h) * ycf, 0) ' set new size
                    control send cbhndl, i, %wm_setfont, hfont, %true ' you can send any font to any control using this statement
                next
                '
                return
                '**********************************************************************************************************
                '
            case %wm_ncactivate
                static hwndsavefocus as dword
                if isfalse cbwparam then
                    ' save control focus
                    hwndsavefocus = getfocus()
                elseif hwndsavefocus then
                    ' restore control focus
                    setfocus(hwndsavefocus)
                    hwndsavefocus = 0
                end if
                '
            case %wm_paint
                '
                w = sol(stsize, 1) : h = sol(stsize, 2)
                xi = sol(prsi, 1)  : yi = sol(prsi, 2) ' previous size        '
                if paintflag = 1 then gosub resize : paintflag = 0
                hdc = beginpaint(cbhndl, ps)
                endpaint cbhndl, ps
                '
            case %wm_destroy                          ' at exit
                deleteobject hfont
                postquitmessage 0
    
            case %wm_command
                ' process control notifications
                select case as long cbctl
                    '
                    case %idc_button1 ' exit
                        if cbctlmsg = %bn_clicked or cbctlmsg = 1 then dialog end cbhndl
                    '
                    case %idc_button2 ' increase
                        if cbctlmsg = %bn_clicked or cbctlmsg = 1 then
                            if stsize < datacount / 2 then incr stsize : prsi = stsize - 1 : control enable cbhndl, %idc_button3 : paintflag = 1
                            if stsize >= datacount / 2 then messagebeep &hffffffff
                            invalidaterect cbhndl,byval %null,byval %false
                            function = 1
                        end if
                    '
                    case %idc_button3 ' decrease
                        if cbctlmsg = %bn_clicked or cbctlmsg = 1 then
                            if stsize > 1 then decr stsize : prsi = stsize + 1 : control enable cbhndl, %idc_button2 : paintflag = 1
                            if stsize <= 1 then messagebeep &hffffffff
                            invalidaterect cbhndl,byval %null,byval %false
                            function = 1
                        end if
                    '
                    case %idm_file_exit : dialog end cbhndl
                    '
                end select
        end select
    end function
    '
    function pbmain()
        initcommoncontrols
        local lrslt as long
        local hdlg  as dword
        dialog new pixels, 0, "screen resolution independence demonstration program", , , _
            547, 364, %ws_popup or %ws_border or %ws_dlgframe or %ws_sysmenu or _
            %ws_minimizebox or %ws_clipsiblings or %ws_visible or %ds_modalframe _
            or %ds_3dlook or %ds_nofailcreate or %ds_setfont , _
            %ws_ex_controlparent or %ws_ex_left or %ws_ex_ltrreading or _
            %ws_ex_rightscrollbar, to hdlg
        control add button,  hdlg, %idc_button2, "&increase", 6, 6, 78, 20
        control add button,  hdlg, %idc_button3, "&decrease", 6, 32, 78, 20
        control add button,  hdlg, %idc_button1, "e&xit", 6, 58, 78, 20
        control add label,   hdlg, %idc_label1, "screen size:", 90, 6, 198, 18
        control add label,   hdlg, %idc_label2, "screen client area:", 90, 26, _
            198, 18
        control add label,   hdlg, %idc_label3, "dialog size:", 90, 46, 198, 18
        control add label,   hdlg, %idc_label4, "font height:", 90, 65, 198, 18
        control add listbox, hdlg, %idc_listbox1, , 294, 6, 246, 130, %ws_tabstop _
            or %ws_vscroll or %lbs_nointegralheight or %lbs_disablenoscroll _
            , %ws_ex_clientedge
        control add textbox, hdlg, %idc_textbox1, ", 6, 84, 282, 254, _
            %ws_child or %ws_visible or %ws_tabstop or _
            %ws_vscroll or %es_left or %es_multiline , _
            %ws_ex_clientedge or %ws_ex_left or %ws_ex_ltrreading or _
            %ws_ex_rightscrollbar
        control add "syslistview32", hdlg, %idc_syslistview32_1, _
            "syslistview32_1", 294, 150, 246, 188, %ws_child or %ws_visible or _
            %ws_tabstop or %lvs_report or %lvs_showselalways, %ws_ex_left or _
            %ws_ex_clientedge or %ws_ex_rightscrollbar
        attachmenu1 hdlg
        dialog show modeless hdlg, call showdialog1proc
    
        ' ddt dialog, so use own message loop to trap wm_keydown..
        ' this part was inspired by code by borje hagsten at this link:
        ' http://www.powerbasic.com/support/pb...ad.php?t=23876 
    
        local msg as tagmsg, x as long, y as long
    
        while getmessage(msg, %null, 0, 0)
            if isdialogmessage(hdlg, msg) then
                select case msg.message
                    case %wm_keydown
                        select case msg.wparam
                            case %vk_left
                                dialog get loc hdlg to x, y
                                dialog set loc hdlg, x+250, y
                            case %vk_right
                                dialog get loc hdlg to x, y
                                dialog set loc hdlg, x-250, y
                            case %vk_up
                                dialog get loc hdlg to x, y
                                dialog set loc hdlg, x, y+250
                            case %vk_down
                                dialog get loc hdlg to x, y
                                dialog set loc hdlg, x, y-250
                        end select
                end select
            else
                translatemessage msg
                dispatchmessage  msg
            end if
        wend
    
    end function



    [this message has been edited by erik christensen (edited may 30, 2007).]
Working...
X