1 | #include "stdafx.h"
|
---|
2 | #include <Resource/Load.h>
|
---|
3 |
|
---|
4 | namespace ActiveBasic { namespace Resource {
|
---|
5 |
|
---|
6 | namespace {
|
---|
7 |
|
---|
8 | template<typename Map, typename Key, typename AddValueFunctor>
|
---|
9 | typename Map::iterator GetCacheFromMap(Map& map, Key key, AddValueFunctor f)
|
---|
10 | {
|
---|
11 | auto low = map.lower_bound(key);
|
---|
12 | if (low != map.end() && map.key_comp()(key, low->first))
|
---|
13 | {
|
---|
14 | return low;
|
---|
15 | }
|
---|
16 | else
|
---|
17 | {
|
---|
18 | typedef typename Map::key_type key_type;
|
---|
19 | typedef typename Map::value_type value_type;
|
---|
20 | return map.insert(low, std::make_pair(std::move(key), f()));
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | HICON LoadIconCursorImpl(HINSTANCE hinst, USHORT id, int cxDesired, int cyDesired, UINT load, bool isIcon)
|
---|
25 | {
|
---|
26 | auto hrsrc = FindResource(hinst, MAKEINTRESOURCE(id), isIcon ? RT_GROUP_ICON : RT_GROUP_CURSOR);
|
---|
27 | auto pResource = LockResource(LoadResource(hinst, hrsrc));
|
---|
28 |
|
---|
29 | auto idIcon = LookupIconIdFromDirectoryEx(reinterpret_cast<PBYTE>(pResource), isIcon, cxDesired, cyDesired, load);
|
---|
30 | auto hrsrcIcon = FindResource(hinst, MAKEINTRESOURCE(idIcon), isIcon ? RT_ICON : RT_CURSOR);
|
---|
31 | auto pResourceIcon = LockResource(LoadResource(hinst, hrsrcIcon));
|
---|
32 |
|
---|
33 | return CreateIconFromResourceEx(reinterpret_cast<PBYTE>(pResourceIcon),
|
---|
34 | SizeofResource(hinst, hrsrcIcon), isIcon, 0x00030000, cxDesired, cyDesired, load);
|
---|
35 | }
|
---|
36 |
|
---|
37 | }
|
---|
38 |
|
---|
39 | HICON LoadIcon(HINSTANCE hinst, USHORT id, int cxDesired, int cyDesired, UINT load)
|
---|
40 | {
|
---|
41 | return LoadIconCursorImpl(hinst, id, cxDesired, cyDesired, load, true);
|
---|
42 | }
|
---|
43 |
|
---|
44 | HICON LoadIcon(HINSTANCE hinst, USHORT id)
|
---|
45 | {
|
---|
46 | return LoadIconCursorImpl(hinst, id, 32, 32, LR_SHARED, true);
|
---|
47 | }
|
---|
48 |
|
---|
49 | HCURSOR LoadCursor(HINSTANCE hinst, USHORT id)
|
---|
50 | {
|
---|
51 | return LoadIconCursorImpl(hinst, id, 32, 32, LR_SHARED, false);
|
---|
52 | }
|
---|
53 |
|
---|
54 | // 文字列リソースの読み込みについては以下を参照。
|
---|
55 | // How To Use LoadResource to Load Strings from a String Table
|
---|
56 | // http://support.microsoft.com/kb/200893/en-us
|
---|
57 | boost::optional<std::wstring> LoadString(HINSTANCE hinst, USHORT id)
|
---|
58 | {
|
---|
59 | UINT idRsrcBlk = id / 16 + 1;
|
---|
60 | int strIndex = id % 16;
|
---|
61 |
|
---|
62 | auto hrsrc = FindResourceEx(hinst, RT_STRING, MAKEINTRESOURCE(idRsrcBlk), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT));
|
---|
63 | if (hrsrc == nullptr)
|
---|
64 | return boost::none;
|
---|
65 |
|
---|
66 | auto hRes = LoadResource(hinst, hrsrc);
|
---|
67 | if (hRes == nullptr)
|
---|
68 | return boost::none;
|
---|
69 | LPCWSTR p = static_cast<LPCWSTR>(LockResource(hRes));
|
---|
70 | if (p == nullptr)
|
---|
71 | return boost::none;
|
---|
72 |
|
---|
73 | for (int i = 0; i < strIndex; ++i)
|
---|
74 | {
|
---|
75 | UINT length = *p++;
|
---|
76 | p += length;
|
---|
77 | }
|
---|
78 |
|
---|
79 | UINT cch = *p++;
|
---|
80 | if (cch == 0)
|
---|
81 | {
|
---|
82 | return boost::none;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | return std::wstring(p, cch);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | }}
|
---|
91 |
|
---|