在Delphi中定义快捷方式的“正确”方式是哪种?

前端之家收集整理的这篇文章主要介绍了在Delphi中定义快捷方式的“正确”方式是哪种?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有很多关于如何在Delphi程序中定义ShortCut的例子,但是
他们归结为两种不同的方式:

>将任何scCtrl,scShift和scAlt常量添加到键的Ord()
>使用Menus.ShortCut功能

例如

Action.ShortCut := scCtrl + scShift + Ord('K');
// vs
Action.ShortCut := Menus.ShortCut(Word('K'),[ssCtrl,ssShift]);

这两种方式之一是最好的?如果是,哪一个为什么?

解决方法

代码几乎相同,但ShortCut有一些额外的检查:
function ShortCut(Key: Word; Shift: TShiftState): TShortCut;
begin
  Result := 0;
  if HiByte(Key) <> 0 then Exit; // if Key is national character then it can't be used as shortcut
  Result := Key;
  if ssShift in Shift then Inc(Result,scShift); // this is identical to "+" scShift
  if ssCtrl in Shift then Inc(Result,scCtrl);
  if ssAlt in Shift then Inc(Result,scAlt);
end;

因为RegisterHotKey功能使用Virtual key codes(其值从$00到$FE)这个额外的检查是重要的.

请注意,实际的Ord函数代替Ord文档,返回smallint(带符号的Word),所以使用国家字符可以改变包含在ShortCut值的Hi字节中的修改器.

所以更好的是使用ShortCut功能.

原文链接:https://www.f2er.com/delphi/101277.html

猜你在找的Delphi相关文章