1 // =================================
 2 // Copyright (c) 2024 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Windows.API;
 8 
 9 namespace System.Windows
10 {
11     public enum StandardIconId : int
12     {
13         IDI_APPLICATION = 32512
14         IDI_ASTERISK = 32516
15         IDI_ERROR = 32513
16         IDI_EXCLAMATION = 32515
17         IDI_HAND = 32513
18         IDI_INFORMATION = 32516
19         IDI_QUESTION = 32514
20         IDI_SHIELD = 32518
21         IDI_WARNING = 32515
22         IDI_WINLOGO = 32517
23     }
24 
25     public class Icon : Resource
26     {
27         public Icon() : iconHandle(null)standard(false)
28         {
29         }
30         public Icon(void* iconHandle_) : iconHandle(iconHandle_)standard(false)
31         {
32         }
33         public Icon(void* iconHandle_bool standard_) : iconHandle(iconHandle_)standard(standard_)
34         {
35         }
36         public Icon(Icon&& that) : iconHandle(that.iconHandle)standard(that.standard)
37         {
38             that.iconHandle = null;
39             that.standard = false;
40         }
41         public ~Icon()
42         {
43             if (iconHandle != null && !standard)
44             {
45                 WinDestroyIcon(iconHandle);
46             }
47         }
48         public default void operator=(Icon&&);
49         suppress Icon(const Icon&);
50         suppress void operator=(const Icon&);
51         public void* Handle() const
52         {
53             return iconHandle;
54         }
55         public bool IsStandard() const
56         {
57             return standard;
58         }
59         public void SetStandard()
60         {
61             standard = true;
62         }
63         private void* iconHandle;
64         private bool standard;
65     }
66 
67     [nodiscard]
68     public Result<Icon> LoadStandardIcon(StandardIconId iconId)
69     {
70         void* iconHandle = WinLoadStandardIcon(cast<int>(iconId));
71         if (iconHandle == null)
72         {
73             int errorId = WinAllocateWindowsError("could not load standard icon"WinGetLastError());
74             return Result<Icon>(ErrorId(errorId));
75         }
76         return Result<Icon>(Icon(iconHandletrue));
77     }
78 
79     [nodiscard]
80     public Result<Icon> LoadIcon(const string& iconName)
81     {
82         auto iconResult = LoadIcon(iconName.Chars());
83         if (iconResult.Error())
84         {
85             return Result<Icon>(ErrorId(iconResult.GetErrorId()));
86         }
87         return Result<Icon>(Icon(iconResult.Value()));
88     }