码迷,mamicode.com
首页 > Windows程序 > 详细

delphi 实现最小化系统托盘

时间:2015-06-23 17:50:43      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

1、new -->application


2、在form1中加入一个tPopMenu 命名为pm1


3、uses ShellAPI;


4、定义一个常量在
const
WM_TRAYMSG = WM_USER + 101;


5、在private中生命变量
NotifyIcon: TNotifyIconData;

6.声明两个消息

技术分享
 procedure TfrmMain.WMSysCommand(var Msg: TMessage); 
    begin
      if Msg.WParam = SC_ICON then
        Self.Visible := False
    else
      DefWindowProc(Self.Handle, Msg.Msg, Msg.WParam, Msg.LParam);
    end;


    procedure TfrmMain.WMTrayMsg(var Msg: TMessage);//声明托盘消息
    var
      p: TPoint;
    begin
      case Msg.LParam of
        WM_LBUTTONDOWN: Self.Visible := True;   //显示窗体
        WM_RBUTTONDOWN:
        begin
              SetForegroundWindow(Self.Handle);   //把窗口提前 
            GetCursorPos(p);
          pm1.Popup(p.X, p.Y);
        end;
     end;

    end;
View Code

7、oncreate中

技术分享
  with NotifyIcon do
  begin
    cbSize := SizeOf(TNotifyIconData);
    Wnd := Self.Handle;
    uID := 1;
    uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;   //图标、消息、提示信息
    uCallbackMessage := WM_TRAYMSG;
    hIcon := Application.Icon.Handle;
    szTip := erp服务;
  end;
  Shell_NotifyIcon(NIM_ADD, @NotifyIcon);

  //去掉关闭按钮
   EnableMenuItem(GetSystemMenu(Handle, FALSE), SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
View Code

8、ondestroy中
Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);

技术分享

整体代码

技术分享
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, Menus;

const
  WM_TRAYMSG = WM_USER + 101;

type
  TForm1 = class(TForm)
    pm1: TPopupMenu;
    N1: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    NotifyIcon: TNotifyIconData;
    procedure WMTrayMsg(var Msg: TMessage); message WM_TRAYMSG;    //声明托盘消息
    procedure WMSysCommand(var Msg: TMessage); message WM_SYSCOMMAND;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with NotifyIcon do
  begin
    cbSize := SizeOf(TNotifyIconData);
    Wnd := Self.Handle;
    uID := 1;
    uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;   //图标、消息、提示信息
    uCallbackMessage := WM_TRAYMSG;
    hIcon := Application.Icon.Handle;
    szTip := erp服务;
  end;
  Shell_NotifyIcon(NIM_ADD, @NotifyIcon);

  //去掉关闭按钮
  EnableMenuItem(GetSystemMenu(Handle, FALSE), SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
end;

{ TForm1 }

procedure TForm1.WMSysCommand(var Msg: TMessage);
begin
  if Msg.WParam = SC_ICON then
    Self.Visible := False
  else
    DefWindowProc(Self.Handle, Msg.Msg, Msg.WParam, Msg.LParam);
end;

procedure TForm1.WMTrayMsg(var Msg: TMessage);
var
  p: TPoint;
begin
  case Msg.LParam of
    WM_LBUTTONDOWN: Self.Visible := True;   //显示窗体
    WM_RBUTTONDOWN:
    begin
      SetForegroundWindow(Self.Handle);   //把窗口提前
      GetCursorPos(p);
      pm1.Popup(p.X, p.Y);
    end;
  end;

end;

end.
View Code

 //这是从网上找的在delphi7测试通过(引用的哪忘了)--- http://blog.csdn.net/akof1314/article/details/6411179 这个列牛B

delphi 实现最小化系统托盘

标签:

原文地址:http://www.cnblogs.com/SoftWareIe/p/4595837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!