自定义一个全局热键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;

解决方案 »

  1.   

    F2?你没有把F2注册为热键啊,你看看你的代码,只注册了F1。
      

  2.   

    self.Visible改为self.onshowing试试
      

  3.   

    真不好意思,二楼的,我打错了,应该是F1三楼的,你的方法不行啊,onshowing是未定义字符,不是属性啊
      

  4.   

    可以定义一个标志变量来代替  if (self.Visible=true) then  的判断
      

  5.   

    试试:
      if self.Visible then 
        Self.Hide
      else
        Self.Show;
      

  6.   

    因为你把WIN23 API和Delphi的属性混用了:
    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) 
      

  7.   

    procedure TForm1.WMHotKey(var Msg: TWMHotKey);
    begin
      if msg.HotKey = id then
      begin
        self.Visible:= not self.Visible;
      end;
    end;
      

  8.   

    你可以看一下delphi里TCustomForm.Hide是怎么定义的,只有在你调用self.hide时form的visible属性才会被设为false,同理self.show时才会被设为true
    你上面的代码用showwindow是不会改变Visible属性的,所以你的Visible永远是true所以运行不正常.
      

  9.   

    procedure TForm1.WMHotKey(var Msg: TWMHotKey); 
    begin 
      if msg.HotKey = id then 
      begin 
        self.Visible:= not self.Visible; 
      end; 
    end;
    这个方法就可以的,我刚试了