如何使用Python clr WinForms挂钩到WndProc

时间:2017-11-02 03:38:05

标签: python winforms clr wndproc

我的最终目标是使用pywebview建立一个可调整大小的无边界窗口。我正在使用Windows和WinForms。 通过这样做,我能够使它无边框:

webview.winforms.BrowserView.instance.browser.FormBorderStyle = 0

但是,我希望能够移动它并调整它的大小。为了在C#中正常执行此操作,我可以覆盖派生的Form类中的WndProc方法并调用resize消息。我不确定如何在Python中执行此操作。 pywebview中的表格是defined here

我无法将self.WndProc设置为我自己的定义;我得到AttributeError : attribute is read-only。我试着像这样直接挂钩(在表单定义中):

WndProcType = ctypes.WINFUNCTYPE(c_int, c_long, c_int, c_int, c_int)
GWL_WNDPROC = -4
handle = windll.kernel32.GetModuleHandleW(None)
self.oldWndProc = self.WndProc
ctypes.windll.user32.SetWindowLongW(handle, GWL_WNDPROC, WndProcType(self.MyWndProc))
def MyWndProc(self, hWnd, msg, wParam, lParam):
    print ("is it working?")
    return CallWindowProc(
        self.oldWndProc,
        hWnd,
        msg,
        wParam,
        lParam
    )

但它似乎没有被召唤。

1 个答案:

答案 0 :(得分:0)

GetModuleHandle检索应用程序模块句柄,而不是窗口句柄。 您需要从CreateWindow / GetActiveWindow返回的需要句柄或返回窗口句柄的另一个函数!

尝试一下:

ctypes.windll.user32.SetWindowLongW(self.Handle, GWL_WNDPROC, WndProcType(self.MyWndProc))