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.