Frequently Asked Questions about Win32Lib
1. What is Win32Lib? ( a Windows routines library )
2. What do I need to know to program for Windows? (controls, attributes, events )
3. How do I make a program for Windows? (make controls, routines to handle events; or IDE)
4. How do I create a "control"? (use the "create" function)
5. What are the "attributes" of a control? (any property, like size, color, location, etc)
6. How can I see or set an attribute of a control? (use appropriate function or procedure)
7. What is an "event"? (any action able to be monitored by Windows)
8. How do I handle "events"? (link an event to a handler routine; or, use setHandler procedure)
9. What is an "event link"? (a connection between an event and a routine to handle the event)
10. How do I create an "event link"? EvntName[CtrlName]=routine_id("hndlr_routine_name")
11. Where are the "event links" PLACED in the program? (AFTER the routine to handle it)
12. What is the "setHandler" procedure? (newer way to connect an event to handler routine)
13. How to I use the "setHandler" procedure?
x. How do I write into a window?
x. How do I draw in a window?
x. How can I write data to a file, and then retrieve it to write into a window?
x. How can I use special keys like up/down arrows in windows?
x. How do I create and use a TIMER?
1. What is Win32Lib? (next) (top)
Answer: Win32Lib is a library of routines which makes a large number of Windows programming elements easily available to the programmer. It was originally created by David Cuny, and later expanded by Derek Parnell, with contributions from Matthew Lewis and others.
Using Win32Lib, you can create one or more windows, and a variety of other "controls" (like push-buttons, menus, text fields, etc.), as well as provide for the occurance of various common "events" (like clicking on a button, pressing a key, etc.).
2. What do I need to know to program for Windows? (next) (prev) (top)
Answer: Programming for Windows is somewhat different than regular Dos programming in that Windows Programming is based on the use of "controls", which have "attributes", and, it is "event" oriented. There is also a defined main processing loop.
A "control" is basically any standard visual element available to be included in the program from a pre-existing library of controls, or a non-visual element such as a timer. They include the main & other windows, push buttons, list boxes, timers, etc. You can create them, query and change their Attributes, and respond to Events which happen to them.
An "attribute" is any property a control might have, such as the size or placement of the control, its font size, or font color, etc. Various routines are provided by Win32Lib to let you inspect and alter the attributes of controls.
An "event" is any action that is available to be monitored by Windows (such as clicking on a button, or a timer reaching a preset count, etc.), which is then linked by the programmer to some routine created by the programmer to respond appropriatly to that event .
The call to the main processing loop, "WinMain", is placed at the very end of the program, after all the controls have been declared , after all the routines you have written to handle all of the events you want to respond to in your program, and after all the event links.
3.How do I make a program for Windows? (next) (prev) (top)
Answer: To make a program for Windows, you first define what you want it to do; then you:
Note: As an easier alternative to the above, there is an Integrated Development Environment program available from user contributions at RDS which utilizes Win32Lib to give Windows programmers the ability to easily create the visual elements of a Windows program by mouse clicks, like Visual Basic. It also allows event handlers to be easily created, and has a built-in editor.
Example of a Windows program using Win32Lib:
-- example4.exw
--
-- This opens a window with a single button.
-- Clicking the button closes the window.
include win32lib.ew
-- create a window, and a control (a button) in the window:
constant
TheWindow = create( Window, "Close Window", 0, Default,
Default, 200, 100, 0 ),
CloseButton = create( DefPushButton, "Close Window",
TheWindow, 40, 10, 120, 30, 0 )
-- routine that accomplishes the desired action:
global procedure onClick_CloseButton()
closeWindow( TheWindow )
end procedure
-- tell Windows when to do the action:
onClick[CloseButton] = routine_id(
"onClick_CloseButton" )
-- hand control over to Windows:
WinMain( TheWindow, Normal )
4. How do I create a "control"? (next) (prev) (top)
Answer: You make a "control" by using the
"create" function. It has the form:
constant your_control_name = create( class, title,
parent, x, y, cx, cy, flags)
class is the kind of control you
wish to create (Window, PushButton, Menu, etc).
title is the text that will appear in the
object's caption.
parent is the name of the control this
control belongs to. The parent of the main window is 0.
x and y specify
the position of the object, and cx and cy
specify the width and height.
flags are optional additional
attributes; place a 0 here if you don't know anything else to
put.
Example:
The following will create a Window called MyWindow:
-- create a window constant MyWindow = create( Window, -- the class "My Window", -- the caption 0, -- the parent Default, Default, -- x and y position 60, 40, -- width and height 0 ) -- no special flags
The following will create a PushButton in MyWindow:
-- create a pushbutton constant MyButton = create( PushButton, -- the class "Push Me!", -- the caption MyWindow, -- the parent 10, 10, -- x and y position 60, 40, -- width and height 0 ) -- no special flags
There is a list of controls in RunDemos, under "Constants", which allow a template for the creation of any of them to be copied to the clipboard, so you can paste it into your program.
5. What are the "attributes" of a control? (next) (prev) (top)
An "attribute" is any property a control might have, such as the size or placement of the control, its font size, or font color, etc. The text on a button or in a label is an attribute, as is the condition of a checkBox being checked in or not, or whether a control is visible or not, or whether it is enabled or not.
6. How can I see or set an attribute of a control? (next) (prev) (top)
Various routines are provided by Win32Lib to let you inspect and alter the attributes of controls.
Examples:
The following function gets the width and height of aButton:
sequence size
size = getCtlSize(aButton) -- size[1] = width of aButton, size[2]
= height of aButton
The following procedure sets a check mark in aCheckBox:
setCheck(aCheckBox, 1) -- a value of "0" would make the
CheckBox NOT be checked.
7. What is an "event"? (next) (prev) (top)
An "event" is any action in an
application which can be monitored by Windows, such as clicking
on a button, or a timer reaching a preset count. Some other
events are:
selecting an item in a listBox (onChange), closing or opening a
window, dragging & dropping a file or data, moving the mouse
or clicking a mouse button, changing the size of a window, and
moving a scrollbar button.
8. How do I handle "events"? (next) (prev) (top)
Answer: Events are handled by writing a statement which sets up a "link" between an "event trap" and some routine you have written to handle the event. The event handling routine you write must be placed before the event trap statement.
There are now two ways to do this. The first, original way, is with an "onSomeEvent" link statement, and the second, newer way, is with a "setHandler" event handler statement using "w32HsomeEvent" constants. You can choose whichever method you wish, though eventually the "onSomeEvent" method may be phased out from Win32Lib.
9. What is an "event link"? (next) (prev) (top)
Answer: An "event link" is a two part statement which establishes a connection between some pre-defined event and the routine which is intended to respond to the event:
Example: An event link might look like this:
onClick[CloseButton] = routine_id( "onClick_CloseButton" )
10. How do I create an "event link"? (next) (prev) (top)
Answer: To create an "event link",
It should look like this in general:
EventName[ControlName] = routine_id("event_handler_routine_name")
NOTE: it helps to make the "event_handler_routine_name" reference in some way to the event and the control, like this:
onClick[CloseButton] = routine_id( "onClick_CloseButton" )
11. Where are the "event links" PLACED in the program? (next) (prev) (top)
Answer: The "event links" must go AFTER the routine they point to.
Example 1, routine and event link together:
-- a routine to handle clicking on the
"CloseButton":
global procedure onClick_CloseButton()
closeWindow( TheWindow )
end procedure
-- an event link which tells Windows when to do what action:
onClick[CloseButton] = routine_id(
"onClick_CloseButton" )
Example 2, all event links grouped together (after the routines they point to):
<begin>
<includes and control creation here>
procedure onOpen_Window1()
-- do something
end procedure
procedure onClick_StartDisplay()
-- do something
end procedure
<all other procedures here>
--EVENTS TO RESPOND TO IN THIS PROGRAM:
----------------------------------------------------------
onOpen[Window1] = routine_id("onOpen_Window1")
onClick[StartDisplay] =
routine_id("onClick_StartDisplay")
onClick[StopDisplay] =
routine_id("StopDisplay_onClick")
onTimer[Window1] = routine_id("Window1_onTimer")
onClick[btnTestaSound] =
routine_id("onClick_btnTestaSound")
-----------------------------------------------------------
-- MAIN PROCESSING LOOP: hand control over to Windows:
WinMain( Window1, Normal )
12. What is the "setHandler" procedure? (next) (prev) (top)
Answer: The setHandler procedure is a new way to link an event with the routine written to handle the event. It looks like this:
setHandler(aControlName, a_w32Hevent, routine_id("event_handler_routine_name"))
13. How to I use the "setHandler" procedure? (prev) (top)
Answer: When you have determined that you want to handle some event, like user clicking on some button, you:
1. write a routine to do whatever you want to have happen when that button is clicked on (read a file, open another window, etc).
That routine should have a parameters list to receive two integers & a sequence,
something like this:
procedure yourRoutine( integer self, integer event, sequence params)
-- your code here, to handle the event;
--can use or test "self", "event" or "params" from parameter list above
end procedure
2. call the setHandler procedure to link your routine to the event:
setHandler( object id, object htype, integer routine) ,
where
id is the ID of the control that the event applies to.
htype is the type of event to trap.
routine is the routine_id of the user written code that will handle the event.
That should look something like this:
setHandler(HelloWin, w32HPaint, routine_id( "onPaint_HelloWin" ))
A list of the various w32H events can be found in RunDemos, under "Constants", and clicking on any w32H event there will copy the general form of "setHandler" for that event to the clipboard so you can paste it into your program.