[up有分,winform]鼠标键盘n长时间无操作,程序自动注销/关闭

csShooter 2006-12-18 06:24:21
一个程序,要求实现如果鼠标键盘n长时间对本程序操作的情况下,程序自动注销!

类似windows的屏保功能!

感谢各位给点儿思路?
----------------------------------
C/S程序能不能向B/S一样有session/coocke生成周期的说法??
...全文
1110 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐的毛毛虫 2006-12-19
  • 打赏
  • 举报
回复
http://www.codeproject.com/csharp/globalsystemhook.asp
alger2001 2006-12-19
  • 打赏
  • 举报
回复
to:wdy9927()

------------------------
多谢你写的代码

我把你写的程序copy到一个新建项目中,按照你的程序添加了
label和textbox

编译能通过,可是执行后没有任何反应,不知道我哪里写的有问题,你能给我一份你写的那个完整程序吗?谢谢了
alger@xinhuanet.com
woshibai112 2006-12-19
  • 打赏
  • 举报
回复
up
wdy9927 2006-12-19
  • 打赏
  • 举报
回复
监视本程序的鼠标把
private const int WH_MOUSE_LL = 14;
改成 7


监视键盘用 OnKeyDown()



csShooter 2006-12-19
  • 打赏
  • 举报
回复


能否有办法知道用户当前操作的窗口(主窗口/主窗口弹出的子窗口)是在本程序进程里面????

这个问题解决了就好办!! 师兄们UP
wdy9927 2006-12-19
  • 打赏
  • 举报
回复
我要判断单个程序!
----------------------
晕,如果是单个程序就不用这么复杂了。

上面那个是对全局的键盘,鼠标都好使,就是说,把你的程序最小化了,不是当前活动程序也好使。
w_lion 2006-12-19
  • 打赏
  • 举报
回复
我建议直接写个屏保程序
csShooter 2006-12-19
  • 打赏
  • 举报
回复
wdy9927()
--------------
感谢师兄,好心人啊!



believefym(feng)
==================================
楼主到底是鼠标键盘对该程序无操作还是对系统无操作?



我要判断单个程序!


ncjcz 2006-12-19
  • 打赏
  • 举报
回复
up
believefym 2006-12-19
  • 打赏
  • 举报
回复
一个程序,要求实现如果鼠标键盘n长时间对本程序操作的情况下,程序自动注销!
----------
楼主到底是鼠标键盘对该程序无操作还是对系统无操作?
yellowen170161599 2006-12-19
  • 打赏
  • 举报
回复
捕捉鼠标的运动啊,如果超过多少时间 鼠标或者键盘没动的话就开始记时,记到XXX秒或分钟 就弹出什么东东出来,现在我发现原来软件的东西有时候原理是很简单的,只是我们没想到,

路过的顺便来学习下
wdy9927 2006-12-19
  • 打赏
  • 举报
回复

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId);

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam);

//Declare the wrapper managed POINT class.
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}

//Declare the wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

//声明键盘钩子的封送结构类型
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
}

private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
{
this.label2.Text = "";
// if ok and someone listens to our events
if ((nCode >= 0))
{
// //Marshall the data from callback.
MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

switch (wParam)
{
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
this.label2.Text = "Left : ";
break;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
this.label2.Text = "Right : ";
break;
case WM_MOUSEWHEEL:
this.label2.Text = "Wheel : ";
break;
}
this.label2.Text += mouseHookStruct.pt.x.ToString() + " " + mouseHookStruct.pt.y.ToString();
}
//call next hook
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
{
//it was ok and someone listens to events
if ((nCode >= 0))
{
//read structure KeyboardHookStruct at lParam
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
//raise KeyDown
if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
this.textBox1.Text +=" -" + keyData.ToString();
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

}
}
wdy9927 2006-12-19
  • 打赏
  • 举报
回复
//简单写了一个
//用来监视全局鼠标和键盘

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


using System.Runtime;
using System.Runtime.InteropServices;
using System.Reflection;
namespace hookKeyMose
{
/// <summary>
/// Summary description for Form1.
///
/// http://www.cnblogs.com/michaelxu/archive/2006/09/22/511557.aspx
/// http://support.microsoft.com/default.aspx?scid=kb;en-us;318804
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


HookProc MouseHookProcedure;
HookProc KeyboardHookProcedure;
static int hHook = 0;

public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
/**//// <summary>
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
/// </summary>
private const int WH_MOUSE_LL = 14;
/**//// <summary>
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events.
/// </summary>
private const int WH_KEYBOARD_LL = 13;

private const int WM_MOUSEMOVE = 0x200;

private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
private const int WM_LBUTTONDBLCLK = 0x203;

private const int WM_RBUTTONDOWN = 0x204;
private const int WM_RBUTTONUP = 0x205;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_MBUTTONUP = 0x208;
private const int WM_MBUTTONDBLCLK = 0x209;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private const int WM_MOUSEWHEEL = 0x20A;

private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;

private const int WM_SYSKEYDOWN = 0x0104;
private const int WM_SYSKEYUP = 0x0105;


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

MouseHookProcedure = new HookProc(this.MouseHookProc);
KeyboardHookProcedure = new HookProc(this.KeyboardHookProc);
//install hook
int hMouseHook = SetWindowsHookEx(
WH_MOUSE_LL,//原来是WH_MOUSE
MouseHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);


//install hook
int hKeyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL, //原来是WH_KEYBORAD
KeyboardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Mouse: ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(208, 16);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(32, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Key:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 56);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(416, 20);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(512, 102);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.TopMost = true;
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

阿非 2006-12-19
  • 打赏
  • 举报
回复
mark
csShooter 2006-12-19
  • 打赏
  • 举报
回复
以上方案针对整个系统还可以,全局Hook+Timer,初步可以解决!


但我想知道如何解决本系统无操作问题。


当然,添加一个组合也行:if(MainForm.IsActive){全局监视OK计时}else{计时}
csShooter 2006-12-19
  • 打赏
  • 举报
回复
感谢各位师兄!

一定把最佳方案帖在后面!
liuxx6 2006-12-19
  • 打赏
  • 举报
回复

顶一下,再借楼主的人气发一条找人信息。

赴日软件工程师:

1.两年工作经验,至少有一年软件项目经验
2.日语可基本交流,能流利交流更佳
3.学历:本科学士学位,计算机相关专业,或大专(正规全日制大专,同时拥有信息产业部的程序员证书)


国内开发部SE:

1.熟悉java、 vb、.net其中一种;
2.有一定日语基础,能看懂日文式样书;
3.两年以上软件开发经验。

联系方式:hudiemeng80@hotmail.com(邮件并MSN)
badcw 2006-12-19
  • 打赏
  • 举报
回复
up
coowoo 2006-12-19
  • 打赏
  • 举报
回复
再次推荐
用Application.AddMessageFilter

如果非要用hook也尽量不要用全局的,对系统性能影响太大
csShooter 2006-12-19
  • 打赏
  • 举报
回复
没有更好的办法也就只有这么着了!呆办法解决!
加载更多回复(15)

111,125

社区成员

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

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

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