#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+#
# PYTHON LESSION 1 WINDOWS API   UNDER CONSTRUCTION!!!!!!!!
# V0.9 05.07.2023 Auth. VODIMAN
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+#
import ctypes
import ctypes.wintypes

# Windows API Funktionen und Konstanten importieren
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

WNDPROC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_int, ctypes.c_int)

# Callback-Funktion für das Fenster
def wndproc(hwnd, msg, wParam, lParam):
    if msg == 0x10:  # WM_CLOSE
        user32.DestroyWindow(hwnd)
        return 0
    elif msg == 0x100:  # WM_KEYDOWN
        if wParam == 0x0D:  # Enter-Taste
            buffer = ctypes.create_unicode_buffer(256)
            user32.GetWindowTextW(edit, buffer, ctypes.sizeof(buffer))
            message = "Eingegebener Text: " + buffer.value
            user32.MessageBoxW(None, message, "Ausgabe", 0)
            return 0

    return user32.DefWindowProcW(hwnd, msg, wParam, lParam)

# Fensterklasse registrieren
#WNDCLASS = ctypes.Structure
#WNDCLASS._fields_ = [
class WNDCLASS(ctypes.Structure):_fields_ = [
    ("style", ctypes.c_uint),
    ("lpfnWndProc", WNDPROC),
    ("cbClsExtra", ctypes.c_int),
    ("cbWndExtra", ctypes.c_int),
    ("hInstance", ctypes.wintypes.HINSTANCE),
    ("hIcon", ctypes.wintypes.HICON),
    ("hCursor", ctypes.wintypes.HANDLE),
    ("hbrBackground", ctypes.wintypes.HBRUSH),
    ("lpszMenuName", ctypes.c_wchar_p),
    ("lpszClassName", ctypes.c_wchar_p)
]

wnd_class = WNDCLASS.__new__(WNDCLASS)
wnd_class.style = 0
wnd_class.lpfnWndProc = WNDPROC(wndproc)
wnd_class.cbClsExtra = 0
wnd_class.cbWndExtra = 0
wnd_class.hInstance = kernel32.GetModuleHandleW(None)
wnd_class.hIcon = None
wnd_class.hCursor = user32.LoadCursorW(None, 32512)  # IDC_ARROW
wnd_class.hbrBackground = ctypes.c_void_p(5)  # COLOR_WINDOW + 1
wnd_class.lpszMenuName = None
wnd_class.lpszClassName = "MyWindowClass"
wnd_class.cbSize = ctypes.sizeof(WNDCLASS)

user32.RegisterClassW(ctypes.byref(wnd_class))

# Fenster erstellen
hwnd = user32.CreateWindowExW(
    0,
    wnd_class.lpszClassName,
    "Python Window",
    0x00CF0000,  # WS_OVERLAPPEDWINDOW | WS_VISIBLE
    ctypes.c_int(100),
    ctypes.c_int(100),
    ctypes.c_int(400),
    ctypes.c_int(200),
    None,
    None,
    None,
    #wnd_class.hInstance,
    None
)

# Texteingabefeld erstellen
edit = user32.CreateWindowExW(
    0,
    "EDIT",
    "",
    0x50210000,  # WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
    ctypes.c_int(10),
    ctypes.c_int(10),
    ctypes.c_int(380),
    ctypes.c_int(20),
    hwnd,
    None,
    #wnd_class.hInstance,
    None,
    None
)

# Schleife für das Fenster-Handling
msg = ctypes.wintypes.MSG()
while user32.GetMessageW(ctypes.byref(msg), None, 0, 0) > 0:
    user32.TranslateMessage(ctypes.byref(msg))
    user32.DispatchMessageW(ctypes.byref(msg))
