首页 >> 文档 >> Delphi专题

利用Delphi实现系统状态栏图标

发布日期:2008-01-09最近更新:2008-01-09来源:BHCODE作者:BH
      1.创建一个应用程序,在主窗体上增加一个TpopupMenu组件。并为该弹出选单组件增加选单项Exit,标题为“退出”。  

  2.在Uses中添加ShellAPI,因为在系统状态栏中增加图标时需调用ShellAPI中的函数Shell_NotifyIconA。该函数需要两个参数,其中一个是TnotifyIconDataA结构,需在主窗体中增加TnotifyIconDataA类型的全局变量ntida。  

  3.定义消息mousemsg,并编写主窗体的mousemessage消息处理函数,此函数说明在图标上用鼠标左键单击时,会打开应用程序窗口;用鼠标右键单击时,会弹出一个选单。  

  下面给出步骤2和3的实现代码: 

  unit Unit1;
  interface
  uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Menus, shellapi;
  const
  mousemsg = wm_user + 1; //自定义消息,用于处理用户在图标上点击鼠标的事件
  iid = 100; //用户自定义数值,在TnotifyIconDataA类型全局变量ntida中使用
  type
  TForm1 = class(TForm)
  ......
  private
  //自定义消息处理函数,处理鼠标点击图标事件
  procedure mousemessage(var message: tmessage); message mousemsg;
  public
  { Public declarations }
  end;
  var
  Form1: TForm1;
  ntida: TNotifyIcondataA;
  //用于增加和删除系统状态图标
  implementation
  {$R .DFM}
  procedure TForm1.mousemessage(var message: tmessage);
  var
  mousept: TPoint; //鼠标点击位置
  begin
  inherited;
  if message.LParam = wm_rbuttonup then begin //用鼠标右键点击图标
  getcursorpos(mousept); //获取光标位置
  popupmenu1.popup(mousept.x, mousept.y);
  //在光标位置弹出选单
  end;
  if message.LParam = wm_lbuttonup then begin //用鼠标左键点击图标
  //显示应用程序窗口
  ShowWindow(Handle, SW_SHOW);
  //在任务栏上显示应用程序窗口
  ShowWindow(Application.handle, SW_SHOW);
  SetWindowLong(Application.Handle, GWL_EXSTYLE,
  not (GetWindowLong(Application.handle, GWL_EXSTYLE)
  or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW));
  end;
  message.Result := 0;
  end   
本周推荐
MORE
热点关注
MORE