msl.loadlib.activex module

Helper module for loading an ActiveX library in an application window.

Example usage,

import sys

from msl.loadlib.activex import Application, Icon, MenuGroup, MenuItem

def letter_clicked(item: MenuItem):
    print(item, item.data)
    if item.text == 'C':
        item.checked = not item.checked

def group_clicked(item: MenuItem):
    group.checked = item
    print(item)

# Create an application window
app = Application(title='My ActiveX Control', icon=Icon(sys.executable))

# Create a new 'Letters' menu
letters = app.menu.create('Letters')

# append items to the 'Letters' menu
app.menu.append(letters, 'A')
app.menu.append(letters, 'B', callback=letter_clicked, data=1)
app.menu.append_separator(letters)
app.menu.append(letters, 'C', callback=letter_clicked, data=[1, 2, 3])

# Create a new menu group
group = MenuGroup()
group.append('Group 1', callback=group_clicked)
group.append('Group 2', callback=group_clicked)
group.append('Group 3', callback=group_clicked)

# Create a new 'Numbers' menu
numbers = app.menu.create('Numbers')
# add the group to the 'Numbers' menu
app.menu.append_group(numbers, group)
# add a separator then another item
app.menu.append_separator(numbers)
app.menu.append(numbers, 'Not in Group')

# Load an ActiveX control in the main application window
# ocx = app.load('My.OCX.Application', width=300, height=300)

app.set_window_size(300, 300)

app.show()
app.run()

Classes

class msl.loadlib.activex.Application(*, background=Background.WHITE, class_style=ClassStyle.NONE, icon=None, style=WindowStyle.OVERLAPPEDWINDOW, title='ActiveX')

Bases: object

Create the main application window to display ActiveX controls.

Parameters:
  • background (int) – The background colour of the main window (a Background value).

  • class_style (int) – The class style(s). Can be any combination (bitwise OR) of ClassStyle values.

  • icon (Icon) – The application icon.

  • style (int) – The window style(s). Can be any combination (bitwise OR) of WindowStyle values.

  • title (str) – The text to display in the titlebar (if one is visible).

add_message_handler(handler)

Add a custom handler for processing window messages.

Messages correspond to events from the user and from the operating system.

Parameters:

handler (Callable[[int, int, int, int], None]) – A function that processes messages sent to the main window. The function must accept four positional arguments (all integer values) and the returned object is ignored. See WindowProc for more details about the input arguments to the handler.

Return type:

None

close()

Close the application.

Return type:

None

handle_events(source, sink=None, *, interface=None)

Handle events from an ActiveX object.

Parameters:
  • source (Any) – An ActiveX object that emits events.

  • sink (Callable[[...], Any]) – The object that handles the events. The sink must define methods with the same names as the ActiveX event names. If not specified, the Application instance is used as the sink.

  • interface (Any) – The COM interface to use.

Returns:

The advise-connection object.

property hwnd: int

Returns the handle to the main application window.

load(activex_id, *, parent=None, x=0, y=0, width=0, height=0, style=WindowStyle.CHILD | VISIBLE, ex_style=ExtendedWindowStyle.NONE)

Load an ActiveX library.

Parameters:
  • activex_id (str) – ProgID or CLSID of the ActiveX object.

  • parent (int) – The handle to the parent window that the ActiveX object will belong to. Default is the main application window.

  • x (int) – Horizontal position of the ActiveX object in the parent window.

  • y (int) – Vertical position of the ActiveX object in the parent window.

  • width (int) – Width (in pixels) of the ActiveX object.

  • height (int) – Height (in pixels) of the ActiveX object.

  • style (int) – Style of the window that is created to contain the ActiveX object. Can be any combination (bitwise OR) of WindowStyle values.

  • ex_style (int) – Extended style of the window that is created to contain the ActiveX object. Can be any combination (bitwise OR) of ExtendedWindowStyle values.

Returns:

The interface pointer to the ActiveX library.

Return type:

POINTER

property menu: Menu

Returns the menu instance.

static message_box(*, hwnd=None, language_id=0, options=MessageBoxOption.OK, text='', title='')

Displays a modal dialog box.

Parameters:
  • hwnd (int) – A handle to the owner window of the message box to be created.

  • language_id (int) – The language for the text displayed in the message box button(s).

  • options (int) – The contents and behavior of the dialog box. Can be any combination (bitwise OR) of MessageBoxOption values.

  • text (str) – The message to be displayed.

  • title (str) – The dialog box title.

Returns:

An indication of how the message box was closed.

Return type:

int

static run()

Run the application.

This is a blocking call. Create and run the application in a separate thread if you want to execute other code while the application is running.

Return type:

None

set_window_position(x, y, width, height, *, flags=PositionFlag.NONE)

Set the position of the main window.

Parameters:
  • x (int) – The new position of the left side of the window.

  • y (int) – The new position of the top of the window.

  • width (int) – The new width of the window (in pixels).

  • height (int) – The new height of the window (in pixels).

  • flags (int) – The window sizing and positioning flags. Can be any combination (bitwise OR) of PositionFlag values.

Return type:

None

set_window_size(width, height)

Set the size of the main window.

Parameters:
  • width (int) – The new width of the window (in pixels).

  • height (int) – The new height of the window (in pixels).

Return type:

None

set_window_title(title)

Set the text to display in the window’s title bar.

Parameters:

title (str) – The title bar text.

Return type:

None

show(command=ShowWindow.SHOWNORMAL)

Show the main application window.

Parameters:

command (int) – Controls how the window is shown (a ShowWindow value).

Return type:

None

property thread_id: int

Returns the identifier of the thread that created the main application window.

class msl.loadlib.activex.Icon(file, *, index=0, hinstance=None)

Bases: object

Extract an icon from an executable file, DLL or icon file.

Parameters:
  • file (str) – The path to an executable file, DLL or icon file.

  • index (int) – The zero-based index of the icon to extract.

  • hinstance (int) – Handle to the instance of the calling application.

property hicon: int | None

Returns the handle to the icon or None if no icon was found.

destroy()

Destroys the icon and frees any memory the icon occupied.

Return type:

None

class msl.loadlib.activex.Menu

Bases: object

A menu associated with the main application window.

Do not instantiate directly. Use the Application.menu property to access the menu instance.

append(hmenu, text, *, callback=None, data=None, flags=MenuFlag.ENABLED)

Create a new MenuItem and append it to a popup menu.

Parameters:
  • hmenu (int) – The handle of a popup menu to append the new menu item to.

  • text (str) – The content of the new menu item.

  • callback (Callable[[MenuItem], None] | None) – A callable object that will be called when this menu item is selected. The callable object will receive the MenuItem instance as an argument and the returned object is ignored.

  • data (Any) – User data associated with the menu item.

  • flags (int) – Controls the appearance and behaviour of the new menu item. Can be any combination (bitwise OR) of MenuFlag values.

Returns:

The menu item that was appended.

Return type:

MenuItem

append_group(hmenu, menu_group)

Append a group of menu items to a popup menu.

Parameters:
  • hmenu (int) – The handle of a popup menu to append the group to.

  • menu_group (MenuGroup) – A group of menu items.

Return type:

None

append_separator(hmenu)

Append a horizontal dividing line to a popup menu.

Parameters:

hmenu (int) – The handle to a popup menu.

Return type:

None

create(text)

Create a new popup menu and append it to the main menu.

Parameters:

text (str) – The text to display for the popup menu.

Returns:

The handle to the popup menu that was created.

Return type:

int

property hmenu: int

Returns the handle to the main menu.

class msl.loadlib.activex.MenuItem(**kwargs)

Bases: object

A menu item that belongs to a popup menu.

Do not instantiate this class directly. Use MenuGroup.append() or Menu.append() to create a new menu item.

data: Any

User data associated with the menu item.

property callback: Callable[[MenuItem], None] | None

The callback function to call when the menu item is clicked.

property checked: bool

Whether the menu item’s check mark is shown.

property flags: int

The flags that were used to create the menu item.

property hmenu: int

The handle to the popup menu that the menu item belongs to.

property id: int

The identifier of the menu item.

property text: str

The content of the menu item.

class msl.loadlib.activex.MenuGroup(name='')

Bases: object

A group of MenuItem's where only one item in the group may have a check mark to indicate that a particular item is selected.

Parameters:

name (str) – A name to associate with the group.

append(text, *, callback=None, data=None, flags=MenuFlag.ENABLED)

Create a new MenuItem and append it to the group.

Parameters:
  • text (str) – The content of the new menu item.

  • callback (Callable[[MenuItem], None] | None) – A callable object that will be called when this menu item is selected. The callable object will receive the MenuItem instance as an argument and the returned object is ignored.

  • data (Any) – User data associated with the menu item.

  • flags (int) – Controls the appearance and behaviour of the new menu item. Can be any combination (bitwise OR) of MenuFlag values.

Returns:

The menu item that was appended to the group.

Return type:

MenuItem

append_separator()

Append a horizontal dividing line to the group.

Return type:

None

property checked: MenuItem | None

Returns the menu item that is currently checked in the group.

property name: str

Returns the name of the menu group.

Enumerations

class msl.loadlib.activex.Background(value)

Bases: IntEnum

Background colours.

WHITE = 0
LIGHT_GREY = 1
GREY = 2
DARK_GREY = 3
BLACK = 4
class msl.loadlib.activex.ClassStyle(value)

Bases: IntFlag

Window class style flags. See window-class-styles for more details.

BYTEALIGNCLIENT = 0x01000
BYTEALIGNWINDOW = 0x02000
CLASSDC = 0x00040
DBLCLKS = 0x00008
DROPSHADOW = 0x20000
GLOBALCLASS = 0x04000
HREDRAW = 0x00002
NOCLOSE = 0x00200
OWNDC = 0x00020
PARENTDC = 0x00080
SAVEBITS = 0x00800
VREDRAW = 0x00001
class msl.loadlib.activex.ExtendedWindowStyle(value)

Bases: IntFlag

Extended window style flags. See extended-window-styles for more details.

DLGMODALFRAME = 0x0000001
NOPARENTNOTIFY = 0x0000004
TOPMOST = 0x0000008
ACCEPTFILES = 0x0000010
TRANSPARENT = 0x0000020
MDICHILD = 0x0000040
TOOLWINDOW = 0x0000080
WINDOWEDGE = 0x0000100
CLIENTEDGE = 0x0000200
CONTEXTHELP = 0x0000400
RIGHT = 0x0001000
RTLREADING = 0x0002000
LEFTSCROLLBAR = 0x0004000
CONTROLPARENT = 0x0010000
STATICEDGE = 0x0020000
APPWINDOW = 0x0040000
LAYERED = 0x0080000
NOINHERITLAYOUT = 0x0100000
NOREDIRECTIONBITMAP = 0x0200000
LAYOUTRTL = 0x0400000
COMPOSITED = 0x2000000
NOACTIVATE = 0x8000000
class msl.loadlib.activex.MenuFlag(value)

Bases: IntFlag

Menu item flags. See append-menu for more details.

BITMAP = 0x004
CHECKED = 0x008
DISABLED = 0x002
GRAYED = 0x001
MENUBARBREAK = 0x020
MENUBREAK = 0x040
OWNERDRAW = 0x100
POPUP = 0x010
SEPARATOR = 0x800
class msl.loadlib.activex.MessageBoxOption(value)

Bases: IntFlag

Message box options. See message-box for more details.

ABORTRETRYIGNORE = 0x000002
HELP = 0x004000
OKCANCEL = 0x000001
YESNO = 0x000004
ICONINFORMATION = 0x000040
ICONQUESTION = 0x000020
ICONSTOP = 0x000010
DEFBUTTON2 = 0x000100
DEFBUTTON3 = 0x000200
SYSTEMMODAL = 0x001000
TASKMODAL = 0x002000
DEFAULT_DESKTOP_ONLY = 0x020000
RIGHT = 0x080000
RTLREADING = 0x100000
SETFOREGROUND = 0x010000
TOPMOST = 0x040000
SERVICE_NOTIFICATION = 0x200000
class msl.loadlib.activex.PositionFlag(value)

Bases: IntFlag

Window position flags. See set-window-pos for more details.

ASYNCWINDOWPOS = 0x4000
DEFERERASE = 0x2000
DRAWFRAME = 0x0020
HIDEWINDOW = 0x0080
NOACTIVATE = 0x0010
NOCOPYBITS = 0x0100
NOMOVE = 0x0002
NOOWNERZORDER = 0x0200
NOREDRAW = 0x0008
NOSENDCHANGING = 0x0400
NOSIZE = 0x0001
NOZORDER = 0x0004
SHOWWINDOW = 0x0040
class msl.loadlib.activex.ShowWindow(value)

Bases: IntEnum

Show window commands. See show-window for more details.

HIDE = 0
SHOWNORMAL = 1
SHOWMINIMIZED = 2
SHOWMAXIMIZED = 3
SHOWNOACTIVATE = 4
SHOW = 5
MINIMIZE = 6
SHOWMINNOACTIVE = 7
SHOWNA = 8
RESTORE = 9
SHOWDEFAULT = 10
FORCEMINIMIZE = 11
class msl.loadlib.activex.WindowStyle(value)

Bases: IntFlag

Window style flags. See window-styles for more details.

POPUP = 0x80000000
CHILD = 0x40000000
MINIMIZE = 0x20000000
VISIBLE = 0x10000000
DISABLED = 0x08000000
CLIPSIBLINGS = 0x04000000
CLIPCHILDREN = 0x02000000
MAXIMIZE = 0x01000000
BORDER = 0x00800000
DLGFRAME = 0x00400000
VSCROLL = 0x00200000
HSCROLL = 0x00100000
SYSMENU = 0x00080000
THICKFRAME = 0x00040000
GROUP = 0x00020000
TABSTOP = 0x00010000