问题描述
MFC功能包似乎神奇地将资源键盘加速器快捷方式打印到菜单项的工具提示。
我该如何找到给定命令ID的组合键(如果我只有HACCEL
句柄?)。
解决方法
您可以使用CopyAcceleratorTable()
function来获取给定HACCEL
句柄的加速器列表。
首先,使用nullptr
和0
作为第二个和第三个参数调用该函数以获取列表的大小(即加速器的数量),然后分配{{1}的缓冲区}大小合适的结构。
然后,您可以再次使用指向该缓冲区的指针和先前返回的计数来调用ACCEL
。
现在,您可以遍历该缓冲区,使用每个结构的三个字段来确定加速键是什么,其具有的标志以及它代表的命令。
CopyAcceleratorTable
您可以在this page上看到HACCEL hAcc; // Assuming this is a valid handle ...
int nAcc = CopyAcceleratorTable(hAcc,nullptr,0); // Get number of accelerators
ACCEL *pAccel = new ACCEL[nAcc]; // Allocate the required buffer
CopyAcceleratorTable(hAcc,pAccel,nAcc); // Now copy to that buffer
for (int a = 0; a < nAcc; ++a) {
DWORD cmd = pAccel[a].cmd; // The command ID
WORD key = pAccel[a].key; // The key code - such as 0x41 for the "A" key
WORD flg = pAccel[a].fVirt; // The 'flags' (things like FCONTROL,FALT,etc)
//...
// Format and display the data,as required
//...
}
delete[] pAccel; // Don't forget to free the data when you're done!
结构的三个数据字段的值和格式的描述。