| 119 | |
| 120 | |
| 121 | def create_window(title, class_name, width, height, window_proc, icon): |
| 122 | # Register window class |
| 123 | wndclass = win32gui.WNDCLASS() |
| 124 | wndclass.hInstance = win32api.GetModuleHandle(None) |
| 125 | wndclass.lpszClassName = class_name |
| 126 | wndclass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW |
| 127 | wndclass.hbrBackground = win32con.COLOR_WINDOW |
| 128 | wndclass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) |
| 129 | wndclass.lpfnWndProc = window_proc |
| 130 | atom_class = win32gui.RegisterClass(wndclass) |
| 131 | assert(atom_class != 0) |
| 132 | |
| 133 | # Center window on screen. |
| 134 | screenx = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) |
| 135 | screeny = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) |
| 136 | xpos = int(math.floor((screenx - width) / 2)) |
| 137 | ypos = int(math.floor((screeny - height) / 2)) |
| 138 | if xpos < 0: |
| 139 | xpos = 0 |
| 140 | if ypos < 0: |
| 141 | ypos = 0 |
| 142 | |
| 143 | # Create window |
| 144 | window_style = (win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN |
| 145 | | win32con.WS_VISIBLE) |
| 146 | window_handle = win32gui.CreateWindow(class_name, title, window_style, |
| 147 | xpos, ypos, width, height, |
| 148 | 0, 0, wndclass.hInstance, None) |
| 149 | assert(window_handle != 0) |
| 150 | |
| 151 | # Window icon |
| 152 | icon = os.path.abspath(icon) |
| 153 | if not os.path.isfile(icon): |
| 154 | icon = None |
| 155 | if icon: |
| 156 | # Load small and big icon. |
| 157 | # WNDCLASSEX (along with hIconSm) is not supported by pywin32, |
| 158 | # we need to use WM_SETICON message after window creation. |
| 159 | # Ref: |
| 160 | # 1. http://stackoverflow.com/questions/2234988 |
| 161 | # 2. http://blog.barthe.ph/2009/07/17/wmseticon/ |
| 162 | bigx = win32api.GetSystemMetrics(win32con.SM_CXICON) |
| 163 | bigy = win32api.GetSystemMetrics(win32con.SM_CYICON) |
| 164 | big_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, |
| 165 | bigx, bigy, |
| 166 | win32con.LR_LOADFROMFILE) |
| 167 | smallx = win32api.GetSystemMetrics(win32con.SM_CXSMICON) |
| 168 | smally = win32api.GetSystemMetrics(win32con.SM_CYSMICON) |
| 169 | small_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, |
| 170 | smallx, smally, |
| 171 | win32con.LR_LOADFROMFILE) |
| 172 | win32api.SendMessage(window_handle, win32con.WM_SETICON, |
| 173 | win32con.ICON_BIG, big_icon) |
| 174 | win32api.SendMessage(window_handle, win32con.WM_SETICON, |
| 175 | win32con.ICON_SMALL, small_icon) |
| 176 | |
| 177 | return window_handle |
| 178 | |