Application.ShowMainForm:=false;如何把隐藏运行的程序,通过热键把它调出来!如何再把程序隐藏哪? (20分)

  • 主题发起人 主题发起人 myjane
  • 开始时间 开始时间
M

myjane

Unregistered / Unconfirmed
GUEST, unregistred user!
Application.ShowMainForm:=false;
 
用 RegisterHotKey 为你的程序注册一个热键,GlobalAddAtom函数是为了获取一个不重复的热键ID。

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
HotKeyID: Integer;
procedure HotKeyPressed(var Msg:TMessage); message WM_HOTKEY;
...

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.ShowMainForm := False;
HotKeyID := GlobalAddAtom(PChar(Application.Title + '^F12'));
if RegisterHotKey(Handle, HotKeyID, MOD_CONTROL, VK_F12) = False then
MessageBox(Handle, '注册热键失败!', PChar(Application.Title), MB_ICONSTOP);
end;

procedure TForm1.HotKeyPressed(var Msg: TMessage);
begin
Show;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnregisterHotKey(Handle, HotKeyID);
DeleteAtom(HotKeyID);
end;
 
通过热键把它调出来后,
如何再把程序隐藏哪?
好像再Application.ShowMainForm:=false;就不能把程序隐藏了
 
是的,再隐藏的话应该用 Visible := False; 或者 Hide; 方法。
 
不可在NT下运行
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
id: Integer;
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
const RSP_SIMPLE_SERVICE=1;
function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord; stdcall;
external 'KERNEL32.DLL';
{$R *.DFM}

procedure TForm1.WMHotKey (var Msg : TWMHotKey);
begin
if msg.HotKey = id then
ShowMessage('Ctrl+Alt+R键被按下!');
form1.Visible :=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
Const
MOD_ALT = 1;
MOD_CONTROL = 2;
VK_R = 82;
begin
if GlobalFindAtom('MyHotkey') = 0 then
begin
{注册全局热键Ctrl + Alt + R}
id:=GlobalAddAtom('MyHotkey');
RegisterHotKey(handle,id,MOD_CONTROL+MOD_Alt,VK_R);
end
else
halt;
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE);
form1.Hide;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnRegisterHotKey(handle,id);
GlobalDeleteAtom(id);
end;

end.
 
多人接受答案了。
 
后退
顶部