delphi里关于关闭窗体的问题!!!(50分)

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

skztjxt

Unregistered / Unconfirmed
GUEST, unregistred user!
程序里,为了不让窗体被用户随便关闭,就像微软的MSN Messenger一样,当用户点击窗口右上角的“×”时,窗口不是被关掉,而是最小化到任务栏里面显示,只有右击任务栏里的图标,选择“退出”菜单时,程序才被关掉退出。
为了实现这个功能,在Delphi里代码该怎么写啊?
请求大虾们帮忙啊!!!
 
截获WM_SYSCOMMAND消息就可以了:

unit TrayIcon;

interface

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

type
//----------------------------------------------------------------------
PNotifyIconData = ^TNotifyIconDataA;
TNotifyIconDataA = record
cbSize : DWORD;
Wnd : HWND;
uID : UINT;
uFlags : UINT;
uCallbackMessage : UINT;
hIcon : HICON;
szTip : array [0..63] of AnsiChar;
end;
//----------------------------------------------------------------------
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
open1: TMenuItem;
close1: TMenuItem;
N1: TMenuItem;
about1: TMenuItem;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure open1Click(Sender: TObject);
procedure close1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
//-------------------------------------------------------------
IconData: TNotifyIconData;
procedure ShowIcon;
procedure IconOnClick(var message:TMessage); message WM_USER+1;
Procedure WMSysCommand(Var message : TMessage) ; Message WM_SYSCOMMAND ;
//-------------------------------------------------------------
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.IconOnClick( var message: Tmessage);
var p : TPoint;
begin
if (message.lParam = WM_LBUTTONDOWN) then
begin
ShowWindow(Handle, SW_SHOW );
end;

if (message.lParam = WM_RBUTTONDOWN) then
begin
GetCursorPos(p);
popupmenu1.Popup( p.x ,p.y );
end;
end;

Procedure TForm1.WMSysCommand(Var Message : TMessage) ;
begin

if (Message.WParam = SC_MINIMIZE)or(Message.WParam = SC_CLOSE) then
begin
ShowIcon;
end
else
Inherited;

end;

procedure TForm1.ShowIcon;
begin
IconData.cbSize := SizeOf( IconData );
IconData.Wnd := Handle;
IconData.uID := 1;
IconData.uFlags := NIF_ICON
or NIF_MESSAGE or NIF_TIP;
IconData.uCallBackMessage := WM_USER+1;
IconData.hIcon := application.Icon.Handle;
IconData.szTip := 'LANChat';
Shell_NotifyIcon( NIM_ADD, @IconData );
ShowWindow(Handle, SW_HIDE);
hide;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon( NIM_DELETE, @IconData );
end;

procedure TForm1.open1Click(Sender: TObject);
begin
Form1.Show;
end;

procedure TForm1.close1Click(Sender: TObject);
begin
Form1.close;
end;

procedure TForm1.FormShow(Sender: TObject);
begin

showwindow(application.handle,sw_hide);


end;

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

end.
 
有用函数的,有用控件的
搜索一下啦,到处都是啊~

 
我刚刚入门Delohi,也是第一次来这里,大家的热情参与让我很感动啊,我我正在尝试上面的
代码,谢谢!
 
接受答案了.
 

Similar threads

S
回复
0
查看
768
SUNSTONE的Delphi笔记
S
S
回复
0
查看
681
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部