自定义一个全局热键f1,响应事件如下,奇怪的是,第一次按下F1时可以隐藏,再按F2时,却不能达到想要的结果,不能显示窗体,什么反应也没有了,procedure TForm1.FormCreate(Sender: TObject);
begin
id:=GlobalAddAtom('Hotkey');//'Hotkey'名字可以随便取
RegisterHotKey(form1.Handle,id,0,VK_F1);
end;procedure TForm1.WMHotKey(var Msg : TWMHotKey);
begin
if msg.HotKey = id then
begin
if (self.Visible=true) then
ShowWindow(self.handle,SW_HIDE)
else if(self.Visible=false) then
ShowWindow(self.Handle,SW_SHOW)
end;end;
if self.Visible then
Self.Hide
else
Self.Show;
if (self.Visible=true) then
ShowWindow(self.handle,SW_HIDE)
else if(self.Visible=false) then
ShowWindow(self.Handle,SW_SHOW) 你要么只使用Delphi的方式:
if (self.Visible=true) then
Self.Hide
else
Self.Show;要么只使用WIN32 API:
if (IsWindowVisible(self.handle)) then
ShowWindow(self.handle,SW_HIDE)
else
ShowWindow(self.Handle,SW_SHOW)
begin
if msg.HotKey = id then
begin
self.Visible:= not self.Visible;
end;
end;
你上面的代码用showwindow是不会改变Visible属性的,所以你的Visible永远是true所以运行不正常.
begin
if msg.HotKey = id then
begin
self.Visible:= not self.Visible;
end;
end;
这个方法就可以的,我刚试了