微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

监视Windows窗体应用程序

我想监视一个winforms应用程序内的用户行为。

是否有一种通用的方法来挂接事件系统,而不必在每个表单或每个button上编写事件处理程序?

我想监视以下事件:

Windows打开

Windowsclosures

点击button

理想情况下:每种forms的时间花费

我不想订阅所有forms的所有事件,应用程序是为了大。 我只想钩住和监视所有事件。

文件比较工具

是否可以将软件注册为游戏控制器设备?

如何在Windows上获得区分大小写的path?

使用Mono部署Linux来部署aspnet web /网站应用程序?

更改显示configuration

构buildWindows服务的安装项目时出现问题?

当X509Certificate2对象的智能卡被移除时的事件

WCF应用程序无法打开频道

如何在LiveCharts中添加每点工具提示? 如何在同一行上使用不同颜色的点?

Mono真的是跨平台吗?

您不需要在每个窗体和每个控件中编写事件句柄。 你可以把逻辑放在一个基本的Form类中。

您可以简单地为您的项目创建一个基本表单,并将日志的逻辑放在那里。 在基本形式中,您可以使用代码订阅您需要的各种事件。

要应用该解决方案:

您不需要更改设计者或设计者生成代码。 从BaseForm导出所有的表单。

可以简单地使用find和replace all命令来完成。

还要创建下面的类,不要添加一个Form ,只需添加一个类,并使用下面的类:

基本形式

public class BaseForm : Form { public BaseForm() { if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return; this.Load += BaseForm_Load; this.FormClosed += BaseForm_FormClosed; } private IEnumerable<Control> GetAllControls(Control control) { var controls = control.Controls.Cast<Control>(); return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls); } void BaseForm_FormClosed(object sender,FormClosedEventArgs e) { Log(string.Format("{0} Closed",this.Name)); } void BaseForm_Load(object sender,EventArgs e) { Log(string.Format("{0} Opened",this.Name)); GetAllControls(this).OfType<Button>().ToList() .ForEach(x => x.Click += ButtonClick); } void ButtonClick(object sender,EventArgs e) { var button = sender as Button; if (button != null) Log(string.Format("{0} Clicked",button.Name)); } public void Log(string text) { var file = System.IO.Path.Combine(Application.StartupPath,"log.txt"); text = string.Format("{0} - {1}",DateTime.Now,text); System.IO.File.AppendAllLines(file,new string[] { text }); } }

注意

您可以使用日志库或任何其他机制进行日志记录。

您可以使用任何字符串格式的日志。

您可以简单地添加属性/方法来打开/关闭日志。

您可能想要登录一些其他有用的事件,如Application.ThreadException事件。

您可以简单地使用StopWatch来计算用户使用表格的时间。 您也可以简单地记录开始时间和结束时间,并将差异记录为用户使用表格的持续时间。

以下是你需要的用途:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows.Forms;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐