1 #include "stdafx.h"
2 #include "AcceleratorRes.h"
3 #include "resource.h"
4 #include "Common.h"
5 #include "MusicPlayer2.h"
6
CAcceleratorRes()7 CAcceleratorRes::CAcceleratorRes()
8 {
9 }
10
~CAcceleratorRes()11 CAcceleratorRes::~CAcceleratorRes()
12 {
13 }
14
UnsignedCharToShort(char ch)15 static short UnsignedCharToShort(char ch)
16 {
17 return static_cast<short>(static_cast<unsigned char>(ch));
18 }
19
20
Init()21 void CAcceleratorRes::Init()
22 {
23 //载入资源
24 HINSTANCE hIns = AfxGetInstanceHandle();
25 HRSRC hRsrc = ::FindResource(hIns, MAKEINTRESOURCE(IDR_ACCELERATOR1), RT_ACCELERATOR);
26 if (hRsrc == NULL)
27 return;
28 DWORD len = SizeofResource(hIns, hRsrc);
29 BYTE* lpRsrc = (BYTE*)LoadResource(hIns, hRsrc);
30 if (lpRsrc == nullptr)
31 return;
32 m_res_data.assign((const char*)lpRsrc, len);
33 FreeResource(lpRsrc);
34
35 //#ifdef _DEBUG
36 // CCommon::SaveDataToFile(m_res_data, L".\\accelerator.bin");
37 //#endif
38
39 //解析Accelerator资源
40 /*
41 Accelerator每8个字节一组
42 字节0: 修饰符
43 0x0B: Ctrl
44 0x13: Alt
45 0x07: Shift
46 0x0F: Ctrl+Shift
47 0x1B: Ctrl+Alt
48 0x17: Alt+Shift
49 0x9F: Ctrl+Alt+Shift
50 字节1: 00
51 字节2: 键
52 字节3: 00
53 字节4、字节5: 命令ID
54 字节6: 00
55 字节7: 00
56 */
57 for (size_t i{}; i < m_res_data.size(); i += 8) //每8个字节一组
58 {
59 std::string data{ m_res_data.substr(i, 8) };
60 if (data.size() < 8)
61 break;
62
63 Key cur_key;
64 //修饰符
65 switch (data[0])
66 {
67 case '\x0b':
68 cur_key.ctrl = true, cur_key.alt = false, cur_key.shift = false;
69 break;
70 case '\x13':
71 cur_key.ctrl = false, cur_key.alt = true, cur_key.shift = false;
72 break;
73 case '\x07':
74 cur_key.ctrl = false, cur_key.alt = false, cur_key.shift = true;
75 break;
76 case '\x0f':
77 cur_key.ctrl = true, cur_key.alt = false, cur_key.shift = true;
78 break;
79 case '\x1b':
80 cur_key.ctrl = true, cur_key.alt = true, cur_key.shift = false;
81 break;
82 case '\x17':
83 cur_key.ctrl = false, cur_key.alt = true, cur_key.shift = true;
84 break;
85 case '\x9f':
86 cur_key.ctrl = true, cur_key.alt = true, cur_key.shift = true;
87 break;
88 default:
89 break;
90 }
91
92 //键
93 cur_key.key = UnsignedCharToShort(data[2]);
94
95 //命令ID
96 UINT cmd_id = UnsignedCharToShort(data[4]) + UnsignedCharToShort(data[5]) * 256;
97
98 m_accelerator_res[cmd_id] = cur_key;
99 }
100
101 //#ifdef _DEBUG
102 // std::string str;
103 // for (const auto& item : m_accelerator_res)
104 // {
105 // str += std::to_string(item.first);
106 // str += "\t";
107 // str += CCommon::UnicodeToStr(item.second.ToString().c_str(), CodeType::ANSI);
108 // str += "\r\n";
109 // }
110 // CCommon::SaveDataToFile(str, L".\\accelerator.txt");
111 //#endif
112
113 }
114
GetShortcutDescriptionById(UINT id) const115 std::wstring CAcceleratorRes::GetShortcutDescriptionById(UINT id) const
116 {
117 auto iter = m_accelerator_res.find(id);
118 if (iter != m_accelerator_res.end())
119 return iter->second.ToString();
120 else
121 return std::wstring();
122 }
123
ToString() const124 std::wstring CAcceleratorRes::Key::ToString() const
125 {
126 wstring str;
127 if (key == 0)
128 return str;
129
130 if (ctrl)
131 str += L"Ctrl+";
132 if (shift)
133 str += L"Shift+";
134 if (alt)
135 str += L"Alt+";
136
137 if ((key >= '0' && key <= '9') || (key >= 'A' && key <= 'Z'))
138 {
139 str += static_cast<wchar_t>(key);
140 }
141 else if (key == VK_DELETE)
142 {
143 str += L"Del";
144 }
145 else if (key == VK_LEFT)
146 {
147 str += theApp.m_str_table.LoadText(L"TXT_SHORCUT_LEFT");
148 }
149 else if (key == VK_RIGHT)
150 {
151 str += theApp.m_str_table.LoadText(L"TXT_SHOTCUT_RIGHT");
152 }
153 else if (key == VK_UP)
154 {
155 str += theApp.m_str_table.LoadText(L"TXT_SHOTCUT_UP");
156 }
157 else if (key == VK_DOWN)
158 {
159 str += theApp.m_str_table.LoadText(L"TXT_SHOTCUT_DOWN");
160 }
161 else if (key >= VK_F1 && key <= VK_F24)
162 {
163 str += L'F';
164 str += std::to_wstring(key - VK_F1 + 1);
165 }
166 else if (key == VK_NEXT)
167 {
168 str += L"PgDn";
169 }
170 else if (key == VK_PRIOR)
171 {
172 str += L"PgUp";
173 }
174 else if (key == VK_RETURN)
175 {
176 str += L"Enter";
177 }
178 else if (key == VK_SPACE)
179 {
180 str += theApp.m_str_table.LoadText(L"TXT_SHOTCUT_SPACE");
181 }
182 else if (key == VK_OEM_MINUS)
183 {
184 str += L"-";
185 }
186 else if (key == VK_OEM_PLUS)
187 {
188 str += L"=";
189 }
190 else
191 {
192 wchar_t buff[16];
193 swprintf_s(buff, L"%d", key);
194 str += buff;
195 }
196
197 return str;
198 }
199