强行关机的语句(100分)

  • 主题发起人 主题发起人 greenwon
  • 开始时间 开始时间
G

greenwon

Unregistered / Unconfirmed
GUEST, unregistred user!
等同于按电源开关,不管有没有其它程序开启,直接关闭。
如用:ExitWindowEx(EWX_SHUTDOWN and EWX_POWEROFF,0) 语句当系统中有不能关闭的
程序时(如Word未保存现编辑文件)则会询问,是不行的。
我要的是:执行语句就关机,不管当前系统状态如何。
 
顺便问一下:怎样使程序一运行就最小化并在托盘图标中显示,单击托盘图标后恢复主窗口显示
 
加上EWX_FORCE参数
 
1、ExitWindowsEx(EWX_Force or EWX_ShutDown,0)
2、将图标加入任务栏:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ShellApi;

const
My_Message = WM_USER + 100; //自定义消息

type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
N1: TMenuItem; //还原
N2: TMenuItem; //退出
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
private
procedure addTray; {增加图标}
procedure RemoveTray; {删除图标}
procedure OnIconNotify(var Message: TMessage); message My_Message;
procedure InsertComp;
procedure RemoveComp;
public
{ Public declarations }
end;

var
Form1 : TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
addTray;
end;

procedure TForm1.addTray;
var
nid : TNotifyIconData;
begin
FillChar(nid, SizeOf(TNotifyIconData), 0);
nid.cbSize := SizeOf(TNotifyIconData); //变量的字节数
nid.Wnd := Handle; //主窗口句柄
nid.uID := 1; //内部标识,可为任意数
nid.hIcon := Application.Icon.Handle; //要加入的图标句柄
nid.szTip := '图标示例程序'; //提示字符串
nid.uCallbackMessage := My_Message; //回调函数消息
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; //指明哪些字段有效
if not Shell_NotifyIcon(NIM_ADD, @nid) then
Application.Terminate;

{将窗口样式设为Tools窗口,可避免在任务条上出现}
SetWindowLong(Form1.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;

procedure TForm1.RemoveTray;
var
nid : TNotifyIconData;
begin
FillChar(nid, SizeOf(TNotifyIconData), 0);
nid.cbSize := SizeOf(TNotifyIconData); //变量的字节数
nid.uID := 1; //内部标识,与加入小图标时一致
nid.Wnd := Handle; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
RemoveComp;
CanClose := False;
end;

procedure TForm1.OnIconNotify(var Message: TMessage);
const
Busy : Boolean = False;
var
Pos : TPoint;
begin
if not Busy then
begin
Busy := True;
if Message.LParam = WM_RBUTTONDOWN then
begin
GetCursorPos(Pos);
PopupMenu1.Popup(Pos.x, Pos.y);
end;
Busy := False;
end;
end;

procedure TForm1.InsertComp;
begin
if Visible then Hide;
Show;
ShowWindow(Application.Handle, SW_SHOW);
Application.BringToFront;
end;

procedure TForm1.RemoveComp;
begin
if not Visible then Show;
Hide;
ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TForm1.N1Click(Sender: TObject);
begin
InsertComp;
end;

procedure TForm1.N2Click(Sender: TObject);
begin
RemoveTray;
Application.Terminate;
end;

end.
 
使用ExitWindowEx(EWX_FORCE or EWX_POWEROFF,0)
->怎样使程序一运行就最小化并在托盘图标中显示,单击托盘图标后恢复主窗口显示
form1.WindowState:=sMinimized;
 
用这句不能强行关机:ExitWindowEx(EWX_FORCE or EWX_POWEROFF,0)
 
一、程序关机:
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
hToken : THandle;
tkp : TTokenPrivileges;
tkpo : TTokenPrivileges;
zero : DWORD;
begin
zero:=0;
if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
then begin
MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end;
// if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)

if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
then begin
MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );
Exit;
end;
// if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)

// SE_SHUTDOWN_NAME
if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid )
then begin
MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );
Exit;
end;
// if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )

tkp.PrivilegeCount := 1;
tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );

if Boolean( GetLastError() )
then begin
MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );
Exit;
end
// if Boolean( GetLastError() )
else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0);
end;
二、最小化到托盘:用TrayIcon控件,可以在控件的click事件里显示或隐藏窗体:
Form.hide;
Form.Show;
 
哈哈,这个我知道。
Application.ShowMainForm := False;//写在创建主窗口之前
 
多人接受答案了。
 
后退
顶部