A
amli
Unregistered / Unconfirmed
GUEST, unregistred user!
//看一下可不可以再改进.
unit HotKeyBtns;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Menus;
type
THotKeyBtn = class(TButton)
private
{ Private declarations }
FHotKey : TShortCut;
protected
{ Protected declarations }
public
{ Public declarations }
procedure WMHOTKEY(var Message : TMessage); message WM_HOTKEY;
procedure WMDESTROY(var Message : TMessage); message WM_DESTROY;
procedure Loaded;override;
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property HotKey : TShortCut read FHotKey write FHotKey;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [THotKeyBtn]);
end;
{ THotKeyBtn }
constructor THotKeyBtn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHotKey := 0;
end;
procedure THotKeyBtn.Loaded;
var
Key: Word;
Shift: TShiftState;
Const
ShiftKeys : array[0..2] of Integer=(MOD_SHIFT, MOD_ALT, MOD_CONTROL);
begin
inherited;
if not (csDesigning in ComponentState) and (FHotKey <> 0) then
begin
ShortCutToKey(FHotKey, Key, Shift);
if Shift = [ssShift] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_SHIFT, Key)
else
if Shift = [ssAlt] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_ALT, Key)
else
if Shift = [ssCtrl] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_CONTROL, Key);
end;
end;
procedure THotKeyBtn.WMDESTROY(var Message: TMessage);
begin
if not (csDesigning in ComponentState) then
UnregisterHotKey(Handle, IDHOT_SNAPWINDOW);
inherited;
end;
procedure THotKeyBtn.WMHOTKEY(var Message: TMessage);
var
Key: Word;
Shift: TShiftState;
function GetPWnd : THandle;
begin
Result := Handle;
repeat
Result := Windows.GetParent(Result);
until not IsWindow(Windows.GetParent(Result));
end;
begin
ShortCutToKey(FHotKey, Key, Shift);
if (GetActiveWindow = GetPWnd) and (Message.WParam = IDHOT_SNAPWINDOW) and
(Message.LParamHi = Key) and
((Message.LParamLo = MOD_CONTROL) and (Shift = [ssCtrl]) or
(Message.LParamLo = MOD_ALT) and (Shift = [ssAlt]) or
(Message.LParamLo = MOD_SHIFT) and (Shift = [ssShift])) and Enabled then Click
end;
end.
unit HotKeyBtns;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Menus;
type
THotKeyBtn = class(TButton)
private
{ Private declarations }
FHotKey : TShortCut;
protected
{ Protected declarations }
public
{ Public declarations }
procedure WMHOTKEY(var Message : TMessage); message WM_HOTKEY;
procedure WMDESTROY(var Message : TMessage); message WM_DESTROY;
procedure Loaded;override;
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property HotKey : TShortCut read FHotKey write FHotKey;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [THotKeyBtn]);
end;
{ THotKeyBtn }
constructor THotKeyBtn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHotKey := 0;
end;
procedure THotKeyBtn.Loaded;
var
Key: Word;
Shift: TShiftState;
Const
ShiftKeys : array[0..2] of Integer=(MOD_SHIFT, MOD_ALT, MOD_CONTROL);
begin
inherited;
if not (csDesigning in ComponentState) and (FHotKey <> 0) then
begin
ShortCutToKey(FHotKey, Key, Shift);
if Shift = [ssShift] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_SHIFT, Key)
else
if Shift = [ssAlt] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_ALT, Key)
else
if Shift = [ssCtrl] then
RegisterHotKey(Handle, IDHOT_SNAPWINDOW, MOD_CONTROL, Key);
end;
end;
procedure THotKeyBtn.WMDESTROY(var Message: TMessage);
begin
if not (csDesigning in ComponentState) then
UnregisterHotKey(Handle, IDHOT_SNAPWINDOW);
inherited;
end;
procedure THotKeyBtn.WMHOTKEY(var Message: TMessage);
var
Key: Word;
Shift: TShiftState;
function GetPWnd : THandle;
begin
Result := Handle;
repeat
Result := Windows.GetParent(Result);
until not IsWindow(Windows.GetParent(Result));
end;
begin
ShortCutToKey(FHotKey, Key, Shift);
if (GetActiveWindow = GetPWnd) and (Message.WParam = IDHOT_SNAPWINDOW) and
(Message.LParamHi = Key) and
((Message.LParamLo = MOD_CONTROL) and (Shift = [ssCtrl]) or
(Message.LParamLo = MOD_ALT) and (Shift = [ssAlt]) or
(Message.LParamLo = MOD_SHIFT) and (Shift = [ssShift])) and Enabled then Click
end;
end.