Skip to content

Commit be95517

Browse files
committed
pywin32 example is working
1 parent a7661ba commit be95517

1 file changed

Lines changed: 48 additions & 46 deletions

File tree

examples/pywin32.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from cefpython3 import cefpython as cef
55

66
import distutils.sysconfig
7+
import math
78
import os
89
import platform
910
import sys
@@ -23,12 +24,6 @@ def main():
2324
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
2425
cef.Initialize()
2526
pyWin32Example()
26-
"""
27-
if g_message_loop == MESSAGE_LOOP_CEF:
28-
cef.MessageLoop()
29-
else:
30-
gtk.main()
31-
"""
3227
cef.Shutdown()
3328

3429

@@ -40,61 +35,68 @@ def check_versions():
4035

4136

4237
def pyWin32Example():
43-
pass
44-
45-
46-
def CefAdvanced():
47-
sys.excepthook = ExceptHook
48-
49-
appSettings = dict()
50-
# appSettings["cache_path"] = "webcache/" # Disk cache
51-
if DEBUG:
52-
# cefpython debug messages in console and in log_file
53-
appSettings["debug"] = True
54-
cefwindow.g_debug = True
55-
appSettings["log_file"] = GetApplicationPath("debug.log")
56-
appSettings["log_severity"] = cefpython.LOGSEVERITY_INFO
57-
appSettings["release_dcheck_enabled"] = True # Enable only when debugging
58-
appSettings["browser_subprocess_path"] = "%s/%s" % (
59-
cefpython.GetModuleDirectory(), "subprocess")
60-
cefpython.Initialize(appSettings)
38+
39+
cef.Initialize()
6140

6241
wndproc = {
6342
win32con.WM_CLOSE: CloseWindow,
6443
win32con.WM_DESTROY: QuitApplication,
65-
win32con.WM_SIZE: cefpython.WindowUtils.OnSize,
66-
win32con.WM_SETFOCUS: cefpython.WindowUtils.OnSetFocus,
67-
win32con.WM_ERASEBKGND: cefpython.WindowUtils.OnEraseBackground
44+
win32con.WM_SIZE: WindowUtils.OnSize,
45+
win32con.WM_SETFOCUS: WindowUtils.OnSetFocus,
46+
win32con.WM_ERASEBKGND: WindowUtils.OnEraseBackground
6847
}
69-
70-
browserSettings = dict()
71-
browserSettings["universal_access_from_file_urls_allowed"] = True
72-
browserSettings["file_access_from_file_urls_allowed"] = True
73-
74-
if os.path.exists("icon.ico"):
75-
icon = os.path.abspath("icon.ico")
76-
else:
77-
icon = ""
78-
79-
windowHandle = cefwindow.CreateWindow(title="pywin32 example",
80-
className="cefpython3_example", width=1024, height=768,
81-
icon=icon, windowProc=wndproc)
82-
windowInfo = cefpython.WindowInfo()
48+
49+
windowHandle = CreateWindow(title="pywin32 example", className="cefpython3_example", width=1024, height=768, windowProc=wndproc)
50+
51+
windowInfo = cef.WindowInfo()
8352
windowInfo.SetAsChild(windowHandle)
84-
browser = cefpython.CreateBrowserSync(windowInfo, browserSettings,
85-
navigateUrl=GetApplicationPath("example.html"))
86-
cefpython.MessageLoop()
87-
cefpython.Shutdown()
53+
browser = cef.CreateBrowserSync(windowInfo, settings={},
54+
url="https://www.google.com/")
55+
cef.MessageLoop()
56+
cef.Shutdown()
57+
8858

8959
def CloseWindow(windowHandle, message, wparam, lparam):
90-
browser = cefpython.GetBrowserByWindowHandle(windowHandle)
60+
browser = cef.GetBrowserByWindowHandle(windowHandle)
9161
browser.CloseBrowser()
9262
return win32gui.DefWindowProc(windowHandle, message, wparam, lparam)
9363

64+
9465
def QuitApplication(windowHandle, message, wparam, lparam):
9566
win32gui.PostQuitMessage(0)
9667
return 0
9768

69+
70+
def CreateWindow(title, className, width, height, windowProc):
71+
72+
wndclass = win32gui.WNDCLASS()
73+
wndclass.hInstance = win32api.GetModuleHandle(None)
74+
wndclass.lpszClassName = className
75+
wndclass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
76+
# win32con.CS_GLOBALCLASS
77+
wndclass.hbrBackground = win32con.COLOR_WINDOW
78+
wndclass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
79+
wndclass.lpfnWndProc = windowProc
80+
atomClass = win32gui.RegisterClass(wndclass)
81+
assert(atomClass != 0)
82+
83+
# Center window on the screen.
84+
screenx = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
85+
screeny = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
86+
xpos = int(math.floor((screenx - width) / 2))
87+
ypos = int(math.floor((screeny - height) / 2))
88+
if xpos < 0: xpos = 0
89+
if ypos < 0: ypos = 0
90+
91+
windowHandle = win32gui.CreateWindow(className, title,
92+
win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE,
93+
xpos, ypos, width, height, # xpos, ypos, width, height
94+
0, 0, wndclass.hInstance, None)
95+
96+
assert(windowHandle != 0)
97+
return windowHandle
98+
99+
98100
def GetPywin32Version():
99101
pth = distutils.sysconfig.get_python_lib(plat_specific=1)
100102
ver = open(os.path.join(pth, "pywin32.version.txt")).read().strip()

0 commit comments

Comments
 (0)