hwnd/um/winuser/functions/load_icon.rs
1use crate::*;
2use winapi::um::winuser::*;
3
4
5
6/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadicona)\]
7/// LoadIconA
8///
9/// Loads an icon, animated icon, or bitmap.
10///
11/// ### Errors
12/// * [ERROR::RESOURCE_DATA_NOT_FOUND] if `icon_name` cannot be found for `Some(hinstance)`
13/// * ~~[ERROR::RESOURCE_NAME_NOT_FOUND]~~ never? (returned by [load_cursor_a])
14/// * [ERROR::RESOURCE_TYPE_NOT_FOUND] if `icon_name` cannot be found for the system (e.g. `hinstance` is `None`)
15///
16/// ### Example
17/// ```rust
18/// # use abistr::*;
19/// # use hwnd::*;
20/// # use winresult::*;
21/// let idi_error = load_icon_a(None, IDI::ERROR).unwrap();
22///
23/// let exe = get_module_handle_entry_exe().unwrap();
24/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_a(None, IDC::SIZE).unwrap_err(), "cursor-only atom to load_icon");
25/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_a(None, 42).unwrap_err(), "(None, 42)");
26/// assert_eq!(ERROR::RESOURCE_DATA_NOT_FOUND, load_icon_a(exe, 42).unwrap_err(), "(exe, 42)");
27/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_a(None, cstr!("nonexistant")).unwrap_err(), "(None, \"nonexistant\")");
28/// assert_eq!(ERROR::RESOURCE_DATA_NOT_FOUND, load_icon_a(exe, cstr!("nonexistant")).unwrap_err(), "(exe, \"nonexistant\")");
29/// ```
30///
31/// ### See Also
32/// * [load_icon_w] — wide equivalent
33/// * [load_cursor_a] — cursor equivalent
34pub fn load_icon_a<'h, 't>(hinstance: impl Into<HInstance<'h>>, icon_name: impl Into<NameAtomOrZero<'t, u8>>) -> Result<HIcon<'h>, Error> {
35 fn_context!(load_icon_a => LoadIconA);
36 let hicon = unsafe { LoadIconA(hinstance.into().into(), icon_name.into().as_atom_or_cstr_ptr()) };
37 fn_succeeded!(!hicon.is_null())?;
38 Ok(unsafe { HIcon::from_unchecked(hicon) })
39}
40
41/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadiconw)\]
42/// LoadIconW
43///
44/// Loads an icon, animated icon, or bitmap.
45///
46/// ### Errors
47/// * [ERROR::RESOURCE_DATA_NOT_FOUND] if `icon_name` cannot be found for `Some(hinstance)`
48/// * ~~[ERROR::RESOURCE_NAME_NOT_FOUND]~~ never? (returned by [load_cursor_w])
49/// * [ERROR::RESOURCE_TYPE_NOT_FOUND] if `icon_name` cannot be found for the system (e.g. `hinstance` is `None`)
50///
51/// ### Example
52/// ```rust
53/// # use abistr::*;
54/// # use hwnd::*;
55/// # use winresult::*;
56/// let idi_error = load_icon_w(None, IDI::ERROR).unwrap();
57///
58/// let exe = get_module_handle_entry_exe().unwrap();
59/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_w(None, IDC::SIZE).unwrap_err(), "cursor-only atom to load_icon");
60/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_w(None, 42).unwrap_err(), "(None, 42)");
61/// assert_eq!(ERROR::RESOURCE_DATA_NOT_FOUND, load_icon_w(exe, 42).unwrap_err(), "(exe, 42)");
62/// assert_eq!(ERROR::RESOURCE_TYPE_NOT_FOUND, load_icon_w(None, cstr16!("nonexistant")).unwrap_err(), "(None, \"nonexistant\")");
63/// assert_eq!(ERROR::RESOURCE_DATA_NOT_FOUND, load_icon_w(exe, cstr16!("nonexistant")).unwrap_err(), "(exe, \"nonexistant\")");
64/// ```
65///
66/// ### See Also
67/// * [load_icon_a] — narrow equivalent
68/// * [load_cursor_w] — cursor equivalent
69pub fn load_icon_w<'h, 't>(hinstance: impl Into<HInstance<'h>>, icon_name: impl Into<NameAtomOrZero<'t, u16>>) -> Result<HIcon<'h>, Error> {
70 fn_context!(load_icon_w => LoadIconW);
71 let hicon = unsafe { LoadIconW(hinstance.into().into(), icon_name.into().as_atom_or_cstr_ptr()) };
72 fn_succeeded!(!hicon.is_null())?;
73 Ok(unsafe { HIcon::from_unchecked(hicon) })
74}