This tutorial walks trough the classic minimal example of any software system, the famous 'HelloWorld' program.
Some working knowledge of WIN32 API programming is presumed.
1 from venster.windows import * 2 from venster.wtl import * 3 from venster import gdi 4 class MyWindow(Window): 5 _window_title_ = "Hello World" 6 _window_background_ = gdi.GetStockObject(WHITE_BRUSH) 7 _window_class_style_ = CS_HREDRAW | CS_VREDRAW 8 9 def OnPaint(self, event): 10 ps = PAINTSTRUCT() 11 hdc = self.BeginPaint(ps) 12 rc = self.GetClientRect() 13 14 msg = "Hello World" 15 gdi.TextOut(hdc, rc.width / 2, rc.height / 2, msg, len(msg)) 16 17 self.EndPaint(ps) 18 19 msg_handler(WM_PAINT)(OnPaint) 20 21 22 def OnDestroy(self, event): 23 PostQuitMessage(NULL) 24 25 msg_handler(WM_DESTROY)(OnDestroy) 26 27 myWindow = MyWindow() 28 29 application = Application() 30 application.Run()
1 - 3 Import the core Venster modules into the script.
4 - 7 Defines a new Venster Window. Class attributes are used to configure properties that will be common for each instance of the Window.
_window_background_causes the window background to be repainted in the specified color each time the window needs repainting
_window_class_style_is used to configure the window so that each time it is resized it will be repainted
The _window_* properties correspond to the parameters passed to the RegisterClassEx and CreateWindowEx WIN32 API methods that are used to create the window
9 - 17 Defines the handler that will be called by Windows whenever the window needs repainting.
GDI methods are used within the handler to draw the famous text
19 This decorator establishes the mapping between the WM_PAINT message send by windows and the OnPaint method that we want to be called to handle it
Please note that the decorator is compatible with Python 2.4 decorator syntax.
If you are running Python 2.4, a function decorator
@msg_handler(WM_PAINT)can be placed right atop of
22 - 25 Defines a handler that will exit the application whenever this window is destroyed.
27 Create an instance of the window
29 - 30 Create an application and run it
The Run method contains the standard WIN32 message loop (
GetMessage, TranslateMessage and DispatchMessage).
The application will stay in the Run method until the WM_QUIT message is received from Windows. This message will be send by the
PostQuitMessagecall of line 23.