如何修改刘麻子的代码(30分)

  • 主题发起人 主题发起人 zqssoft
  • 开始时间 开始时间
Z

zqssoft

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的[如何检测用户多长时间没有鼠标与键盘操作]代码,如何修改,能用到带主窗口的程序中,比如用在form1.pas中,谢谢。<br>program UseHook;<br><br><br>//-----按 Icon.txt 生成 icon.Res 编译到exe中---------<br>{$R &nbsp; &nbsp;'../Icon/icon.Res' &nbsp;'../Icon/Icon.txt' }<br><br>uses<br>&nbsp; Windows,ShellAPI;<br><br>const<br>&nbsp; ClassName &nbsp; &nbsp; &nbsp; = 'LiuMaZi'; //窗体类名<br>&nbsp; WindowsName &nbsp; &nbsp; = 'LoveLiuMazi';//窗体标题<br>&nbsp; WM_COMMAND &nbsp; &nbsp; &nbsp;= $0111; &nbsp;//单击菜单项消息<br>&nbsp; WM_LBUTTONDOWN &nbsp;= $0201; &nbsp;//鼠标左键单击消息<br>&nbsp; WM_TIMER &nbsp; &nbsp; &nbsp; &nbsp;= $0113; &nbsp;//计时器函数<br>&nbsp; WM_KEYFIRST &nbsp; &nbsp; = $0100; &nbsp;//第一个键盘消息<br>&nbsp; WM_KEYLAST &nbsp; &nbsp; &nbsp;= $0108; &nbsp;//最后一个键盘消息<br>&nbsp; WM_MOUSEFIRST &nbsp; = $0200; &nbsp;//第一个鼠标消息<br>&nbsp; WM_MOUSELAST &nbsp; &nbsp;= $020A; &nbsp;//最后一个鼠标消息<br>&nbsp; WM_MyTray &nbsp; &nbsp; &nbsp; = $0800; &nbsp;//自定义消息,用于响应托盘操作<br><br>var<br>&nbsp; MyMessage : TMsg; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//消息结构<br>&nbsp; WinClass &nbsp;: TWndClass; &nbsp; &nbsp; &nbsp; //窗体类结构<br>&nbsp; WinHandle : hWnd; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//窗体句柄<br>&nbsp; PopupMenu : hMenu; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //菜单句柄<br>&nbsp; MousePos &nbsp;: TPoint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//鼠标位置<br>&nbsp; IconData &nbsp;: TNotifyIconData; //托盘结构<br>&nbsp; Count &nbsp; &nbsp; : integer; &nbsp; &nbsp; &nbsp; &nbsp; //累计经过的时间<br>&nbsp; state &nbsp; &nbsp; : Boolean=True; &nbsp; &nbsp; &nbsp; &nbsp; //当前状态,True为在线,False为离开<br>&nbsp; NextHook &nbsp;: LongWord;<br><br>&nbsp;//-----------窗体消息处理过程,回调函数-------<br>function WndProc(WinH : hWnd; WinMsg,WParam,LParam : Longint): Integer; StdCall;<br>Begin<br>&nbsp; Result := 0;<br>&nbsp; Case WinMsg of<br>&nbsp;//预定时间到<br>&nbsp; &nbsp; WM_TIMER &nbsp;:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if state=True then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Count:=Count+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{5秒后"离开"}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Count=5 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//换托盘图标为离开<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconData.HIcon:=LoadIcon(hInstance,'Icon2');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconData.UFlags:=NIF_ICON ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_MODIFY,@IconData);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//置当前状态为离开<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp;//鼠标单击菜单<br>&nbsp; &nbsp; WM_COMMAND:begin {菜单项ID}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case WParam of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{关于}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1: &nbsp;MessageBox(0,'刘浏 &nbsp; QQ &nbsp;: 71892967'+#13+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'http://Liumazi.efile.com.cn/'+#13+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'MSN: Liu_mazi@126.com ',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '象MSN那样判断用户离开',MB_OK);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{退出}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2: PostQuitMessage(0);//给线程消息队列送WM_QUIT退出消息<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp;//操作托盘图标<br>&nbsp; &nbsp; WM_MyTray :begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {鼠标左键}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (LParam = WM_LBUTTONDOWN) Then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //在鼠标当前位置处弹出菜单<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(WinHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCursorPos(MousePos);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TrackPopupMenu(PopupMenu,tpm_LeftAlign Or tpm_LeftButton,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MousePos.X,MousePos.Y,0,WinHandle,nil);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>//其他消息调用默认消息处理过程<br>&nbsp; &nbsp; else Result := DefWindowProc(WinH,WinMsg,WParam,LParam);<br>&nbsp; end;<br>end;<br>//-------钩子回调函数---(有消息时候该函数将被调用)-----------------<br>function HookProc( iCode:Integer; wParam:WPARAM; lParam:LPARAM ):LRESULT;stdcall;<br>begin<br>&nbsp; Result:=CallNextHookEx(NextHook,iCode,wParam,lParam);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //键盘消息<br>&nbsp; if ( (PEventMsg(lparam)^.message&gt;=WM_KEYFIRST)and<br>&nbsp; &nbsp; &nbsp;(PEventMsg(lparam)^.message&lt;=WM_KEYLAST) ) or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //鼠标消息<br>&nbsp; &nbsp; &nbsp;( (PEventMsg(lparam)^.message&gt;=WM_MOUSEFIRST)and<br>&nbsp; &nbsp; &nbsp;(PEventMsg(lparam)^.message&lt;=WM_MOUSELAST) )then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Count:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp;if state=false then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;//换托盘图标为离开<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconData.HIcon:=LoadIcon(hInstance,'Icon1');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconData.UFlags:=NIF_ICON ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Shell_NotifyIcon(NIM_MODIFY,@IconData);<br>&nbsp; &nbsp; &nbsp;//置当前状态为在线<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp;end;<br><br>//----------------主程序------------------<br>begin //找到则说明已有一个实例运行<br>&nbsp; if findwindow(Pchar(ClassName),Pchar(WindowsName))&lt;&gt;0 then<br>&nbsp; &nbsp; MessageBox(0,'程序已运行.....^_^ &nbsp;','',MB_OK)<br>&nbsp; else<br>&nbsp; begin<br>&nbsp;//填充窗体类结构体<br>&nbsp; &nbsp; With WinClass do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Style &nbsp; &nbsp; &nbsp; := 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{类风格}<br>&nbsp; &nbsp; &nbsp; lpfnWndProc := @WndProc; &nbsp; {窗口过程}<br>&nbsp; &nbsp; &nbsp; cbClsExtra &nbsp;:= 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{额外类信息}<br>&nbsp; &nbsp; &nbsp; cbWndExtra &nbsp;:= 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{额外窗口信息}<br>&nbsp; &nbsp; &nbsp; hIcon &nbsp; := 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{图标}<br>&nbsp; &nbsp; &nbsp; hCursor := 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{光标}<br>&nbsp; &nbsp; &nbsp; hbrBackground := 0; &nbsp; &nbsp; &nbsp; &nbsp;{颜色}<br>&nbsp; &nbsp; &nbsp; lpszMenuName &nbsp;:= nil; &nbsp; &nbsp; &nbsp;{菜单}<br>&nbsp; &nbsp; &nbsp; lpszClassName := ClassName;{类名称}<br>&nbsp; &nbsp; &nbsp; hInstance:=GetModuleHandle(nil);{实例句柄}<br>&nbsp; &nbsp; end;<br>&nbsp; //注册窗体类<br>&nbsp; &nbsp; RegisterClass(WinClass);<br>&nbsp; //建立一个窗体(不可见) &nbsp;<br>&nbsp; &nbsp; WinHandle :=CreateWindowEx(0,ClassName,WindowsName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ws_OverlappedWindow,0,0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,0,0,0,hInstance,nil);<br>&nbsp; //建立菜单<br>&nbsp; &nbsp; PopupMenu := CreatePopupMenu;<br>&nbsp; //依次添加菜单项<br>&nbsp; &nbsp; AppendMenu(PopupMenu,mf_Enabled &nbsp;Or mf_String,1,'&amp;Abort');<br>&nbsp; &nbsp; AppendMenu(PopupMenu,mf_Enabled Or mf_String,2,'E&amp;xit');<br>&nbsp; //填充托盘结构体<br>&nbsp; &nbsp; With IconData do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cbSize := SizeOf(IconData);<br>&nbsp; &nbsp; &nbsp; Wnd :=WinHandle; &nbsp;//指定所属窗体<br>&nbsp; &nbsp; &nbsp; uID := 100;<br>&nbsp; &nbsp; &nbsp; uFlags := nif_Icon Or nif_Message Or nif_Tip;<br>&nbsp; &nbsp; &nbsp; uCallBackMessage := WM_MyTray; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //指定消息ID<br>&nbsp; &nbsp; &nbsp; hIcon := LoadIcon(hInstance,'Icon1'); //指定托盘图标<br>&nbsp; &nbsp; &nbsp; szTip :='象MSN那样'+#13+'判断用户是否离开 &nbsp; &nbsp; '+#13+'Coder : 刘麻子 ';<br>&nbsp; &nbsp; end;<br>&nbsp; //添加托盘到系统任务栏<br>&nbsp; &nbsp; Shell_NotifyIcon(nim_Add,@IconData);<br>&nbsp; //创建定时器<br>&nbsp; &nbsp; SetTimer(WinHandle,1,1000,NIL);<br>&nbsp; //挂消息钩子<br>&nbsp; &nbsp; NextHook:=SetWindowsHookEx(WH_JOURNALRECORD, HookProc,HInstance,0);<br>//====消息循环==开始===========<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {此函数从消息队列取消息,遇WM_QUIT返回Fasle,从而导致循环结束}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {如果队列内没消息,则当前线程挂起,直到有消息进入队列}<br>&nbsp; &nbsp; While GetMessage(MyMessage,0,0,0) do<br>&nbsp; &nbsp; &nbsp; DispatchMessage(MyMessage);//分发消息到相应窗体消息处理过程<br>//====消息循环==结束===========<br><br>//卸载钩子 &nbsp;<br>&nbsp; &nbsp;UnHookWindowsHookEx(NextHook);<br>//删除定时器<br>&nbsp; &nbsp; KillTimer(WinHandle,1);<br>//释放菜单<br>&nbsp; &nbsp; DestroyMenu(PopupMenu);<br>//释放托盘<br>&nbsp; &nbsp; IconData.cbSize:=0;<br>&nbsp; &nbsp; Shell_NotifyIcon(NIM_DELETE,@IconData);<br>//释放窗体<br>&nbsp; &nbsp; DestroyWindow(WinHandle);<br>&nbsp; end;<br>&nbsp; <br>end.
 
给钱就搞
 

Similar threads

后退
顶部