diff --git a/hello/hello-platform/ver-windows/src/HPApp.swift b/hello/hello-platform/ver-windows/src/HPApp.swift index 3d25cbf..58159e0 100644 --- a/hello/hello-platform/ver-windows/src/HPApp.swift +++ b/hello/hello-platform/ver-windows/src/HPApp.swift @@ -8,7 +8,9 @@ struct HPApp: App { var body: some Scene { WindowGroup("Hello platform") { HPView(delegate.hpVM) + .onAppear { + delegate.setWindowIcons() + } } } } - diff --git a/hello/hello-platform/ver-windows/src/HPAppDelegate.swift b/hello/hello-platform/ver-windows/src/HPAppDelegate.swift index 574afa6..844eae6 100644 --- a/hello/hello-platform/ver-windows/src/HPAppDelegate.swift +++ b/hello/hello-platform/ver-windows/src/HPAppDelegate.swift @@ -1,4 +1,5 @@ import SwiftCrossUI +import WinSDK final class HPAppDelegate { let hp = HPComponent() @@ -8,4 +9,24 @@ final class HPAppDelegate { otherSetupConsoleLogging(hp.ctrl, "HP") hp.setup() } + + func setWindowIcons() { + // Only visit windows owned by this UI thread, never another application's window. + EnumThreadWindows(GetCurrentThreadId(), { window, _ in + let resource = UnsafePointer(bitPattern: 101) + let module = GetModuleHandleW(nil) + if let small = LoadImageW(module, resource, UINT(IMAGE_ICON), + GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), + UINT(LR_SHARED)) { + SendMessageW(window, UINT(WM_SETICON), WPARAM(ICON_SMALL), LPARAM(Int(bitPattern: small))) + } + if let large = LoadImageW(module, resource, UINT(IMAGE_ICON), + GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), + UINT(LR_SHARED)) { + SendMessageW(window, UINT(WM_SETICON), WPARAM(ICON_BIG), LPARAM(Int(bitPattern: large))) + } + // Shared resource icons remain valid for the lifetime of the process. + return 1 + }, 0) + } }