|
|
wxLua Hello WorldSummary
Output CodeDownloadif not wx then require("wx") end -- (optional) explicitly load wxLua libraries
-- The "frame" is the main window
local my_frame = wx.wxFrame(wx.NULL,
wx.wxID_ANY,
"this is the title of the HelloWorld (tm) application",
wx.wxDefaultPosition,
wx.wxSize(640, 200), -- initial size of the window
wx.wxDEFAULT_FRAME_STYLE
)
-- note, the frame is invisible unless shown.
-- The "panel" is a region inside the frame. By itself it is invisible.
local my_panel = wx.wxPanel(my_frame, wx.wxID_ANY)
-- The callback function will be called from wxLua
function my_redraw_callback(event)
-- "my_dc" is a so-called "drawing context".
-- This one is obtained from the panel.
-- If we'd get one for a bitmap instead, we could save the window contents as image.
-- Or similar for a printer dc to get the contents on paper.
local my_dc = wx.wxPaintDC(my_panel)
-- the "pen" draws the border of the rectangle
local my_pen=wx.wxPen("black", 13, wx.wxLONG_DASH)
-- the "brush" fills the area of the rectangle
local my_brush=wx.wxBrush("yellow", wx.wxSOLID)
-- set pen / brush on the device context
my_dc:SetPen(my_pen)
my_dc:SetBrush(my_brush, wx.wxSOLID)
-- request the size of the panel, because we don't know the menu bar, window borders etc.
local my_size=my_panel:GetSize()
my_dc:DrawRectangle(10, 10, my_size:GetWidth()-20, my_size:GetHeight()-20) -- x, y, width, height
my_dc:DrawText("Hello World", 200, 100);
-- need to explicitly delete a dc when it is not needed anymore.
my_dc:delete()
end
-- Tell wxlua to call "my_redraw_callback", whenever "my_panel" requires a redraw
my_panel:Connect(wx.wxEVT_PAINT, my_redraw_callback)
-- Show the window
my_frame:Show(true)
wx.wxGetApp():MainLoop() -- (optional) explicitly hand over control to wxLua
DiscussionFirst, a “frame” is created. The top-level frame is the window.Inside the frame, a “panel” is added. The panel is an area within its parent. A callback function is created, and bound to the “paint” event on the panel: Every time the window needs a redraw, the callback function is called by wxLua from the main loop. The window is shown, and control is handed over to wxLua's main loop. Control returns, when the last window disappears from the screen. © Markus Nentwig 2007-2008 The content of this page is provided without any warranty and may not be reproduced without permission. Comments? Questions?Please send me a mail! mnentwig@elisanet.fi |