aboutsummaryrefslogtreecommitdiff
path: root/src/mingwMain/kotlin/moe/yuuta/aero/Aero.kt
blob: e9f47b477b7a284e699e718b5b87380f766eaf29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package moe.yuuta.aero

import kotlinx.cinterop.*
import platform.windows.*

fun main() {
    SetProcessDPIAware()
    val className = "Main Window Class"

    memScoped {
        val wc = cValue<WNDCLASSA> {
            lpfnWndProc = staticCFunction(::windowProc)
            this.hInstance = hInstance
            lpszClassName = className.cstr.ptr
        }
        RegisterClassA(wc.ptr)
        return@memScoped wc
    }

    val hwnd = CreateWindowExA(
        0.convert(),
        className,
        "", // Filling texts will get the white background of the label
        WS_OVERLAPPEDWINDOW.convert(),
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        null,
        null,
        WinCompat.hInstance,
        null
    )

    if (hwnd == null) {
        MessageBoxA(null,
            "The system returns an empty dialog (${GetLastError()})",
            "Aero",
            (MB_OK or MB_ICONERROR).convert())
        return
    }

    val result = DwmEnableComposition(DWM_EC_ENABLECOMPOSITION) and 0xFFFF
    if (result != 0) {
        MessageBoxA(hwnd,
            "DwmEnableComposition() fail (0x${result.toString(16)})",
            "Aero",
            (MB_OK or MB_ICONERROR).convert())
    }
    ShowWindow(hwnd, WinCompat.nCmdShow)
    WinApi.msgLoop()
}

fun windowProc(hwnd: HWND?,
               uMsg: UINT,
               wParam: WPARAM,
               lParam: LPARAM): LRESULT {
    return when (uMsg.convert<Int>()) {
        WM_CREATE -> {
            WinApi.setAero10(hwnd)
            1
        }
        WM_DESTROY -> {
            PostQuitMessage(0)
            1
        }
        WM_ACTIVATE -> {
            WinApi.setAero7(hwnd)
            1
        }
        WM_PAINT -> {
            val ps = nativeHeap.alloc<PAINTSTRUCT>()
            val hdc = BeginPaint(hwnd, ps.ptr)
            FillRect(hdc,
                ps.rcPaint.ptr,
                GetStockObject(BLACK_BRUSH) as HBRUSH)
            EndPaint(hwnd, ps.ptr)
            nativeHeap.free(ps)
            1
        }
        else -> {
            DefWindowProcA(hwnd, uMsg, wParam, lParam)
        }
    }
}