# -*- mode: nim; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2026/03/03 03:38:23 +0900> # # winim/lean を使ってWindows上で空ウインドウを生成 # Google Geminiに生成してもらった # # Windows11 x64 25H2 + Nim 2.2.8 64bit import winim/lean # アプリの状態を保持する型(今回はウィンドウハンドルのみ) type App = object hwnd: HWND # --- 1. ウィンドウプロシージャ(イベント処理) --- proc windowProc( hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM ): LRESULT {.stdcall.} = case uMsg of WM_DESTROY: PostQuitMessage(0) return 0 of WM_KEYDOWN: if wParam == VK_ESCAPE: # ESCキーで終了 PostMessage(hwnd, WM_CLOSE, 0, 0) # DestroyWindow(hwnd) return 0 else: return DefWindowProc(hwnd, uMsg, wParam, lParam) # --- 2. ウィンドウの生成メソッド --- proc initApp(title: string, width, height: int): App = let hInstance = GetModuleHandle(nil) let className = "NimSimpleWindow" var wc: WNDCLASS wc.lpfnWndProc = windowProc wc.hInstance = hInstance wc.lpszClassName = className wc.hCursor = LoadCursor(0, IDC_ARROW) wc.hbrBackground = COLOR_WINDOW + 1 RegisterClass(addr wc) let hwnd = CreateWindowEx( 0, className, title, WS_OVERLAPPEDWINDOW or WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, width.int32, height.int32, 0, 0, hInstance, nil, ) # 作成したハンドルを App 型に包んで返す result = App(hwnd: hwnd) # --- 3. メインループメソッド --- proc run(app: App) = var msg: MSG # GetMessageは WM_QUIT を受け取ると 0 を返し、ループが終了する while GetMessage(addr msg, 0, 0, 0) != 0: TranslateMessage(addr msg) DispatchMessage(addr msg) proc main() = let myApp = initApp("Nim Win32 Application", 640, 480) if myApp.hwnd == 0: MessageBox(0, "Failed to create the application window.", "Fatal Error", MB_OK or MB_ICONERROR) return run(myApp) main()