delphi 隐藏运行程序,按某按键后调出来仍继续运行
答案:3 悬赏:80 手机版
解决时间 2021-03-20 11:03
- 提问者网友:火车头
- 2021-03-19 12:04
我想做个程序,开机后台运行(隐藏起来),比如用ctrl+F2组合键调出来 但程序不会因此而关闭,调到前台来继续运行
最佳答案
- 五星知识达人网友:一叶十三刺
- 2021-03-19 12:45
需要使用热键方式来实现此功能,在热键事件中添加显示窗口的代码,以下为以为写过的一个类似程序的关键代码:
//主窗口Visible属性设置为false则启动时不显示主窗口。
private
{ Private declarations }
HotKeyId: Integer; //热键ID
procedure hotkey(var msg:tmessage);message wm_hotkey; //热键事件
//窗口创建事件中添加热键
procedure TForm1.FormCreate(Sender: TObject);
begin
HotKeyId := GlobalAddAtom('MyHotKey') - $C000;
if HotKeyId = 0 then
begin
close;
end;
//热键为ctrl + F8
if(not RegisterHotKey(Handle, hotkeyid, MOD_CONTROL, VK_F8))then
close;
end;
//热键事件 显示主窗口
procedure TForm1.hotkey(var msg:tmessage);
begin
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi = VK_F8) then
begin
form1.Show ;
end;
end;
//退出时取消热键
procedure TForm1.FormDestroy(Sender: TObject);
begin
globalDeleteatom(HotKeyId);
end;
//主窗口Visible属性设置为false则启动时不显示主窗口。
private
{ Private declarations }
HotKeyId: Integer; //热键ID
procedure hotkey(var msg:tmessage);message wm_hotkey; //热键事件
//窗口创建事件中添加热键
procedure TForm1.FormCreate(Sender: TObject);
begin
HotKeyId := GlobalAddAtom('MyHotKey') - $C000;
if HotKeyId = 0 then
begin
close;
end;
//热键为ctrl + F8
if(not RegisterHotKey(Handle, hotkeyid, MOD_CONTROL, VK_F8))then
close;
end;
//热键事件 显示主窗口
procedure TForm1.hotkey(var msg:tmessage);
begin
if (Msg.LparamLo = MOD_CONTROL) AND (Msg.LParamHi = VK_F8) then
begin
form1.Show ;
end;
end;
//退出时取消热键
procedure TForm1.FormDestroy(Sender: TObject);
begin
globalDeleteatom(HotKeyId);
end;
全部回答
- 1楼网友:冷風如刀
- 2021-03-19 13:29
在dpr文件里面设置:
program project1; uses forms, unit1 in 'unit1.pas' {form1}; {$r *.res} begin application.initialize; application.showmainform := false; //设置主窗体不显示,达到隐藏运行的效果 application.createform(tform1, form1); application.run; end.form创建的时候(即create事件),安装一个全局键盘钩子用来截获按键消息。 设置一个boolean类型的公共变量,用来标记窗口是否显示或隐藏
var isformshow: boolean;当截获到ctrl+f2消息时
if isformshow then begin showwindow(form1.handle, sw_hide); showwindow(application.handle, sw_hide); isformshow := false; end else begin showwindow(form1.handle, sw_restore); showwindow(application.handle, sw_restore); isformshow := true; end;//关于delphi全局键盘钩子的资料,网上很多,拿来可以直接用。
- 2楼网友:十年萤火照君眠
- 2021-03-19 13:18
在dpr文件里面设置:
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.ShowMainForm := false; //设置主窗体不显示,达到隐藏运行的效果
Application.CreateForm(TForm1, Form1);
Application.Run;
end.Form创建的时候(即Create事件),安装一个全局键盘钩子用来截获按键消息。
设置一个boolean类型的公共变量,用来标记窗口是否显示或隐藏
var
isformshow: Boolean;
当截获到ctrl+F2消息时
if isformshow then
begin
ShowWindow(Form1.Handle, SW_HIDE);
ShowWindow(Application.Handle, SW_HIDE);
isformshow := false;
end
else
begin
ShowWindow(Form1.Handle, SW_RESTORE);
ShowWindow(Application.Handle, SW_RESTORE);
isformshow := true;
end;
//关于delphi全局键盘钩子的资料,网上很多,拿来可以直接用。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯