我参考网上朋友的东东,想做一个托盘程序~遇见一个问题,就是在windows刚登陆自动运行本程序的时候,怎么让它一开始就最小化,或者闪一个splash屏过一会最小

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

soulmate

Unregistered / Unconfirmed
GUEST, unregistred user!
我参考网上朋友的东东,想做一个托盘程序~遇见一个问题,就是在windows刚登陆自动运行本程序的时候,怎么让它一开始就最小化,或者闪一个splash屏过一会最小化~附程序 ( 积分: 100 )<br />如题~应该怎样修改才可以呢?
unit Server_Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus,Shellapi, StdCtrls, ExtCtrls,registry;

const
WM_WZGLNOTIFY = WM_USER + 199; //自定义消息
strNotifyTip = '◆售水管理系统数据应用服务器◆';
ID_MAIN = 199;

CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}

RegHome:string='SOFTWARE/MicroSoft/Windows/CurrentVersion/Run';

type
TFrm_Main = class(TForm)
PopupMenu1: TPopupMenu;
Show: TMenuItem;
N2: TMenuItem;
Quit: TMenuItem;
GroupBox1: TGroupBox;
Label1: TLabel;
Image1: TImage;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure ShowClick(Sender: TObject);
procedure QuitClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private

nid:NOTIFYICONDATA;
cancels:boolean;
function AddIcon(hwnd: HWND): Boolean; //在状态区添加图标
function RemoveIcon(hwnd: HWND): Boolean; //从状态区移去图标
procedure Notify(var Msg: TMessage); message WM_WZGLNOTIFY; //自定义消息处理函数
procedure minimize(sender: Tobject); //定义最小化过程,赋给Application.OnMinimize
procedure RestoreRequest(var message: TMessage); message CM_RESTORE;


{ Private declarations }
public
{ Public declarations }
end;




var
Frm_Main: TFrm_Main;

implementation

{$R *.dfm}

///////////////自定义函数/////////////////////////////////
////////////////////////////////////////{处理“恢复”消息}

procedure TFrm_Main.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = True then //窗体是否最小化
Application.Restore //恢复窗体
else
Application.BringToFront; //提到前面显示
end;


//在状态区添加图标
function TFrm_Main.AddIcon(hwnd: HWND): Boolean;
begin
nid.cbSize := sizeof(NOTIFYICONDATA);
nid.Wnd := hwnd;

nid.uID := iD_MAIN;
nid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nid.uCallbackMessage := WM_WZGLNOTIFY;
nid.hIcon := LoadIcon(hInstance, 'MAINICON');
strCopy(nid.szTip, strNotifyTip);
AddIcon := Shell_NotifyIcon(NIM_ADD, @nid);
end;

//从状态区移去图标
function TFrm_Main.RemoveIcon(hwnd: HWND): Boolean;
var
nid: NOTIFYICONDATA;
begin
nid.cbSize := sizeof(NOTIFYICONDATA);
nid.Wnd := hwnd;
nid.uID := iD_MAIN ;
nid.uFlags := 0;
RemoveIcon := Shell_NotifyIcon(NIM_DELETE, @nid);
end;

//自定义消息处理函数
procedure TFrm_Main.Notify(var Msg: TMessage);
var
Pt: TPoint;
begin
case msg.LParam of
WM_RBUTTONDOWN: //当点击右键时,弹出快捷菜单
begin
SetForeGroundWindow(nid.wnd);
GetCursorPos(Pt);
Popupmenu1.Popup(pt.x, pt.y);
end;
WM_LBUTTONDBLCLK: //双击
begin
ShowClick(self);
end;
end;
end;

//定义最小化过程,赋给Application.OnMinimize;
procedure TFrm_Main.minimize(sender: Tobject);
begin
AddIcon(handle);
ShowWindow(Application.handle, sw_hide);
end;



procedure TFrm_Main.FormCreate(Sender: TObject);
var
Reg:Tregistry;
str:string;
begin
Reg := Tregistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;

if Reg.OpenKey(RegHome,false) then
begin
if not Reg.ValueExists('SellWater_Server') then
begin
str := Application.ExeName ;
Reg.WriteString('SellWater_Server',str);
end;
end;

Reg.Free;



application.OnMinimize:=minimize;


end;



procedure TFrm_Main.ShowClick(Sender: TObject);
begin
RemoveIcon(handle);
self.WindowState:=wsNormal;

ShowWindow(Application.handle, SW_SHOWNORMAL);
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE);

end;



procedure TFrm_Main.QuitClick(Sender: TObject);
begin
cancels:=true;
RemoveIcon(handle);
application.Terminate;
end;



procedure TFrm_Main.FormShow(Sender: TObject);
begin
cancels:=false;

end;


procedure TFrm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
end;



procedure TFrm_Main.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
canclose:=cancels;
if not canclose then application.Minimize;
end;

end.
 
如题~应该怎样修改才可以呢?
unit Server_Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus,Shellapi, StdCtrls, ExtCtrls,registry;

const
WM_WZGLNOTIFY = WM_USER + 199; //自定义消息
strNotifyTip = '◆售水管理系统数据应用服务器◆';
ID_MAIN = 199;

CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}

RegHome:string='SOFTWARE/MicroSoft/Windows/CurrentVersion/Run';

type
TFrm_Main = class(TForm)
PopupMenu1: TPopupMenu;
Show: TMenuItem;
N2: TMenuItem;
Quit: TMenuItem;
GroupBox1: TGroupBox;
Label1: TLabel;
Image1: TImage;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure ShowClick(Sender: TObject);
procedure QuitClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private

nid:NOTIFYICONDATA;
cancels:boolean;
function AddIcon(hwnd: HWND): Boolean; //在状态区添加图标
function RemoveIcon(hwnd: HWND): Boolean; //从状态区移去图标
procedure Notify(var Msg: TMessage); message WM_WZGLNOTIFY; //自定义消息处理函数
procedure minimize(sender: Tobject); //定义最小化过程,赋给Application.OnMinimize
procedure RestoreRequest(var message: TMessage); message CM_RESTORE;


{ Private declarations }
public
{ Public declarations }
end;




var
Frm_Main: TFrm_Main;

implementation

{$R *.dfm}

///////////////自定义函数/////////////////////////////////
////////////////////////////////////////{处理“恢复”消息}

procedure TFrm_Main.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = True then //窗体是否最小化
Application.Restore //恢复窗体
else
Application.BringToFront; //提到前面显示
end;


//在状态区添加图标
function TFrm_Main.AddIcon(hwnd: HWND): Boolean;
begin
nid.cbSize := sizeof(NOTIFYICONDATA);
nid.Wnd := hwnd;

nid.uID := iD_MAIN;
nid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nid.uCallbackMessage := WM_WZGLNOTIFY;
nid.hIcon := LoadIcon(hInstance, 'MAINICON');
strCopy(nid.szTip, strNotifyTip);
AddIcon := Shell_NotifyIcon(NIM_ADD, @nid);
end;

//从状态区移去图标
function TFrm_Main.RemoveIcon(hwnd: HWND): Boolean;
var
nid: NOTIFYICONDATA;
begin
nid.cbSize := sizeof(NOTIFYICONDATA);
nid.Wnd := hwnd;
nid.uID := iD_MAIN ;
nid.uFlags := 0;
RemoveIcon := Shell_NotifyIcon(NIM_DELETE, @nid);
end;

//自定义消息处理函数
procedure TFrm_Main.Notify(var Msg: TMessage);
var
Pt: TPoint;
begin
case msg.LParam of
WM_RBUTTONDOWN: //当点击右键时,弹出快捷菜单
begin
SetForeGroundWindow(nid.wnd);
GetCursorPos(Pt);
Popupmenu1.Popup(pt.x, pt.y);
end;
WM_LBUTTONDBLCLK: //双击
begin
ShowClick(self);
end;
end;
end;

//定义最小化过程,赋给Application.OnMinimize;
procedure TFrm_Main.minimize(sender: Tobject);
begin
AddIcon(handle);
ShowWindow(Application.handle, sw_hide);
end;



procedure TFrm_Main.FormCreate(Sender: TObject);
var
Reg:Tregistry;
str:string;
begin
Reg := Tregistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;

if Reg.OpenKey(RegHome,false) then
begin
if not Reg.ValueExists('SellWater_Server') then
begin
str := Application.ExeName ;
Reg.WriteString('SellWater_Server',str);
end;
end;

Reg.Free;



application.OnMinimize:=minimize;


end;



procedure TFrm_Main.ShowClick(Sender: TObject);
begin
RemoveIcon(handle);
self.WindowState:=wsNormal;

ShowWindow(Application.handle, SW_SHOWNORMAL);
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE);

end;



procedure TFrm_Main.QuitClick(Sender: TObject);
begin
cancels:=true;
RemoveIcon(handle);
application.Terminate;
end;



procedure TFrm_Main.FormShow(Sender: TObject);
begin
cancels:=false;

end;


procedure TFrm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree;
end;



procedure TFrm_Main.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
canclose:=cancels;
if not canclose then application.Minimize;
end;

end.
 
Application.ShowMainForm := False;
 
procedure TFrm_Main.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = True then //窗体是否最小化
Application.Restore //恢复窗体
else
Application.BringToFront; //提到前面显示
end;


这一段代码去掉就可以
 
楼上的应该行了啊???
 
不行~我自己有个办法,不是最好的办法~
加一个Timer,200ms
Form Create增加 CurrentTime:=GetTickCount div 1000;
procedure TFrm_Main.Timer1Timer(Sender: TObject);
begin
if ((GetTickCount div 1000)&lt;(CurrentTime+3)) then
begin
Sleep(1);
end
else
begin
Timer1.Enabled := false;
postmessage(handle,wm_close,0,0);
end;
end;

这样就可以了~ 请试过了在说啊~我要求的是启动时直接在托盘或闪个扉屏后在托盘,在双击或弹出菜单中选择显示界面时窗口要正常显示~
 
1、打开你的工程文件 project1.dpr:
program Project1;
uses
Windows,
Forms,
Server_Main in 'Server_Main.pas' {Frm_Main};
{$R *.RES}
begin
Application.Initialize;
//加上这句
Application.ShowMainForm := False;
Application.CreateForm(TFrm_Main, Frm_Main);
Application.Run;
end.
2、建议将托盘消息处理中的 WM_RBUTTONDOWN 改为 WM_RBUTTONUP,否则你会发现按下右键弹出不止一个菜单。
3、另外下面这个事件也不妥,你希望一关闭主窗体就退出整个托盘程序么?
procedure TFrm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=cafree; //caHide?
end;
4、还搞不定的话给我发邮件 zh5430@yahoo.com.cn 。
 
兄台,首先谢谢您回复啊~其次您的方法不行,我早试过了~请运行验证后在说~
我的方法是可以的,虽然用起来效果一样,但不是很好~我想找到比较正统的方法~不一定非要找出答案,我只是想找出一个好的方法~
您的Application.ShowMainForm := False;只是让主窗口不显示,我要的是一上来就最小化到托盘,在双击或显示界面时要能恢复,很多软件都有这样的功能,它们在windows启动时会先显示一个欢迎窗口,过一会自动最小化到托盘~
最早时我想在form create中
application.OnMinimize:=minimize;
后面加入
application.Minimize;但是发现这样不行,很多问题,所以才考虑到用一个Timer~
 
用一个第三方控件吧,不用写代码,COOLTRAYICON,网上有的
 
你问问题请讲清楚:
“怎么让它一开始就最小化,或者闪一个splash屏过一会最小化~附程序”
“我要求的是启动时直接在托盘或闪个扉屏后在托盘,在双击或弹出菜单中选择显示界面时窗口要正常显示”
“主窗口不显示”是不是“一开始就最小化”?啊?
上面这些都是你说的,怪谁,话都不会说,啊?
 
呵呵~
我说的很清楚~
 
后退
顶部