Last active
November 6, 2018 02:44
-
-
Save 7kry/388bda1c8ccdaacf5bbb to your computer and use it in GitHub Desktop.
手持ちのPython 3環境で動かしたらちょっと手直しが必要だったので、まずフォーク。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# http://algomarket.wikidot.com/win32-api-in-python | |
# `Hello world in python using win32py' | |
# 手持ちのPython 3環境で動かしたらちょっと手直しが必要だったので、まずフォーク。 | |
from win32api import (GetModuleHandle,) | |
from win32gui import (WNDCLASS, | |
GetStockObject, | |
RegisterClass, | |
CreateWindow, | |
ShowWindow, | |
UpdateWindow, | |
GetMessage, | |
TranslateMessage, | |
DispatchMessage, | |
PostQuitMessage, | |
BeginPaint, | |
GetClientRect, | |
DrawText, | |
EndPaint, | |
) | |
from win32con import (WHITE_BRUSH, | |
WS_OVERLAPPEDWINDOW, | |
WS_CAPTION, | |
CW_USEDEFAULT, | |
SW_SHOW, | |
WM_PAINT, | |
WM_DESTROY, | |
CS_VREDRAW, | |
CS_HREDRAW, | |
DT_SINGLELINE, | |
DT_CENTER, | |
DT_VCENTER, | |
) | |
def OnPaint(hwnd, message, wParam, lParam): | |
hdc, ps = BeginPaint(hwnd) | |
rect = GetClientRect(hwnd) | |
int, rect = DrawText(hdc, u"Hello, Windows 98!" , -1 , rect, | |
DT_SINGLELINE | DT_CENTER | DT_VCENTER) | |
EndPaint(hwnd, ps) | |
return 0 | |
def OnDestroy(hwnd, message, wParam, lParam): | |
PostQuitMessage(0) | |
message_map = { | |
WM_PAINT: OnPaint, | |
WM_DESTROY: OnDestroy, | |
} | |
# have to create hInstance explicitly | |
hInst = GetModuleHandle() | |
# create window class structure | |
szAppName = u"HelloWin" | |
wndclass = WNDCLASS() | |
wndclass.style = CS_HREDRAW | CS_VREDRAW | |
wndclass.lpfnWndProc = message_map | |
wndclass.cbWndExtra = 0 | |
wndclass.hInstance = hInst | |
wndclass.hIcon = 0 | |
wndclass.hCursor = 0 | |
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH) | |
wndclass.lpszMenuName = '' | |
wndclass.lpszClassName = szAppName | |
# need encode! | |
# register window class | |
hr_registerclass = RegisterClass(wndclass) | |
# create window | |
hwnd = CreateWindow(szAppName, | |
u"The Hello Program", | |
WS_OVERLAPPEDWINDOW | WS_CAPTION, | |
CW_USEDEFAULT, | |
CW_USEDEFAULT, | |
CW_USEDEFAULT, | |
CW_USEDEFAULT, | |
0, | |
0, | |
hInst, | |
None) # must be None! | |
ShowWindow(hwnd, SW_SHOW) | |
UpdateWindow(hwnd) | |
# message loop | |
while True: | |
b, msg = GetMessage(hwnd, 0, 0) | |
if msg == 0: | |
break | |
TranslateMessage(msg) | |
DispatchMessage(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment