H
hans222
Unregistered / Unconfirmed
GUEST, unregistred user!
我是一个delphi初学者,我参照一编关于“CDROM软开关”的文章,写了下面这个unit1.pas代码如下:
unit Unit1;
interface
uses
Windows, Messages,shellapi, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, mmsystem;
const
ICON_ID=1;
//ICON的ID标志
MI_ICONEVENT=WM_USER+1;
//自定义ICON事件消息
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
open: TMenuItem;
close: TMenuItem;
shutdown: TMenuItem;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure openClick(Sender: TObject);
procedure closeClick(Sender: TObject);
//procedure shutdownclick(Sender: TObject);
private
{ Private declarations }
{ Private declarations }
NormalIcon,DisabledIcon:TIcon;
//正常和失效两种情况下的图标
Status:Boolean;
//标志"允许使用"还是"禁止使用"}
procedure InstallIcon;
//procedure ChangeIcon(s:Boolean);
procedure UnInstallIcon;
procedure IconOnClick(var message:TMessage);
message MI_ICONEVENT;
//捕捉自定义消息MI_ICONEVENT的过程IconOnClick的声明
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{procedure TForm1.ChangeIcon(s:Boolean);
//自定义过程ChangeIcon,改变本程序在指示状态栏的图标
var
IconData:TNotifyIconData;
begin
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
if s=False then
//禁止使用
begin
IconData.hIcon:=DisabledIcon.Handle;
Status:=False;
end
else
//允许使用
begin
IconData.hIcon:=Normalicon.Handle;
Status:=True;
end;
IconData.uFlags:=NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY,@IconData);
end;
}
procedure TForm1.IconOnClick(var message:TMessage);
//处理鼠标在指示状态栏对应的图标上的单击事件
var
p:TPoint;
begin
//如果按下的是鼠标左键,并且允许显示,则显示Form2
{if((message.lParam=WM_LBUTTONDOWN) and (Status=True)) then
Form2.Show;}
//如果按下的是鼠标右键,则显示弹出菜单
if(message.lParam=WM_RBUTTONDOWN)then
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
end;
end;
procedure TForm1.InstallIcon;
//安装图标
var
IconData:TNotifyIconData;
begin
NormalIcon:=TIcon.Create;
DisabledIcon:=TIcon.Create;
NormalIcon.LoadFromFile('normal.ico');
//在托盘区显示一个CDROM图标
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage:=MI_ICONEVENT;
IconData.hIcon:=NormalIcon.Handle;
IconData.szTip:='CDROM软开关';
//鼠标悬在状态指示栏对应的图标上时的提示信息
Shell_NotifyIcon(NIM_ADD,@IconData);
end;
procedure TForm1.UnInstallIcon;
//卸载图标
var
IconData:TNotifyIconData;
begin
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@IconData);
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
UnInstallIcon;
//卸载图标
NormalIcon.Free;
DisabledIcon.Free;
//释放两个图标资源
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Status:=True;
InstallIcon;
//安装图标
ShowWindow(Application.Handle,SW_HIDE);
//隐藏主窗体
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
//在切换状态栏上不显示程序图标
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Form1.width:=0;
Form1.Height:=0;
ShowWindow(Application.Handle,SW_HIDE);
//隐藏Form1
{form1.Hide}
end;
procedure TForm1.openClick(Sender: TObject);
begin
mciSendString('Set cdaudiodo
or open wait',nil,0,handle);
end;
procedure TForm1.closeClick(Sender: TObject);
begin
mcisendstring('set cdaudiodo
or closed wait',nil,0,handle);
end;
end.
我现在想在程序运行时,隐藏form1窗体,并在点击系统托盘区的CDROM图标的“退出”弹出式菜单时,关闭程序退出,我
定义了下面过程,但总是显示“[error]unit1.pas(159):statement expected,but expression of type 'Tmenuitem found'”
的错误信息,不能编译通过:
procedure TForm1.shutdownClick(Sender: TObject);
begin
form1.close;
end;
请前辈指点应如何完善上述代码,请别见笑,谢谢!
unit Unit1;
interface
uses
Windows, Messages,shellapi, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, mmsystem;
const
ICON_ID=1;
//ICON的ID标志
MI_ICONEVENT=WM_USER+1;
//自定义ICON事件消息
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
open: TMenuItem;
close: TMenuItem;
shutdown: TMenuItem;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure openClick(Sender: TObject);
procedure closeClick(Sender: TObject);
//procedure shutdownclick(Sender: TObject);
private
{ Private declarations }
{ Private declarations }
NormalIcon,DisabledIcon:TIcon;
//正常和失效两种情况下的图标
Status:Boolean;
//标志"允许使用"还是"禁止使用"}
procedure InstallIcon;
//procedure ChangeIcon(s:Boolean);
procedure UnInstallIcon;
procedure IconOnClick(var message:TMessage);
message MI_ICONEVENT;
//捕捉自定义消息MI_ICONEVENT的过程IconOnClick的声明
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{procedure TForm1.ChangeIcon(s:Boolean);
//自定义过程ChangeIcon,改变本程序在指示状态栏的图标
var
IconData:TNotifyIconData;
begin
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
if s=False then
//禁止使用
begin
IconData.hIcon:=DisabledIcon.Handle;
Status:=False;
end
else
//允许使用
begin
IconData.hIcon:=Normalicon.Handle;
Status:=True;
end;
IconData.uFlags:=NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY,@IconData);
end;
}
procedure TForm1.IconOnClick(var message:TMessage);
//处理鼠标在指示状态栏对应的图标上的单击事件
var
p:TPoint;
begin
//如果按下的是鼠标左键,并且允许显示,则显示Form2
{if((message.lParam=WM_LBUTTONDOWN) and (Status=True)) then
Form2.Show;}
//如果按下的是鼠标右键,则显示弹出菜单
if(message.lParam=WM_RBUTTONDOWN)then
begin
GetCursorPos(p);
PopupMenu1.Popup(p.x,p.y);
end;
end;
procedure TForm1.InstallIcon;
//安装图标
var
IconData:TNotifyIconData;
begin
NormalIcon:=TIcon.Create;
DisabledIcon:=TIcon.Create;
NormalIcon.LoadFromFile('normal.ico');
//在托盘区显示一个CDROM图标
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage:=MI_ICONEVENT;
IconData.hIcon:=NormalIcon.Handle;
IconData.szTip:='CDROM软开关';
//鼠标悬在状态指示栏对应的图标上时的提示信息
Shell_NotifyIcon(NIM_ADD,@IconData);
end;
procedure TForm1.UnInstallIcon;
//卸载图标
var
IconData:TNotifyIconData;
begin
IconData.cbSize:=SizeOf(IconData);
IconData.Wnd:=Handle;
IconData.uID:=ICON_ID;
Shell_NotifyIcon(NIM_DELETE,@IconData);
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
UnInstallIcon;
//卸载图标
NormalIcon.Free;
DisabledIcon.Free;
//释放两个图标资源
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Status:=True;
InstallIcon;
//安装图标
ShowWindow(Application.Handle,SW_HIDE);
//隐藏主窗体
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
//在切换状态栏上不显示程序图标
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Form1.width:=0;
Form1.Height:=0;
ShowWindow(Application.Handle,SW_HIDE);
//隐藏Form1
{form1.Hide}
end;
procedure TForm1.openClick(Sender: TObject);
begin
mciSendString('Set cdaudiodo
or open wait',nil,0,handle);
end;
procedure TForm1.closeClick(Sender: TObject);
begin
mcisendstring('set cdaudiodo
or closed wait',nil,0,handle);
end;
end.
我现在想在程序运行时,隐藏form1窗体,并在点击系统托盘区的CDROM图标的“退出”弹出式菜单时,关闭程序退出,我
定义了下面过程,但总是显示“[error]unit1.pas(159):statement expected,but expression of type 'Tmenuitem found'”
的错误信息,不能编译通过:
procedure TForm1.shutdownClick(Sender: TObject);
begin
form1.close;
end;
请前辈指点应如何完善上述代码,请别见笑,谢谢!