User Tools

Site Tools


Effect of menu bar on window size

by Richard Russell, May 2006

Adding a menu bar to your output window has the effect of reducing the size of the 'client area', i.e. the area available for your program's output. So, assuming for example that your program selects MODE 8 (which would normally be 640 pixels wide by 512 pixels high), after creating the menu the available height will be reduced to something like 492 pixels.

In practice, because Windows uses the top-left corner as its reference point, it's the bottom 20 pixels or so of the window that is 'lost', and anything drawn there will not be visible (in effect the bottom left-hand corner of the visible area will correspond to approximate graphics coordinates of 0,40 rather than 0,0). You can reset the graphics origin to the bottom left corner by executing a VDU 26 but the height will still be reduced.

If you want to add a menu bar without making the client area smaller it is necessary to increase the size of the output window. There are two ways of doing that, a rough-and-ready way and a better way!

The rough-and-ready way is to assume that the menu bar will always be the same height, say 20 pixels. In that case you can simply set your window size accordingly, so to achieve the equivalent of MODE 8 (640 x 512):

        VDU 23,22,640;532;8,16,16,0
        SYS "CreateMenu" TO hmenu%
        SYS "SetMenu", @hwnd%, hmenu%
        SYS "AppendMenu", hmenu%, 0, 0, "&Menu"
        SYS "DrawMenuBar", @hwnd%
        VDU 26

However it is unwise to assume that the menu bar will always be a particular height. The better way of doing it is to use the AdjustWindowRect API function:

        GWL_STYLE = -16
        DIM rc{l%,t%,r%,b%}
        rc.r% = 640
        rc.b% = 512
        SYS "GetWindowLong", @hwnd%, GWL_STYLE TO style%
        SYS "AdjustWindowRect", rc{}, style%, 1
        SYS "SetWindowPos", @hwnd%, 0,0,0, rc.r%-rc.l%, rc.b%-rc.t%, 6
        SYS "CreateMenu" TO hmenu%
        SYS "SetMenu", @hwnd%, hmenu%
        SYS "AppendMenu", hmenu%, 0, 0, "&Menu"
        SYS "DrawMenuBar", @hwnd%
        VDU 4,20,26,12

An extended version of this code which also takes into account the presence of a toolbar and/or status bar, and the effect of different DPI (dots per inch) values, can be found here.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
effect_20of_20menu_20bar_20on_20window_20size.txt · Last modified: 2018/04/16 14:23 by richardrussell