全局快捷键与窗口消息的问题? ( 积分: 100 )

  • 主题发起人 主题发起人 shaoye9604
  • 开始时间 开始时间
S

shaoye9604

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用一个隐藏在后台的窗口来接收全局快捷键消息,

发现问题:
1、如果将窗体设置成:
Application.ShowMainForm:=False;
Self.Hide;
窗体将不接收全局快捷键消息;
只要窗体“Self.Hide;”,全局快捷键消息就接收不到了;

所以我变通用了第二种方法:
Self.Width:=0;
Self.Height:=0;
Self.BordStyle:=bsNone;
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
这样也是个隐藏窗体。
但是发现“Self.BordStyle:=bsNone;”这句影响全局快捷键消息的接收
没这句,可以接收全局快捷键消息,但窗体有个标题栏没办法隐藏了。

还望高手能指点一下,有什么好的解决方案没有?
 
Application.ShowMainForm:=False;
Self.Hide;
都能处理注册的快捷键

private
procedure WMHOTKEY(var Msg: TWMHOTKEY); message WM_HOTKEY; //添加处理快捷键的程序

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, 1, MOD_CONTROL, VK_F3); //注册快捷键 Ctrl + F3
end;

procedure TForm1.WMHOTKEY(var Msg: TWMHOTKEY);
begin
if Msg.HotKey = 1 then //判断是否为自己注册的快捷键
ShowMessage('HotKey Ctrl + F3 Pressed'); //测试快捷键
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle, 1); //取消注册
end;
 
你在
RegisterHotKey(Handle, 1, MOD_CONTROL, VK_F3); //注册快捷键 Ctrl + F3
这句前面加上下面两句,能行吗?
Application.ShowMainForm:=False;
Self.Hide;
 
你在
RegisterHotKey(Handle, 1, MOD_CONTROL, VK_F3); //注册快捷键 Ctrl + F3
这句前面加上下面两句,能行吗?
Application.ShowMainForm:=False;
Self.Hide;

能行,你试了吗?我试过行的
 
Self.Hide;
都隐藏了,还有快捷键吗
 
这个东西以前做过的,找找给你:

......

interface
TFormPassWord = class(TForm)
procedure FormCreate(Sender: TObject);
Private
{ Private declarations }
procedure HotKey(var msg: TMessage); Message WM_HOTKEY; //这里声明
Public
{ Public declarations }
function ChangeHotKey(cNewHotKey: Char): Boolean;
end;


......


implementation


//创建
procedure TFormPassWord.FormCreate(Sender: TObject);
var
acHotKey: Char; //呼出热键
aiHotKeyID: Integer; //热键ID
begin
acHotKey := 's';
//注册全局热键
aiHotKeyID := GlobalAddAtom('BwHotkey');
if RegisterHotKey(Handle, aiHotKeyID, MOD_CONTROL + MOD_ALT, Word(acHotKey)) = False then begin
ShowMessage('注册热键 Ctrl + Alt + ' + acHotKey + ' 失败' + #13 + #13 + '程序将退出');
PostQuitMessage(0);
end;
end;




//响应全局热键消息(呼出窗口时使用)
procedure TFormPassWord.HotKey(var msg: TMessage);
begin
if (msg.LParamLo = MOD_CONTROL + MOD_ALT) and (msg.LParamHi = Word(acHotKey)) then begin
//判断应用程序是否处于可见状态
if (Application.Tag = 0) then
FormPassWord.Show
else
FormMain.WindowState := wsNormal;
//激活程序
SetForegroundWindow(Application.Handle);
end;
end;


......
 
to ANiDelphi
我试了,怎么都不行,不知你如何行的,想不通。
 
.pas文档
----------------------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
procedure WMHotKey(var Msg: TWMHOTKEY); message WM_HOTKEY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.WMHotKey(var Msg: TWMHOTKEY);
begin
if Msg.HotKey = 1 then
ShowMessage('HotKey Ctrl + F3 Pressed');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.ShowMainForm := False;
Self.Hide;
RegisterHotKey(Handle, 1, MOD_CONTROL, VK_F3);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle, 1);
end;

end.

=======================================================================
.dfm文档
-------------------------------------
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
end

=======================================================================
环境:Win200SP4 + D7
 
多人接受答案了。
 
后退
顶部