// Copyright 2010 The Walk Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build windows package lib import ( "syscall" "unsafe" "github.com/lxn/win" ) var ( registeredWindowClasses = make(map[string]bool) defaultWndProcPtr = syscall.NewCallback(defaultWndProc) ) // MustRegisterWindowClass registers the specified window class. // // MustRegisterWindowClass must be called once for every window type that is not // based on any system provided control, before calling InitChildWidget or // InitWidget. Calling MustRegisterWindowClass twice with the same className // results in a panic. func MustRegisterWindowClass(className string) { MustRegisterWindowClassWithWndProcPtr(className, defaultWndProcPtr) } func MustRegisterWindowClassWithStyle(className string, style uint32) { MustRegisterWindowClassWithWndProcPtrAndStyle(className, defaultWndProcPtr, style) } func MustRegisterWindowClassWithWndProcPtr(className string, wndProcPtr uintptr) { MustRegisterWindowClassWithWndProcPtrAndStyle(className, wndProcPtr, 0) } func MustRegisterWindowClassWithWndProcPtrAndStyle(className string, wndProcPtr uintptr, style uint32) { if registeredWindowClasses[className] { panic("window class already registered") } hInst := win.GetModuleHandle(nil) if hInst == 0 { panic("GetModuleHandle") } hIcon := win.LoadIcon(hInst, win.MAKEINTRESOURCE(7)) // rsrc uses 7 for app icon if hIcon == 0 { hIcon = win.LoadIcon(0, win.MAKEINTRESOURCE(win.IDI_APPLICATION)) } if hIcon == 0 { panic("LoadIcon") } hCursor := win.LoadCursor(0, win.MAKEINTRESOURCE(win.IDC_ARROW)) if hCursor == 0 { panic("LoadCursor") } var wc win.WNDCLASSEX wc.CbSize = uint32(unsafe.Sizeof(wc)) wc.LpfnWndProc = wndProcPtr wc.HInstance = hInst wc.HIcon = hIcon wc.HCursor = hCursor wc.HbrBackground = win.COLOR_BTNFACE + 1 wc.LpszClassName = syscall.StringToUTF16Ptr(className) wc.Style = style if atom := win.RegisterClassEx(&wc); atom == 0 { panic("RegisterClassEx") } registeredWindowClasses[className] = true } func defaultWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) { return win.DefWindowProc(hwnd, msg, wParam, lParam) }