Skip to content

Instantly share code, notes, and snippets.

@zelid
Created September 3, 2016 15:49
Show Gist options
  • Save zelid/831d8e59d5bbe86324aa056ffe3cbb5d to your computer and use it in GitHub Desktop.
Save zelid/831d8e59d5bbe86324aa056ffe3cbb5d to your computer and use it in GitHub Desktop.
import wx
import win32api
import win32gui
import win32con
from sciter import sapi
from sciter.capi.scdef import *
# install wxPython as: 'pip install --upgrade --trusted-host wxpython.org --pre -f http://wxpython.org/Phoenix/snapshot-builds/ wxPython_Phoenix'
# hooking windproc:
# https://wiki.wxpython.org/HookingTheWndProc
# https://wiki.wxpython.org/MonitoringWindowsUsb
# other:
# https://github.com/KellyZ/USB-Clipboard/blob/master/clipboard.py
# https://github.com/rene-aguirre/pywinusb/blob/master/pywinusb/hid/wnd_hook_mixin.py
def on_load_data(ld):
"""Custom documents loader, just for example."""
uri = ld.uri
uri = uri
return 0
def on_create_behavior(ld):
"""Custom behavior factory, just for example."""
name = ld.behaviorName
name = name
return 0
def on_sciter_callback(pld, param):
"""Sciter notifications callback."""
ld = pld.contents
if ld.code == SciterNotification.SC_LOAD_DATA:
return on_load_data(cast(pld, POINTER(SCN_LOAD_DATA)).contents)
elif ld.code == SciterNotification.SC_ATTACH_BEHAVIOR:
return on_create_behavior(cast(pld, POINTER(SCN_ATTACH_BEHAVIOR)).contents)
return 0
class TestFrame(wx.Frame):
def __init__(self):
clsname = sapi.SciterClassName()
wx.Frame.__init__(self, None, title="WndProc Test", size=(200, 150))
# p = wx.Panel(self)
# Set the WndProc to our function
self.oldWndProc = win32gui.SetWindowLong(self.GetHandle(),
win32con.GWL_WNDPROC,
self.MyWndProc)
# Make a dictionary of message names to be used for printing below
self.msgdict = {}
for name in dir(win32con):
if name.startswith("WM_"):
value = getattr(win32con, name)
self.msgdict[value] = name
# init sciter
hWnd = self.GetHandle()
scproc = SciterHostCallback(on_sciter_callback)
sapi.SciterSetCallback(hWnd, scproc, None)
url = u"minimal.htm"
sapi.SciterLoadFile(hWnd, url)
def MyWndProc(self, hWnd, msg, wParam, lParam):
"""WindowProc Function."""
handled = BOOL(0)
lr = sapi.SciterProcND(hWnd, msg, wParam, lParam, byref(handled))
if handled:
return lr
# Display what we've got.
print(self.msgdict.get(msg), msg, wParam, lParam)
if msg == win32con.WM_DESTROY:
windll.user32.PostQuitMessage(0)
return 0
try:
return windll.user32.DefWindowProcW(hWnd, msg, wParam, lParam)
except:
# etype, evalue, estack = sys.exc_info()
print("WndProc exception: %X, 0x%04X, 0x%X, 0x%X" % (hWnd, msg, wParam, lParam))
# traceback.print_exception(etype, evalue, estack)
return 0
app = wx.PySimpleApp()
f = TestFrame()
f.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment