怎么开发Windows服务,而且还能有托盘图标

武稀松 2004-04-19 07:39:05
怎么开发Windows服务,而且还能有托盘图标。有窗体更好
...全文
447 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcoder 2004-04-22
  • 打赏
  • 举报
回复
晕,看这个吧

http://www.csdn.net/develop/read_article.asp?id=20794
netcoder 2004-04-22
  • 打赏
  • 举报
回复
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Data.SqlClient; 
using System.Diagnostics; 
using System.ServiceProcess; namespace CodeGuru.MyWindowsService 

 public class MyService : System.ServiceProcess.ServiceBase 
 { 
  private System.Timers.Timer timer1; 
  /// <remarks> 
  /// Required designer variable. 
  /// </remarks> 
  private System.ComponentModel.Container components = null;   public MyService() 
  { 
    // This call is required by the Windows.Forms 
    // Component Designer. 
   InitializeComponent(); 
  }   // The main entry point for the process 
  static void Main() 
  { 
   System.ServiceProcess.ServiceBase[] ServicesToRun; 
  
   ServicesToRun = new System.ServiceProcess.ServiceBase[] 
{ new MyService() };    System.ServiceProcess.ServiceBase.Run(ServicesToRun); 
  }   /// <summary> 
  /// Required method for Designer support - do not modify 
  /// the contents of this method with the code editor. 
  /// </summary> 
  private void InitializeComponent() 
  { 
   this.timer1 = new System.Timers.Timer(); 
   ((System.ComponentModel.ISupportInitialize) 
(this.timer1)).BeginInit(); 
   // 
   // timer1 
   // 
   this.timer1.Interval = 30000; 
   this.timer1.Elapsed += 
  new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); 
   // 
   // MyService 
   // 
   this.ServiceName = "My Sample Service"; 
   ((System.ComponentModel.ISupportInitialize) 
(this.timer1)).EndInit();   }   /// <summary> 
  /// Clean up any resources being used. 
  /// </summary> 
  protected override void Dispose( bool disposing ) 
  { 
   if( disposing ) 
   { 
   if (components != null) 
   { 
     components.Dispose(); 
   } 
   } 
   base.Dispose( disposing ); 
  }   /// <summary> 
  /// Set things in motion so your service can do its work. 
  /// </summary> 
  protected override void OnStart(string[] args) 
  { 
   this.timer1.Enabled = true; 
   this.LogMessage("Service Started"); 
  }   /// <summary> 
  /// Stop this service. 
  /// </summary> 
  protected override void OnStop() 
  { 
   this.timer1.Enabled = false; 
   this.LogMessage("Service Stopped"); 
  }   /* 
  * Respond to the Elapsed event of the timer control 
  */ 
  private void timer1_Elapsed(object sender, 
System.Timers.ElapsedEventArgs e) 
  { 
   this.LogMessage("Service Running"); 
  }   /* 
  * Log specified message to database 
  */ 
  private void LogMessage(string Message) 
  { 
   SqlConnection connection = null; 
   SqlCommand command = null; 
   try 
   { 
   connection = new SqlConnection( 
"Server=localhost;Database=SampleDatabase;Integrated 
Security=false;User Id=sa;Password=;"); 
command = new SqlCommand( 
"INSERT INTO MyServiceLog (vc_Status, dt_Created) 
valueS ('" + Message + "',getdate())", connection); 
   connection.Open(); 
   int numrows = command.ExecuteNonQuery(); 
   } 
   catch( Exception ex ) 
   { 
   System.Diagnostics.Debug.WriteLine(ex.Message); 
   } 
   finally 
   { 
   command.Dispose(); 
   connection.Dispose(); 
   } 
  } 
 } 
netcoder 2004-04-22
  • 打赏
  • 举报
回复
我们将研究如何创建一个作为Windows服务的应用程序。内容包含什么是Windows服务,如何创建、安装和调试它们。会用到System.ServiceProcess.ServiceBase命名空间的类。 

什么是Windows服务? Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户消息都会被写进Windows事件日志。计算机启动时,服务会自动开始运行。它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。 Windows 服务,以前的NT服务,都是被作为Windows NT*作系统的一部分引进来的。它们在Windows 9x及Windows Me下没有。你需要使用NT级别的*作系统来运行Windows服务,诸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。 
创建一个Windows服务 
我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已经启动了。在服务运行期间,它会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向Windows应用程序日志当中登记下它成功启动或停止时的记录。 Visual Studio .NET能够使创建一个Windows服务变成相当简单的一件事情。启动我们的演示服务程序的说明概述如下。 1. 新建一个项目 
2. 从一个可用的项目模板列表当中选择Windows服务 
3. 设计器会以设计模式打开 
4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer) 
5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒 
6. 切换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能 
Windows服务的构成 
在你类后面所包含的代码里,你会注意到你所创建的Windows服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的Windows服务必须扩充这个类。它会要求你的服务重载下面的方法,Visual Studio默认时包括了这些方法。 • Dispose – 清除任何受控和不受控资源(managed and unmanaged resources) 
• OnStart – 控制服务启动 
• OnStop – 控制服务停止 数据库表脚本样例 
在这个例子中使用的数据库表是使用下面的T-SQL脚本创建的。我选择SQL Server数据库。你可以很容易修改这个例子让它在Access或任何你所选择的别的数据库下运行。 CREATE TABLE [dbo].[MyServiceLog] ( 
  [in_LogId] [int] IDENTITY (1, 1) NOT NULL, 
  [vc_Status] [nvarchar] (40) 
      COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, 
  [dt_Created] [datetime] NOT NULL 
) ON [PRIMARY] 
Windows服务样例 
下面就是我命名为MyService的Windows服务的所有源代码。大多数源代码是由Visual Studio自动生成的
这几天没事做,研究了下Delphi的托盘编程 。现在很多程序都用这个,比如傲游,迅雷,==,很方便。主要代码如下: uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI, AppEvnts, StdCtrls, Menus; const WM_NID = WM_User + 1000; //声明一个常量 private { Private declarations } // 定义两个函数 procedure SysCommand(var SysMsg: TMessage); message WM_SYSCOMMAND; procedure WMNID(var msg:TMessage); message WM_NID; public end; var Form1: TForm1; NotifyIcon: TNotifyIconData; // 全局变量 implementation {$R *.dfm} procedure TForm1.WMNID(var msg:TMessage); var mousepos: TPoint; begin GetCursorPos(mousepos); //获取鼠标位置 case msg.LParam of WM_LBUTTONUP: // 在托盘区点击左键后 begin Form1.Visible := not Form1.Visible; // 显示主窗体与否 Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 显示主窗体后删除托盘区的图标 SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW); // 在任务栏显示程序 end; WM_RBUTTONUP: PopupMenu1.Popup(mousepos.X, mousepos.Y); // 弹出菜单 end; end; procedure TForm1.FormDestroy(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标 end; procedure TForm1.SysCommand(var SysMsg: TMessage); begin case SysMsg.WParam of SC_MINIMIZE: // 当最小化时 begin SetWindowPos(Application.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_HIDEWINDOW); Hide; // 在任务栏隐藏程序 // 在托盘区显示图标 with NotifyIcon do begin cbSize := SizeOf(TNotifyIconData); Wnd := Handle; uID := 1; uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; uCallBackMessage := WM_NID; hIcon := Application.Icon.Handle; szTip := '托盘程序'; end; Shell_NotifyIcon(NIM_ADD, @NotifyIcon); // 在托盘区显示图标 end; else inherited; end; end; {以下三个函数为托盘右键菜单,可自行添加功能} procedure TForm1.N1Click(Sender: TObject); begin ShowMessage('Delphi托盘小程序,Code by Zero Zhang'); end; procedure TForm1.N2Click(Sender: TObject); begin Form1.Visible := true; // 显示窗体 SetWindowPos(Application.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW); Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); // 删除托盘图标 end; procedure TForm1.N3Click(Sender: TObject); begin Shell_NotifyIcon(NIM_DELETE, @NotifyIcon); Application.Terminate; end; end.

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧