{A}
{S0}简介
此示例代码,允许用户自定义分配给菜单项的快捷方式。快捷方式获取存储在user.confg。因此,应用程序可用于在多用户环境。
当应用程序获取开始,所有菜单项列表。这个列表绑定到ListBox,用户可以选择一个菜单项。选择后,该菜单项的快捷方式可以被分配或修改。在关闭应用程序,分配的快捷键可以存储在user.config。在重新启动,从user.config这些快捷方式读取和分配的菜单。背景
快捷键是按位组合的快捷键。因此,它是可能的结合使用一个快捷键或。为了获得用于一个快捷键,消除已知的密钥,建设快捷,使用XOR。
要保存永久的快捷方式,使用的VS提供的设置。绑定设置(快捷键列表),一个ArrayList使用。消息循环的应用程序已完成后的设置得到保存。这是因为有可能会在应用程序的进一步的形式,因此,"内存"设置只有一次写入永久存储。所有菜单项列表
获取所有菜单项列表
一个递归方法是用来填充的形式与所有的菜单项列表。递归是通过检查,如果一个菜单项下拉项目,即所做的一切,是其他菜单项的父。
绑定到ListBox的列表菜单项private List<ToolStripMenuItem> menuItemsList = new List<ToolStripMenuItem>();
private void FillListWithMenuEntries(ToolStripItemCollection items)
{
foreach (ToolStripMenuItem item in items)
{
menuItemsList.Add(item);
// Verify if this item is a parent for other menu-items.
// If so add these children by recursion:
if (item.HasDropDownItems)
FillListWithMenuEntries(item.DropDownItems);
}
}
的DataSource属性的ListBox是用于绑定的菜单项列表。要显示的菜单项的标题,DisplayMember属性是分配给该属性的名称。{C}类提供了一个键的列表
内置在枚举密钥是不是在一个适当的顺序,使我实现了我自己的钥匙。此列表提供了可能的字母AZ和F -键(F1 - F11)。转换一个关键是进入Keys枚举值的名称,KeysConverter类的一个实例被使用。/// <summary>
/// Class for use in the List of Keys
/// </summary>
public class Key
{
/// <summary>
/// Name of the Key
/// </summary>
public string Name { get; set; }
/// <summary>
/// KeyCode of the key. ReadOnly.
/// </summary>
public Keys KeyCode
{
get
{
KeysConverter keyConverter = new KeysConverter();
return (Keys)keyConverter.ConvertFrom(this.Name);
}
}
}
提供的清单,我实现了另一个类。这个类作为其财产清单。在构造函数中,该列表是建立起来了。/// <summary>
/// Class providing a list with Keys for selection
/// </summary>
public class KeysList
{
#region Properties
/// <summary>
/// ReadOnly by private set (for automatic properties in .net 3.5)
/// </summary>
public List<Key> KeyList { get; private set; }
#endregion
//---------------------------------------------------------------------
#region Contructor
/// <summary>
/// Initializes the KeyList with the selectable keys for the
/// shortcuts (A-Z, F1-F11)
/// </summary>
public KeysList()
{
this.KeyList = new List<Key>();
// Letters A-Z:
for (byte b = 65; b <= 90; b++)
{
// Convert ASCII to character:
char c = Convert.ToChar(b);
// Add to list:
this.KeyList.Add(new Key { Name = c.ToString() });
}
// Add F-keys (F1-F11):
for (byte b = 1; b <= 11; b++)
this.KeyList.Add(new Key { Name = "F" + b.ToString() });
}
#endregion
}
字母AZ代表ASCII码65-90。通过Convert.ToChar(ASCII码),信。
该列表绑定到用户的选择组合框。
结合捷径cmbKeys.DataSource = new KeysList().KeyList;
cmbKeys.DisplayMember = "Name";
cmbKeys.ValueMember = "KeyCode";
上述修饰符和键,快捷键是按位组合。检查(或取消)一个复选框被选中的修饰符。或操作的组合。Keys shortCut = (Keys)cmbKeys.SelectedValue;
if (chkCtrl.Checked) shortCut |= Keys.Control;
if (chkAlt.Checked) shortCut |= Keys.Alt;
if (chkShift.Checked) shortCut |= Keys.Shift;
以下布尔代数规则,组合的顺序无关紧要。
用于查询是否已分配选择/组合快捷,我用一个LINQ查询到所有菜单项列表。var query = menuItemsList.FirstOrDefault(s => s.ShortcutKeys == shortCut);
使用FirstOrDefault,结果是空的元素时没有通过查询获得的 - 这意味着该快捷方式是quot; freequot使用。检索键和修饰符
反向方式得到相结合,一个快捷键和修饰符。
获取的修饰符,我们可以位掩码的快捷和检查,如果结果是等于一个可能的修饰符。位掩蔽通过。在这次行动中,复选框的值都设置。Keys shortCut = selectedItem.ShortcutKeys;
chkCtrl.Checked = (shortCut & Keys.Control) == Keys.Control;
chkAlt.Checked = (shortCut & Keys.Alt) == Keys.Alt;
chkShift.Checked = (shortCut & Keys.Shift) == Keys.Shift;
要获得字母或F键,我们要消除的修饰符。这是通过结合所有的设置修饰符,然后从异快捷。
示例(值使用的是假人,以表明它是如何工作的):Ctrl = 00100000
Alt = 01000000 OR
-------------------
modifyer = 01100000
shortcut = 01100010
modifyer = 01100000 XOR
-------------------
Key = 00000010
代码:
保存的快捷键Keys modifiers = Keys.None;
if (chkCtrl.Checked) modifiers |= Keys.Control;
if (chkAlt.Checked) modifiers |= Keys.Alt;
if (chkShift.Checked) modifiers |= Keys.Shift;
Keys buchstabe = shortCut ^ modifiers;
这是通过使用VS内置的设置设计师。从可能的类型,设计者提供了选择的一个数组列表。的FormClosing,此数组列表的价值已被分配。只有快捷的价值被分配 - 匹配的菜单项的菜单项列表举行。从WinForms应用程序的消息循环结束后的设置写入到永久存储,因为可能有其他形式的操作来执行只有一次。private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// For saving the shortcut in user.config they have to be
// written in an ArrayList. Only the shortcuts are saved:
ArrayList arrList = new ArrayList(menuItemsList.Count);
foreach (ToolStripMenuItem item in menuItemsList)
arrList.Add(item.ShortcutKeys);
Properties.Settings.Default.ShortCuts = arrList;
}
在Program.cs:
在启动Application.Run(new Form1());
// Save the settings:
Properties.Settings.Default.Save();
保存设置值写入user.config文件。在启动时,这些值都被分配到的菜单项。这通过创建一个菜单项列表,然后由指定的快捷键的值。// Get ShortCuts from the saved user-settings (user.config):
ArrayList arrList = Properties.Settings.Default.ShortCuts;
if (arrList != null)
for (int i = 0; i < menuItemsList.Count; i++)
menuItemsList[i].ShortcutKeys = (Keys)arrList[i];
一般来说:ToolStripMenuItem是一个引用类型。因此,改变在任何一个"参考"更改属性在"基本"ToolStripMenuItem的变量的属性。使用代码
该示例是一个完整的工作项目。在您的应用程序一起使用时,你必须调整到用户界面的代码。在有关详细信息,代码,请参阅的意见。兴趣点
组合和检索使用,OR和XOR位操作的捷径键。另请参阅
{A2}描述了一个组件,它允许自定义菜单快捷方式。这可以为无障碍aspplications有用。历史
这是首次发布。灵感来自于在一个论坛上的要求。该代码是由形式从头开始。可能有一些改进,在未来。|德博M. FOIDL