API Corner

Modifying Liberty BASIC Controls

Home

Game Tutorial

Pseudo Menubar

Binary Numbers

Designing Games

API Corner

JPEG.DLL

Graphics Text

Tip Corner

Installers

Demos

Submission Guide

Newsletter help

Index


API Zone... Beware!

It is best to understand the use of API calls in Liberty BASIC before using this technique. Earlier issues of the newsletter include tutorials for calling the Windows API from Liberty BASIC, and for creating an manipulating controls with API calls. This method is meant for the more experienced Liberty BASIC programmer.

Controls limitations in Liberty BASIC

Liberty BASIC includes all of the basic Windows controls. They are very easy to create and to use. There are some limitations, though. Some controls are not available with native LB commands. These include statusbars, toolbars, line-wrapping texteditors, buttons with multiline captions, statictext with text that isn't left-aligned, tab controls and others. In some cases, it is necessary to create controls with API calls to get around these limitations. In other cases, it is possible to create controls from other controls - for instance, a fake toolbar can be created using bmpbuttons and a statusbar can be simulated with a graphicbox or statictext control. See the Pseudo Menubar article in this issue for an example that uses a graphicbox to simulate a menubar.

Modifying Controls

It is also possible to use a Liberty BASIC control and modify the style of the control. This requires three steps.

  1. Get the style bits of the control into a variable with GetWindowLongA and a flag of _GWL_STYLE
  2. Modify the style by putting the bits together with bitwise operators OR, XOR, depending on whether a style bit is to be added or removed from the original style bits. For instance, a right-aligned statictext is created when the _SS_RIGHT style is OR'd with the original style.
  3. Give the control the modified style with SetWindowLongA

API Control Information

The style constants used in Windows controls are part of Liberty BASIC. To tell Liberty BASIC that a constant is a defined Windows constant, begin it with an underscore character. The Windows style constant "SS_RIGHT" is expressed in Liberty BASIC as "_SS_RIGHT". For a list of constants for the style bits of controls, see this website: [http://www.mentalis.org/apilist/CreateWindowEx.shtml] The styles are documented in the CreateWindowExA function, and there are quite a lot of them. To discover the possibilities, read the documentation at the URL above, or try the Microsoft Developers Network [http://msdn.microsoft.com/] The MSDN Library is here: [http://msdn.microsoft.com/library/default.asp]

Get a free utility to view the list of Windows API functions, constants, and structs here: [http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html]

Adding or Subtracting Style Bits

To ADD a style to the existing control style, it is put together with the existing style with the OR operator. To add the style bit that causes statictext to be aligned to the right:

    newStyle = origStyle OR _SS_RIGHT

To remove a style bit, it is put together with the existing style with the XOR operator. For instance, to remove a border from a control, the original style would be XOR'd with _WS_BORDER:

    newStyle = origStyle XOR _WS_BORDER

The Demos

In the following demos, Mike Bradbury modifies a button so that the caption wraps to produce multiple lines, and Brent Thorn modifies a statictext control so that the caption is right-aligned.


Multiline Button Caption - by Mike Bradbury

'Mike
'mike@karemi.fsnet.co.uk

nomainwin
WindowWidth=300:WindowHeight=150
caption$="If you are 20 and have green hair and a tail Click me. (Is that you Darrell?)"
button #1.b1, caption$,[b1],UL,20,20,100,80

open "button styles" for window as #1
#1, "trapclose [quit]"

'change font for button1, which has a long label
#1.b1, "!font arial 7"
hW=hwnd(#1)

h1=hwnd(#1.b1)
modifyStyle=_BS_CENTER or _BS_MULTILINE
r=changeStyle(h1,modifyStyle)
#1 "refresh"
wait
[b1]
wait
[quit]
close#1
end

function changeStyle(h,ms)
    calldll #user32,"GetWindowLongA",h as long,_
    _GWL_STYLE as long,style as long
    style=style or ms
    calldll #user32,"SetWindowLongA",_
    h as long,_GWL_STYLE as long,style as long, _
    changeStyle as long
end function


Right-aligned Statictext - by Brent Thorn

'Brent Thorn, 2003
'b6sw@yahoo.com
'lbgui@aol.com
'http://members.aol.com/b6sw/
    nomainwin
    statictext #1,"Left",10,10,100,20
    statictext #1.st,"",10,30,100,20
    open "SS_RIGHT Demo" for window as #1
    #1 "trapclose [quit]"
    h=hwnd(#1.st)
    calldll #user32,"GetWindowLongA",_
        h as long,_GWL_STYLE as long,_
        style as long
    style=style or _SS_RIGHT
    calldll #user32,"SetWindowLongA",_
        h as long,_GWL_STYLE as long,_
        style as long,r as long
    #1.st "Right"
    wait
[quit]
    close #1
    end


Home

Game Tutorial

Pseudo Menubar

Binary Numbers

Designing Games

API Corner

JPEG.DLL

Graphics Text

Tip Corner

Installers

Demos

Submission Guide

Newsletter help

Index