Starting an application as an icon in the system tray

31437 
Created at 2006-09-29 06:39:09 
166   0   0   0  
 
Copyright © 2000 Ernesto De Spirito

Download the source code


Placing an icon the system tray

The system tray is the area at the right of the task bar where "background" applications can place their icons. To place an icon in the tray you have to call the Shell_NotifyIcon API declared in the ShellAPI unit, passing it the constant NIM_ADD (to tell it to add an icon) and the address of a TNotifyIconData record with the icon information that includes the handle of the icon to show, the text to show as tool tip when the mouse is over the icon, the handle of the window that will receive the messages of the icon and the message type the icon will send to this window.

uses
  ..., ShellAPI;

const
WM_ICONTRAY = WM_USER + 1;   // User-defined message

type
...

var
...
NotifyIconData : TNotifyIconData;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
with NotifyIconData do begin
   hIcon := Icon.Handle;
   StrPCopy(szTip, Application.Title);
   Wnd := Handle;
   uCallbackMessage := WM_ICONTRAY;
   uID := 1;
   uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
   cbSize := sizeof(TNotifyIconData);
end;
Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
end;
Handling icon messages

Form1 will receive WM_ICONTRAY messages from the icon. To capture and handle these messages we have to declare a new method in our form. For example:

type
TForm1 = class(TForm)
   ...
private
   { Private declarations  }
   ...
   procedure Icontray(var Msg: TMessage); message WM_ICONTRAY;
public
   { Public declarations  }
   ...
end;
In the implementation of this method we can for example display a pop-up menu when the user right-clicks on the icon. Assuming you have created a pop-up menu named PopupMenu1, the following code will do the trick:

procedure TForm1.Icontray(var Msg: TMessage);
var
CursorPos : TPoint;
begin
if Msg.lParam = WM_RBUTTONDOWN then begin
   GetCursorPos(CursorPos);
   PopupMenu1.Popup(CursorPos.x, CursorPos.y);
end else
   inherited;
end;
Showing and hiding the form

Normally you will include a menu item in the pop-up menu to show the form, so all you have to do is write an event handler to call the Show method of the form and optionally remove the icon from the system tray:

procedure TForm1.mnuShowClick(Sender: TObject);
begin
Show;
// Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
end;
When the user closes the form, you can for example hide it instead of closing it and optionally show the icon again in the system tray if you have removed it when you made the form visible. For example:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Hide;
// Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
end;
Finishing the application

You can have a menu item in the pop-up menu to exit the application. In this case you have to write an event handler for its click event to remove the icon and finish the application:

procedure TForm1.mnuExitClick(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
Application.ProcessMessages;
Application.Terminate;
end;


Tags: tray 윈도우즈 Share on Facebook Share on X

◀ PREVIOUS
디렉토리 복사 루틴 (하부디렉토리까지 몽땅)
▶ NEXT
ShellExecute 실행 끝날때 까지 기다리기
  Comments 0
Login for comment
SIMILAR POSTS

Extracting a Program's Version Information (created at 2006-09-29)

ShellExecute 실행 끝날때 까지 기다리기 (created at 2006-09-29)

디렉토리 복사 루틴 (하부디렉토리까지 몽땅) (created at 2006-09-29)

디렉토리와 그 안에 있는 모든 파일 지우기 (created at 2006-09-29)

Create a standard windows shortcut file (created at 2006-09-29)

DirectShow에서 비디오 출력 크기 조정해주기 (created at 2006-09-29)

공유폴더 찾아주기 (created at 2006-09-29)

로컬 IP 알아내기 (created at 2006-09-29)

File Attribute 바꾸는 방법 (created at 2006-09-29)

스크린세이버 동작 못하게 막아주기 (created at 2006-09-29)

PC 셧다운 (shutdown) (created at 2006-09-29)

how to record keyboard & mouse event (created at 2006-09-29)

웹브라우져에 붙이기 위한 ActiveX 프로그래밍 방법 (created at 2006-09-29)

투명 윈도우 구현 방법 (created at 2006-09-29)

ActiveX를 Safe한 Component로 등록하는 방법 (created at 2006-09-29)

ActiveX의 등록 - Safe Code 넣어주기 (updated at 2023-12-17)

윈도우 찾아서 Top Most로 올려주기 (updated at 2023-12-17)

Application defined window call procedure (created at 2006-09-29)

Application Event써서 Keyboard 받아내기 (created at 2006-09-29)

Web Browser Control에서 Enter키 먹게 하기 (created at 2006-09-29)

String Find 알고리즘 (created at 2006-09-29)

HTTP Downloading Reference Source Code (created at 2006-09-29)

File Search Sample Code (created at 2006-09-29)

모니터 끄는 API (created at 2006-09-29)

wave 파일 mixing 하기. (웨이브 믹싱) (created at 2006-09-29)

ATL/ActiveX 에서 자바스크립트로 데이터(문자열) 보내기 (created at 2006-09-29)

System Log-Off, Suspend, Reboot, Shutdown 시키기 (created at 2006-09-29)

GDI+ 에서 이미지 반투명 처리하기.. (created at 2006-09-29)

자기자신 IP알아내기(로칼컴퓨터) (created at 2006-09-29)

프로세스명으로 프로세스 죽이는 함수 (created at 2006-09-29)

OTHER POSTS IN THE SAME CATEGORY

Web Browser Control에서 Enter키 먹게 하기 (created at 2006-09-29)

Application Event써서 Keyboard 받아내기 (created at 2006-09-29)

Application defined window call procedure (created at 2006-09-29)

윈도우 찾아서 Top Most로 올려주기 (updated at 2023-12-17)

ActiveX의 등록 - Safe Code 넣어주기 (updated at 2023-12-17)

ActiveX를 Safe한 Component로 등록하는 방법 (created at 2006-09-29)

투명 윈도우 구현 방법 (created at 2006-09-29)

웹브라우져에 붙이기 위한 ActiveX 프로그래밍 방법 (created at 2006-09-29)

how to record keyboard & mouse event (created at 2006-09-29)

PC 셧다운 (shutdown) (created at 2006-09-29)

스크린세이버 동작 못하게 막아주기 (created at 2006-09-29)

로컬 IP 알아내기 (created at 2006-09-29)

공유폴더 찾아주기 (created at 2006-09-29)

DirectShow에서 비디오 출력 크기 조정해주기 (created at 2006-09-29)

ShellExecute 실행 끝날때 까지 기다리기 (created at 2006-09-29)

디렉토리 복사 루틴 (하부디렉토리까지 몽땅) (created at 2006-09-29)

디렉토리와 그 안에 있는 모든 파일 지우기 (created at 2006-09-29)

Create a standard windows shortcut file (created at 2006-09-29)

File Attribute 바꾸는 방법 (created at 2006-09-29)

wave 파일 mixing 하기. (웨이브 믹싱) (created at 2006-09-29)

ATL/ActiveX 에서 자바스크립트로 데이터(문자열) 보내기 (created at 2006-09-29)

System Log-Off, Suspend, Reboot, Shutdown 시키기 (created at 2006-09-29)

GDI+ 에서 이미지 반투명 처리하기.. (created at 2006-09-29)

자기자신 IP알아내기(로칼컴퓨터) (created at 2006-09-29)

프로세스명으로 프로세스 죽이는 함수 (created at 2006-09-29)

빈폴더 찾아내기 (created at 2006-09-29)

IE 패치에 따른 object, embed, applet 대처 방안 (created at 2006-09-28)

Virtusertable (created at 2006-09-28)

웹서버 및 웹메일 설정 방법 (created at 2006-09-28)

음력-양력 변환기 (created at 2006-09-28)

UPDATES

햄과 소세지가 우리 몸에 일으키는 부작용 (updated at 2024-04-27)

글루코사민 vs. 콘드로이친: 무엇이 더 나은 관절 건강 보조제일까? (created at 2024-04-22)

광주 5·18 민주화운동 알린 테리 앤더슨 前 AP 기자 (created at 2024-04-22)

콘드로이친의 염증 감소효과 (updated at 2024-04-22)

코사민 DS - 글루코사민+콘드로이친 복합물이 함유된 퇴행성 관절 건강보조제 (updated at 2024-04-22)

삼겹살 먹을때 환상조합 (created at 2024-04-22)

일본 여중생의 특이한 취향 (created at 2024-04-22)

우리가 먹는 약물이 바꿔버린 생태계 (created at 2024-04-21)

일본에서 그린 상상속의 사무직과 현실속의 사무직 (updated at 2024-04-21)

북한 미대생들이 그린 북한 최고존엄 김정은 (created at 2024-04-21)

입사 후 1년도 되지 않은 회사에서 구조조정에 의한 퇴직 불응에 따른 해고 처리시 대응 가능한 방법 (updated at 2024-04-20)

한고은님의 옛날 사진 (updated at 2024-04-20)

소녀대 - Bye Bye Girl (updated at 2024-04-13)

대한민국 날씨 근황 (created at 2024-04-13)

성일종 인재육성 강조하며 이토 히로부미 언급 - 인재 키운 선례? (updated at 2024-04-13)

일제강점기가 더 살기 좋았을지도 모른다는 조수연 국민의힘 후보 - 친일파? (updated at 2024-04-13)

Marshall Ha님의 샤오미 SU7 시승기 - 테슬라의 일론 머스크님이 긴장할만한 느낌 (updated at 2024-04-09)

윙크하는 귀염둥이 반려견들 (created at 2024-04-08)

달콤 살벌한 고백 (created at 2024-04-08)

북한 최정예 공수부대 훈련 모습 (updated at 2024-04-02)

맛있었던 친구 어머니의 주먹밥이 먹고 싶어요 (created at 2024-04-02)

자리 마음에 안든다고 6급 공무원 패는 농협 조합장 (created at 2024-03-26)

85세 딸 짜장면 사주는 102세 어머니 (created at 2024-03-26)

1990년대 감각파 도둑 (created at 2024-03-26)

치매에 걸린 69살의 브루스 윌리스가 전부인 데미무어를 보고 한 말 (updated at 2024-03-22)

경제는 대통령이 살리는 것이 아닙니다 라던 윤석열대통령 - 상황 안좋아지자 여러 전략을 펼쳤지만, 부작용 속출했던 2024년의 봄 (updated at 2024-03-13)

극빈의 생활을 하고 배운것이 없는 사람은 자유가 뭔지도 모를 뿐 아니라 왜 개인에게 필요한지에 대한 필요성을 못느낀다는 윤석열 대통령 (updated at 2024-03-08)

조선일보를 안본다는 사람들이 말하는 그 이유 - 천황폐하, 전두환 각하, 김일성 장군 만세? (created at 2024-03-07)

광폭타이어를 장착하면 성능이 좋아질거라는 착각 (updated at 2024-03-03)

면허시험장에서 면허갱신하면 하루만에 끝나나? (updated at 2024-03-03)