Windows Fenster erzeugen
Verfasst: Donnerstag 30. April 2020, 07:15
Hallo Forum,
ich benutze Python 3.6 für Windows (64-bit) und versuche allein mit dem Modul "ctypes" (ohne win32api etc.) ein Windows-Fenster zu erzeugen.
Das Projekt kompiliert zwar, ein Fenster wird nicht erzeugt und erscheint ebenso nicht im Taskmanager.
Was mache ich falsch?
Es ist nur ein Übungsprojekt.
ich benutze Python 3.6 für Windows (64-bit) und versuche allein mit dem Modul "ctypes" (ohne win32api etc.) ein Windows-Fenster zu erzeugen.
Das Projekt kompiliert zwar, ein Fenster wird nicht erzeugt und erscheint ebenso nicht im Taskmanager.
Was mache ich falsch?
Es ist nur ein Übungsprojekt.
Code: Alles auswählen
from ctypes import *
# Types
ATOM = c_ushort
BOOL = c_bool
DWORD = c_ulong
HANDLE = c_void_p
HBRUSH = HANDLE
HICON = HANDLE
HCURSOR = HICON
HINSTANCE = HANDLE
HMENU = HANDLE
HWND = HANDLE
INT = c_int
LONG = c_long
LONG_PTR = c_long
LPARAM = LONG_PTR
LPCWSTR = c_wchar_p
LPVOID = c_void_p
LRESULT = LONG_PTR
UINT = c_uint
UINT_PTR = c_uint
WPARAM = UINT_PTR
# Callbacks
WNDPROC = WINFUNCTYPE(LRESULT, HWND, UINT, WPARAM, LPARAM)
# Window styles
WS_BORDER = 0x00800000
WS_CAPTION = 0x00C00000
WS_VISIBLE = 0x10000000
class POINT(Structure):
_fields_ = [('x', LONG),
('y', LONG)]
class MSG(Structure):
_fields_ = [('hwnd', HWND),
('message', UINT),
('wParam', WPARAM),
('lParam', LPARAM),
('time', DWORD),
('pt', POINT)]
class WNDCLASSW(Structure):
_fields_ = [('style', UINT),
('lpfnWndProc', WNDPROC),
('cbClsExtra', INT),
('cbWndExtra', INT),
('hInstance', HINSTANCE),
('hIcon', HICON),
('hCursor', HCURSOR),
('hbrBackground', HBRUSH),
('lpszMenuName', LPCWSTR),
('lpszClassName', LPCWSTR)]
def CreateWindowW(lpClassName, lpWindowName: LPCWSTR, dwStyle: DWORD, x, y, nWidth, nHeight: INT,
hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpParam: LPVOID) -> HWND:
return windll.user32.CreateWindowExW(0, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent,
hMenu, hInstance, lpParam)
def DefWindowProcW(hWnd: HWND, Msg: UINT, wParam: WPARAM, lParam: LPARAM) -> LRESULT:
return windll.user32.DefWindowProcW(hWnd, Msg, wParam, lParam)
def DispatchMessageW(lpMsg: POINTER(MSG)) -> LRESULT:
return windll.user32.DispatchMessageW(lpMsg)
def GetMessageW(lpMsg: POINTER(MSG), hWnd: HWND, wMsgFilterMin, wMsgFilterMax: UINT) -> BOOL:
return windll.user32.GetMessageW(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax)
def RegisterClassW(lpWndClass: POINTER(WNDCLASSW)) -> ATOM:
return windll.user32.RegisterClassA(lpWndClass)
def TranslateMessage(lpMsg: POINTER(MSG)) -> BOOL:
return windll.user32.TranslateMessage(lpMsg)
"""
def PostQuitMessage(nExitCode: INT):
windll.user32.PostQuitMessage(nExitCode)
"""
def WindowProc(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM) -> LRESULT:
return DefWindowProcW(hwnd, uMsg, wParam, lParam)
def main():
wc = WNDCLASSW()
wc.lpfnWndProc = WNDPROC(WindowProc)
wc.hInstance = 0
wc.lpszClassName = "myPython"
RegisterClassW(pointer(wc))
CreateWindowW("myPython", "Python1", WS_BORDER | WS_CAPTION | WS_VISIBLE, 40, 40, 400, 400, None, None, None, None)
msg = MSG()
while GetMessageW(pointer(msg), None, None, None):
TranslateMessage(pointer(msg))
DispatchMessageW(pointer(msg))
if __name__ == "__main__":
main()