一個托盤圖標組件

来源:https://www.cnblogs.com/adsoft/archive/2020/01/03/12146147.html
-Advertisement-
Play Games

最近在溫故Delphi精要,下麵是按照其中做的托盤圖標組件,記錄一下。 工具:Delphi 7+Image Editer 先上圖: 組件源碼如下:對於圖標,百度 unit XsdTrayIcon; interface uses SysUtils, Classes, Windows, Messages ...


最近在溫故Delphi精要,下麵是按照其中做的托盤圖標組件,記錄一下。

工具:Delphi 7+Image Editer

先上圖:

 

組件源碼如下:對於圖標,百度 

unit XsdTrayIcon;

interface

uses
  SysUtils, Classes, Windows, Messages, Graphics, Menus, ShellAPI, ExtCtrls,
  Forms, Registry;

const
  ICON_ID = 1;
  MI_ICONEVENT = WM_USER + 1;    //自定義一個消息

type
  TXsdTrayIcon = class(TComponent)
  private
    FHint: string;
    FOnDblClick: TNotifyEvent;
    FTrayIcon: TIcon;
    FPopMenu: TPopupMenu;
    FNotificationWnd: HWND;
    FStartAtBoot: Boolean;
    FInterval: Cardinal;
    TimerHandle: LongWord;
    NotifyIconData: TNotifyIconData;
    OldWindowProc: TWndMethod;
    procedure NotificationWndProc(var Message: TMessage);
    procedure SetTrayIcon(const Value: TIcon);
    procedure SetStartAtBoot(const Value: Boolean);
    procedure Registry(B: Boolean);
    procedure NewWindowProc(var Message: TMessage);
  protected
    procedure DoDblClick;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    (*
    Loaded 是TComponent 的一個虛擬方法。當所有組件被創建,並從dfm 文件讀出數據
    初始化這些組件實例後,Loaded 方法被自動調用。在Loaded 中可以進行額外的初始化
    工作,可以對組件實例的一些成員進行改變、嫁接
    *)
    procedure Loaded; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    //操作托盤正常顯示應用程式
    procedure RestoreAPP();
    procedure ShowTrayIcon(Mode: Cardinal = NIM_ADD; Animated: Boolean = False);
  published
    property Hint: string read FHint write FHint;
    property OnDoDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
    property PopMenu: TPopupMenu read FPopMenu write FPopMenu;
    property TrayIcon: TIcon read FTrayIcon write SetTrayIcon;
    //是否自動啟動
    property StartAtBoot: Boolean read FStartAtBoot write SetStartAtBoot;
    property Interval: Cardinal read FInterval write FInterval;
  end;

procedure Register;

implementation

var
  FXsdTrayIcon: TXsdTrayIcon ;
  
procedure Register;
begin
  RegisterComponents('XsdInfo', [TXsdTrayIcon]);
end;

{ TXsdTrayIcon }

constructor TXsdTrayIcon.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FXsdTrayIcon := Self;
  FTrayIcon := TIcon.Create;
  FInterval := 500;
  TimerHandle := 0;
  FNotificationWnd := Classes.AllocateHWnd(NotificationWndProc);
  if AOwner is TForm then
  begin
    OldWindowProc := TForm(AOwner).WindowProc;
    TForm(AOwner).WindowProc := NewWindowProc;
  end;
end;

destructor TXsdTrayIcon.Destroy;
begin
  ShowTrayIcon(NIM_DELETE); //刪除托盤圖標
  FreeAndNil(FTrayIcon);
  if FNotificationWnd<>0 then
    Classes.DeallocateHWnd(FNotificationWnd);  //銷毀視窗
  if TimerHandle<>0 then
    KillTimer(0, TimerHandle);  //關掉定時器
  inherited Destroy;
end;

procedure TXsdTrayIcon.DoDblClick;
begin
  if Assigned(OnDoDblClick) then OnDoDblClick(Self);
end;

procedure TXsdTrayIcon.Loaded;
begin
  inherited;
  if not (csDesigning in ComponentState) then
  begin
    if FTrayIcon.Handle=0 then
      FTrayIcon.Assign(Application.Icon);
    //初始化NotifiCationData;
    FillChar(NotifyIconData, SizeOf(NotifyIconData), 0);
    with NotifyIconData do
    begin
      cbSize := SizeOf(TNotifyIconData);
      Wnd := FNotificationWnd;
      uID := ICON_ID;
      uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
      uCallbackMessage := MI_ICONEVENT;
      hIcon := FTrayIcon.Handle;
      StrLCopy(szTip, PChar(FHint), SizeOf(szTip));
    end;
    ShowTrayIcon();
  end;
end;

procedure TXsdTrayIcon.NewWindowProc(var Message: TMessage);
begin
  if Assigned(OldWindowProc) then
    OldWindowProc(Message);
  with Message do
  begin
    if ((Msg=WM_SYSCOMMAND) and (WParam=SC_MINIMIZE)) then
      ShowWindow(Application.Handle, SW_HIDE);
  end;
end;

procedure TXsdTrayIcon.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation=opRemove then
  begin
    if AComponent=FPopMenu then FPopMenu := nil;
  end;
end;

procedure TXsdTrayIcon.NotificationWndProc(var Message: TMessage);
var
  PT: TPoint;
begin
  if Message.Msg=MI_ICONEVENT then
  begin
    case Message.LParam of
      WM_LBUTTONDBLCLK:
      begin
        DoDblClick;
        RestoreAPP;
      end;
      WM_RBUTTONDOWN:
      begin
        if Assigned(FPopMenu) then
        begin
          GetCursorPos(PT);
          FPopMenu.Popup(PT.X, PT.Y);
        end;
      end;
    end;
  end else //對於其它消息 預設處理。
    Message.Result := DefWindowProc(FNotificationWnd, Message.Msg, message.WParam, message.LParam);
end;

procedure SetAnimatedIcon(Wnd: HWND; Msg, idEvent: UINT; dwTime: DWORD); stdcall;
begin
  if Msg=wm_timer then
  with FXsdTrayIcon.NotifyIconData do
  begin
    if hIcon=0 then
      hIcon := FXsdTrayIcon.FTrayIcon.Handle
    else
      hIcon := 0;
    Shell_NotifyIcon(NIM_MODIFY, @FXsdTrayIcon.NotifyIconData);
  end;
end;

procedure TXsdTrayIcon.Registry(B: Boolean);
var
  Reg: TRegistry;
  KeyName: string;
begin
  Reg := TRegistry.Create;
  KeyName := ExtractFileName(Application.ExeName);
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False) then
    begin
      if B then
        Reg.WriteString(KeyName, Application.ExeName)
      else
        Reg.DeleteKey(KeyName);
      Reg.CloseKey;
    end;
  finally
    FreeAndNil(Reg);
  end;
end;

procedure TXsdTrayIcon.RestoreAPP;
begin
  ShowTrayIcon(NIM_MODIFY, False);
  ShowWindow(Application.Handle, SW_SHOWNORMAL);
  ShowWindow(Application.MainForm.Handle, SW_SHOWNORMAL);
  SetForegroundWindow(Application.MainForm.Handle);
end;

procedure TXsdTrayIcon.SetStartAtBoot(const Value: Boolean);
begin
  if FStartAtBoot<>Value then
  begin
    FStartAtBoot := Value;
    if not (csDesigning in ComponentState) then
      Registry(FStartAtBoot);
  end;
end;

procedure TXsdTrayIcon.SetTrayIcon(const Value: TIcon);
begin
  FTrayIcon := Value;
end;

procedure TXsdTrayIcon.ShowTrayIcon(Mode: Cardinal; Animated: Boolean);
begin
  if csDesigning in ComponentState then Exit;
  if Mode=NIM_MODIFY then
  begin
    if Animated then
    begin
      if TimerHandle=0 then
        TimerHandle := SetTimer(0, 0, FInterval, @SetAnimatedIcon);
    end else begin
      if TimerHandle<>0 then
      begin
        KillTimer(0, TimerHandle);
        TimerHandle := 0;
        NotifyIconData.hIcon := FTrayIcon.Handle;
      end;
    end;
  end;
  Shell_NotifyIcon(Mode, @NotifyIconData);
end;

end.

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 之前的博客里記錄了PHP解決跨域的方案:JSONP;https://www.cnblogs.com/pawn-i/p/11899120.html 除了jsonp之後,還是通過header函數設置響應頭解決跨域問題: 設置允許訪問的功能變數名稱: 設置允許訪問的請求方式: 然後根據需要再設置其他的參數…… ...
  • 1024是2的10次方,1024=2¹º。 在電腦中,1GB=1024MB,1MB=1024KB,1KB=1024Byte。 因此1024多指互聯網和科技公司,經常表示程式員,另外還表示一級棒的意思(1GB)。 image 996是個工作制,表示工作時間從早上9點到晚上9點,每周工作6天。也就是說 ...
  • 首先要使用composer來下載一個第三方擴展就可以實現php的websocket客戶端,直接在當前目錄生成下composer.json文件就可以了composer require textalk/websocket 配合php的讀取文件操作,只讀取最新的追加的內容,下麵代碼為讀取日誌的客戶端 , ...
  • 記錄一道學長們說有點難度的題目 好好玩啊這道題 ACM程式設計大賽是大學級別最高的腦力競賽,素來被冠以"程式設計的奧林匹克"的尊稱。大賽至今已有近40年的歷史,是世界範圍內歷史最悠久、規模最大的程式設計競賽。比賽形式是:從各大洲區域預賽出線的參賽隊伍,於指定的時間、地點參加世界級的決賽,由1個教練、 ...
  • 開發者工具(F12) 其中常用的有Elements(元素麵板)、Console(控制臺面板)、Sources(源代碼面板)、Network(網路面板) 找 JS 文件的幾種方法 1、找發起地址 2、設置事件觸發斷點 Event Listener Breakpoint 使用Sources面板上的Eve ...
  • 沒有什麼能比學以致用讓學習變得更有動力的了。 不知道大家在工作中有沒有一些工作需要重覆的點擊滑鼠,因為會影響到財務統計報表的關係,我們每個月底月初都要修改ERP中的單據日期,單據多的時候光修改就能讓你點滑鼠點到手麻。(這裡要吐槽一下浪沙軟體,別的單據都可以批量修改日期,就是這個移倉單不行,你們研發怎 ...
  • 告別枯燥,60秒學會一個Python小例子。奔著此出發點,我在過去1個月,將平時經常使用的代碼段換為小例子,分享出來後受到大家的喜歡。 一、基本操作 。 1 鏈式比較 i = 3 print(1 < i < 3) # False print(1 < i <= 3) # True 2 不用else和i ...
  • 一、發送純文本郵件 import smtplib from email.mime.text import MIMEText subject = "標題" # 郵件的主題 content = "測試" # 郵件的內容 sender = "[email protected]" # 發件人 passwor ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...