1+ # Copyright (c) 2012 CefPython Authors. All rights reserved.
2+ # License: New BSD License.
3+ # Website: http://code.google.com/p/cefpython/
4+
15import win32gui
26import win32con
37import win32api
48import time
9+ import math
510
611__debug = False
712
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+
5275def 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
6688def GetLastError ():
89+
6790 code = win32api .GetLastError ()
6891 return "(%d) %s" % (code , win32api .FormatMessage (code ))
6992
7093
7194def MessageLoop (classname ):
95+
7296 while win32gui .PumpWaitingMessages () == 0 :
7397 time .sleep (0.001 )
74- win32gui .UnregisterClass (classname , None )
7598
7699
77100if __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