public
{ Public declarations }
atom : ATOM;
procedure WndProc( var Message : TMessage ); override;
end;
var
Form1: TForm1;
iconData : TNotifyIconData;
implementation
{$R *.DFM}
const MY_SHOWFORM_ID = WM_USER+1;
procedure TForm1.WndProc( var Message : TMessage );
var
p : TPoint;
begin
case Message.Msg of
//아이콘에 클릭시
MY_SHOWFORM_ID:
case Message.lParam of
WM_LBUTTONDOWN: begin
Form_HIDE := False;
ShowWindow(Handle, SW_SHOWNORMAL);
//Self.Show;
end;
WM_RBUTTONDOWN: begin
GetCursorPos(p);
TrayPop.Popup(p.x, p.y);
// 마우스의 위치를 알아내서 그위치에 팝업메뉴를 띄운다
end;
end;
//폼이 최소될때는
WM_SysCommand:
begin
if TWMSysCommand(Message).CmdType and $FFF0 = SC_MINIMIZE then begin
TWMSysCommand(Message).CmdType := 0;
ShowWindow(Handle, SW_HIDE);
Form_HIDE := True;
end;
end;
end;
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
wnd : HWND;
Form_Caption : string;
begin
{프로그램이 이미 실행되어 있나 검사하는 곳}
if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
atom := GlobalAddAtom('PROGRAM_RUNNING') {실행되어 있지 않으면..}
else begin
{ 프로그램이 이미 실행되어 있으면..}
MessageDlg('이미 실행되어 있네요...', mtWarning,[mbOK], 0);
Form_Caption := Form1.Caption;
Form1.Caption := Form1.Caption + '2';
Wnd := FindWindow(nil, PChar(Form_Caption) );
if Wnd <> 0 then begin
ShowWindow(Wnd, SW_SHOWNORMAL);
end;
Halt;
end;
정리해서 사용해보세요.
파일은 자료실에 넣을께요...다른이들도 보고 고쳐서 리플달게요
program Project1;
uses
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
SetWindowLong(Application.Handle,GWL_EXSTYLE,
GetWindowLong(Application.Handle,GWL_EXSTYLE)
or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
/////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ShellAPi, Menus;
type
TForm1 = class(TForm)
TrayPop: TPopupMenu;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
N41: TMenuItem;
N51: TMenuItem;
Exit1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N21Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Form_HIDE : Boolean;
public
{ Public declarations }
atom : ATOM;
procedure WndProc( var Message : TMessage ); override;
end;
var
Form1: TForm1;
iconData : TNotifyIconData;
implementation
{$R *.DFM}
const MY_SHOWFORM_ID = WM_USER+1;
procedure TForm1.WndProc( var Message : TMessage );
var
p : TPoint;
begin
case Message.Msg of
//아이콘에 클릭시
MY_SHOWFORM_ID:
case Message.lParam of
WM_LBUTTONDOWN: begin
Form_HIDE := False;
ShowWindow(Handle, SW_SHOWNORMAL);
//Self.Show;
end;
WM_RBUTTONDOWN: begin
GetCursorPos(p);
TrayPop.Popup(p.x, p.y);
// 마우스의 위치를 알아내서 그위치에 팝업메뉴를 띄운다
end;
end;
//폼이 최소될때는
WM_SysCommand:
begin
if TWMSysCommand(Message).CmdType and $FFF0 = SC_MINIMIZE then begin
TWMSysCommand(Message).CmdType := 0;
ShowWindow(Handle, SW_HIDE);
Form_HIDE := True;
end;
end;
end;
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
wnd : HWND;
Form_Caption : string;
begin
{프로그램이 이미 실행되어 있나 검사하는 곳}
if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
atom := GlobalAddAtom('PROGRAM_RUNNING') {실행되어 있지 않으면..}
else begin
{ 프로그램이 이미 실행되어 있으면..}
MessageDlg('이미 실행되어 있네요...', mtWarning,[mbOK], 0);
Form_Caption := Form1.Caption;
Form1.Caption := Form1.Caption + '2';
Wnd := FindWindow(nil, PChar(Form_Caption) );
if Wnd <> 0 then begin
ShowWindow(Wnd, SW_SHOWNORMAL);
end;
Halt;
end;
with IconData do
begin
cbSize := SizeOf ( IconData );
Wnd := Handle;
uID := 100;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_USER + 1;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
Shell_NotifyIcon( NIM_ADD, @IconData );
//ShowWindow (Handle, SW_HIDE);
Form_HIDE := True;
// 프로그램의 아이콘을 트레이에...
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
if Form_HIDE then
ShowWindow(Handle, SW_HIDE);
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.N11Click(Sender: TObject);
begin
Form_HIDE := False;
ShowWindow(Handle, SW_SHOWNORMAL);
end;
procedure TForm1.N21Click(Sender: TObject);
begin
ShowWindow(Handle, SW_HIDE);
Form_HIDE := True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon( NIM_DELETE, @IconData ); //trayicon 정상화
{프로그램이 끝날때 다시 정보를 지워줌}
GlobalDeleteAtom(atom);
end;
end.