Skip to content

Commit 7f39a58

Browse files
committed
save.
1 parent b780565 commit 7f39a58

5 files changed

Lines changed: 153 additions & 111 deletions

File tree

bindings.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
# License: New BSD License.
3+
# Website: http://code.google.com/p/cefpython/
4+
15
from libcpp cimport bool
26

37
cdef extern from *:

cefexample/cefexample.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1+
# Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
# License: New BSD License.
3+
# Website: http://code.google.com/p/cefpython/
4+
15
import cefpython # cefpython.pyd
26
import cefwindow
37
import win32con # pywin32 extension
48
import win32gui
9+
import os
510

611

7-
def QuitApplication(hwnd, msg, wparam, lparam):
12+
def QuitApplication(windowID, msg, wparam, lparam):
813

9-
browser = cefpython.GetBrowserByHwnd(hwnd)
10-
cefpython.CloseBrowser(browser)
14+
browserID = windowID # yes, they are the same!
15+
cefpython.CloseBrowser(browserID)
16+
cefwindow.DestroyWindow(windowID)
1117
win32gui.PostQuitMessage(0)
1218

1319

1420
def CefExample():
21+
22+
# Programming API:
23+
# http://code.google.com/p/cefpython/wiki/API
1524

16-
# Whether to print debug output to console.
17-
cefwindow.__debug = True
25+
cefwindow.__debug = True # Whether to print debug output to console.
1826
cefpython.__debug = True
1927

2028
appSettings = {} # See: http://code.google.com/p/cefpython/wiki/AppSettings
21-
appSettings["multi_threaded_message_loop"] = False # The UI thread will be the same as the main application thread.
22-
29+
appSettings["multi_threaded_message_loop"] = False
2330
cefpython.Initialize(appSettings)
2431

25-
# 5th and 6th arguments to CreateWindow() are x/y position,
26-
# if you do not provide them the window will be centered on the screen.
2732
wndproc = {win32con.WM_CLOSE: QuitApplication}
2833
windowID = cefwindow.CreateWindow("CefExample", "cefexample", 800, 600, None, None, wndproc)
2934

3035
browserSettings = {} # See: http://code.google.com/p/cefpython/wiki/BrowserSettings
31-
browser = cefpython.CreateBrowser(windowID, browserSettings)
36+
url = "%s/cefexample.html" % os.getcwd()
37+
browserID = cefpython.CreateBrowser(windowID, browserSettings, url)
3238

3339
cefpython.MessageLoop()
3440
cefpython.Shutdown()
@@ -85,4 +91,5 @@ def LoadContentFromEncryptedZip():
8591

8692

8793
if __name__ == "__main__":
94+
8895
CefExample()

cefexample/cefwindow.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
# Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
# License: New BSD License.
3+
# Website: http://code.google.com/p/cefpython/
4+
15
import win32gui
26
import win32con
37
import win32api
48
import time
9+
import math
510

611
__debug = False
712

@@ -11,18 +16,16 @@
1116

1217
__windows = {} # windowID(int): classname
1318

14-
def CreateWindow(title, classname, width, height, x=None, y=None, wndproc=None):
19+
20+
def CreateWindow(title, classname, width, height, xpos=None, ypos=None, wndproc=None):
1521

1622
for key in __windows:
1723
if __windows[key] == classname:
1824
raise Exception("There was already created a window with that classname: %s."
1925
"Each created window must have an unique classname." % classname)
2026

2127
if not wndproc:
22-
wndproc = {
23-
win32con.WM_CLOSE: WM_CLOSE,
24-
win32con.WM_DESTROY: WM_DESTROY
25-
}
28+
wndproc = {win32con.WM_CLOSE: WM_CLOSE}
2629

2730
wndclass = win32gui.WNDCLASS()
2831
wndclass.hInstance = win32api.GetModuleHandle(None)
@@ -38,9 +41,20 @@ def CreateWindow(title, classname, width, height, x=None, y=None, wndproc=None):
3841
print "win32gui.RegisterClass(wndclass)"
3942
print "GetLastError(): %s" % GetLastError()
4043

44+
if xpos is None or ypos is None:
45+
# Center window on the screen.
46+
if __debug:
47+
print "Centering window on the screen."
48+
screenx = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
49+
screeny = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
50+
xpos = int(math.floor((screenx - width) / 2))
51+
ypos = int(math.floor((screeny - height) / 2))
52+
if xpos < 0: xpos = 0
53+
if ypos < 0: ypos = 0
54+
4155
windowID = win32gui.CreateWindow(classname, title,
4256
win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE,
43-
200, 200, 600, 400, # xpos, ypos, width, height
57+
xpos, ypos, width, height, # xpos, ypos, width, height
4458
0, 0, wndclass.hInstance, None)
4559
__windows[windowID] = classname
4660

@@ -49,31 +63,42 @@ def CreateWindow(title, classname, width, height, x=None, y=None, wndproc=None):
4963

5064
return windowID
5165

66+
67+
def DestroyWindow(windowID):
68+
69+
win32gui.DestroyWindow(windowID)
70+
classname = GetWindowClassname(windowID)
71+
win32gui.UnregisterClass(classname, None)
72+
del __windows[windowID] # Let window with this classname be created again.
73+
74+
5275
def GetWindowClassname(windowID):
76+
5377
for key in __windows:
5478
if key == windowID:
5579
return __windows[key]
5680

5781

58-
def WM_CLOSE(hwnd, msg, wparam, lparam):
59-
win32gui.DestroyWindow(hwnd)
60-
61-
62-
def WM_DESTROY(hwnd, msg, wparam, lparam):
82+
def WM_CLOSE(windowID, msg, wparam, lparam):
83+
84+
DestroyWindow(windowID)
6385
win32gui.PostQuitMessage(0)
6486

6587

6688
def GetLastError():
89+
6790
code = win32api.GetLastError()
6891
return "(%d) %s" % (code, win32api.FormatMessage(code))
6992

7093

7194
def MessageLoop(classname):
95+
7296
while win32gui.PumpWaitingMessages() == 0:
7397
time.sleep(0.001)
74-
win32gui.UnregisterClass(classname, None)
7598

7699

77100
if __name__ == "__main__":
78-
hwnd = CreateWindow("Test window", "testwindow")
101+
102+
__debug = True
103+
hwnd = CreateWindow("Test window", "testwindow", 800, 600)
79104
MessageLoop("testwindow")

0 commit comments

Comments
 (0)