﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-貝殼兒的寶貝匣子</title><link>http://www.cnitblog.com/chessxu/</link><description>有一天,可愛的小狗狗問媽媽:幸福是什麼?
狗媽媽說:幸福其實就是你的尾巴.
小狗狗拼命咬自己的尾巴,但是它都咬不到
"媽媽,為什麼我抓不住我的幸福?"
"傻孩子,抬起頭去走,幸福就會跟著你的!" </description><language>zh-cn</language><lastBuildDate>Wed, 29 Apr 2026 05:58:19 GMT</lastBuildDate><pubDate>Wed, 29 Apr 2026 05:58:19 GMT</pubDate><ttl>60</ttl><item><title>一個返回它所有上級科目名稱的SQL函數</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/24/4862.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Wed, 23 Nov 2005 23:56:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/24/4862.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4862.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/24/4862.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4862.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4862.html</trackback:ping><description><![CDATA[CREATE FUNCTION Getacctype(@accid varchar(20))<BR>RETURNS varchar(300) AS&nbsp; <BR>BEGIN <BR>declare @i int,@accidup varchar(20), @str varchar(300)<BR>set @accid=@accid+'.'<BR>set @accidup=''<BR>set @str=''<BR>set @i=1<BR>while CHARINDEX('.',@accid)&gt;0<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; if @accidup='' <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @accidup=substring(@accid, 1,CHARINDEX('.',@accid)-1)<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @accidup=@accidup+'.'+substring(@accid, 1,CHARINDEX('.',@accid)-1)<BR>&nbsp;&nbsp;&nbsp; set @accid=substring(@accid,CHARINDEX('.',@accid)+1,len(@accid)-CHARINDEX('.',@accid))<BR>&nbsp;&nbsp;&nbsp; if&nbsp; @str='' <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @str= (select acctype from HD_AccType where <A href="mailto:accid=@accidup">accid=@accidup</A>)<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set&nbsp; @str=@str+'-&gt;'+&nbsp; (select acctype&nbsp; from HD_AccType where <A href="mailto:accid=@accidup">accid=@accidup</A>)<BR>end<BR>return&nbsp; @str<BR>END<BR><img src ="http://www.cnitblog.com/chessxu/aggbug/4862.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-24 07:56 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/24/4862.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>小寫轉大寫的函數</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/14/4463.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Mon, 14 Nov 2005 08:23:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/14/4463.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4463.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/14/4463.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4463.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4463.html</trackback:ping><description><![CDATA[function SmallToBig(Small:Double): string;<BR>var<BR>i,m:integer;<BR>str:string;<BR>sx:string[14];<BR>x:double;<BR>n:array[1..10] of string;<BR>p:array[1..5] of string;<BR>begin<BR>n[10]:='零'; n[1]:='壹'; n[2]:='貳'; n[3]:='參'; n[4]:='肆'; n[5]:='伍';<BR>n[6]:='陸';&nbsp; n[7]:='柒'; n[8]:='捌';&nbsp; n[9]:='玖';&nbsp; p[1]:='拾'; p[2]:='佰';<BR>p[3]:='仟';&nbsp; p[4]:='萬'; p[5]:='億';<BR>//-----------------------------------------------------<BR>if small&lt;=9999999999.99 then<BR>begin<BR>x:=small;<BR>sx:=floattostr(int(x));<BR>for i:=1 to 10-length(sx) do<BR>sx:='a'+sx;<BR>{十億位}<BR>if (sx[1]&lt;&gt;'a')and(sx[1]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[1]);<BR>&nbsp; str:=str+n[m]+p[1];<BR>&nbsp; end;<BR>{億位}<BR>if (sx[2]&lt;&gt;'a')and(sx[2]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[2]);<BR>&nbsp; str:=str+n[m]+p[5];<BR>&nbsp; end;<BR>if sx[2]='0' then<BR>&nbsp; str:=str+p[5];<BR>{仟萬位}<BR>if (sx[3]&lt;&gt;'a')and(sx[3]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[3]);<BR>&nbsp; str:=str+n[m]+p[3];<BR>&nbsp; end;<BR>if (sx[3]&lt;&gt;'a')and(sx[3]='0')and(sx[4]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{佰萬位}<BR>if (sx[4]&lt;&gt;'a')and(sx[4]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[4]);<BR>&nbsp; str:=str+n[m]+p[2];<BR>&nbsp; end;<BR>if (sx[4]&lt;&gt;'a')and(sx[4]='0')and(sx[5]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{拾萬位}<BR>if (sx[5]&lt;&gt;'a')and(sx[5]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[5]);<BR>&nbsp; str:=str+n[m]+p[1];<BR>&nbsp; end;<BR>if (sx[5]&lt;&gt;'a')and(sx[5]='0')and(sx[6]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{萬位}<BR>if (sx[6]&lt;&gt;'a')and(sx[6]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[6]);<BR>&nbsp; str:=str+n[m]+p[4];<BR>&nbsp; end<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp; if ((sx[3]&lt;&gt;'0')and(sx[3]&lt;&gt;'a'))or((sx[4]&lt;&gt;'0')and(sx[4]&lt;&gt;'a'))or((sx[5]&lt;&gt;'0')and(sx[5]&lt;&gt;'a')) then<BR>&nbsp;&nbsp; str:=str+p[4];<BR>if (sx[6]='0')and(sx[3]='0')and(sx[4]='0')and(sx[5]='0')and(sx[7]&lt;&gt;'0')then<BR>&nbsp;&nbsp;&nbsp; str:=str+'零';<BR>{仟位}<BR>if (sx[7]&lt;&gt;'a')and(sx[7]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[7]);<BR>&nbsp; str:=str+n[m]+p[3];<BR>&nbsp; end;<BR>if (sx[7]&lt;&gt;'a')and(sx[7]='0')and(sx[8]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{佰位}<BR>if (sx[8]&lt;&gt;'a')and(sx[8]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[8]);<BR>&nbsp; str:=str+n[m]+p[2];<BR>&nbsp; end;<BR>if (sx[8]&lt;&gt;'a')and(sx[8]='0')and(sx[9]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{拾位}<BR>if (sx[9]&lt;&gt;'a')and(sx[9]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[9]);<BR>&nbsp; str:=str+n[m]+p[1];<BR>&nbsp; end;<BR>if (sx[9]&lt;&gt;'a')and(sx[9]='0')and(sx[10]&lt;&gt;'0') then<BR>&nbsp; str:=str+'零';<BR>{個位}<BR>if (sx[10]&lt;&gt;'a')and(sx[10]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[10]);<BR>&nbsp; str:=str+n[m];<BR>&nbsp; end;<BR>//--------------------------------------------------------------<BR>if frac(x)&lt;&gt;0 then<BR>begin<BR>sx:=formatfloat('#.###',(frac(x)));<BR>if length(sx)=2 then<BR>sx:=sx+'0';<BR>if int(x)&lt;&gt;0 then<BR>begin<BR>if (sx[2]&lt;&gt;'0')and(sx[3]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[2]);<BR>&nbsp; str:=str+'圓'+n[m]+'角';<BR>&nbsp; m:=strtoint(sx[3]);<BR>&nbsp; str:=str+n[m]+'分整';<BR>&nbsp; end;<BR>if (sx[2]='0')and(sx[3]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[3]);<BR>&nbsp; str:=str+'圓零'+n[m]+'分整';<BR>&nbsp; end;<BR>if (sx[2]&lt;&gt;'0')and(sx[3]='0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[2]);<BR>&nbsp; str:=str+'圓'+n[m]+'角整';<BR>&nbsp; end;<BR>if (sx[2]='0')and(sx[3]='0') then<BR>&nbsp; begin<BR>&nbsp; str:=str+'圓整';<BR>&nbsp; end;<BR>end;<BR>if int(x)=0 then<BR>begin<BR>if (sx[2]&lt;&gt;'0')and(sx[3]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[2]);<BR>&nbsp; str:=str+n[m]+'角';<BR>&nbsp; m:=strtoint(sx[3]);<BR>&nbsp; str:=str+n[m]+'分整';<BR>&nbsp; end;<BR>if (sx[2]='0')and(sx[3]&lt;&gt;'0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[3]);<BR>&nbsp; str:=str+n[m]+'分整';<BR>&nbsp; end;<BR>if (sx[2]&lt;&gt;'0')and(sx[3]='0') then<BR>&nbsp; begin<BR>&nbsp; m:=strtoint(sx[2]);<BR>&nbsp; str:=str+n[m]+'角整';<BR>&nbsp; end;<BR>if (sx[3]='0')and(sx[4]='0') then<BR>&nbsp; begin<BR>&nbsp; str:=str+'';<BR>&nbsp; end;<BR>end;<BR>&nbsp;end<BR>&nbsp; else<BR>&nbsp;&nbsp;&nbsp; if int(x)&lt;&gt;0 then<BR>&nbsp;&nbsp;&nbsp; str:=str+'圓整';<BR>&nbsp; smalltobig:=str;<BR>end<BR>else<BR>Application.MessageBox('超出範圍,不能轉換!!!',Pchar(Application.Title),MB_OK+MB_ICONWARNING);<BR>end;<img src ="http://www.cnitblog.com/chessxu/aggbug/4463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-14 16:23 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/14/4463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>獲得服務器的時間的函數</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/14/4440.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Mon, 14 Nov 2005 00:58:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/14/4440.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4440.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/14/4440.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4440.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4440.html</trackback:ping><description><![CDATA[//得到SQL服務器時間的函數;<BR>function GetDateTime:TDateTime;<BR>var<BR>&nbsp; TempDatetime:TDatetime;<BR>begin<BR>QAPubDataOpen('select getdate() as stime');<BR>TempDatetime:=ADataModule.QApubData.FieldByName('stime').AsDateTime;<BR>Result:=TempDatetime;<BR>end;<BR><BR>//得到Oracle服務器時間的函數;<BR>function GetDateTime:TDateTime;<BR>var<BR>&nbsp; TempDatetime:TDatetime;<BR>begin<BR>QAPubDataOpen('SELECT sysdate asysdate FROM dual');<BR>TempDatetime:=ADMSystem.QApubData.FieldByName('asysdate').AsDateTime;<BR>Result:=TempDatetime;<BR>end;<img src ="http://www.cnitblog.com/chessxu/aggbug/4440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-14 08:58 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/14/4440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>獲得本地電腦IP的函數</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/14/4438.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Mon, 14 Nov 2005 00:55:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/14/4438.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4438.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/14/4438.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4438.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4438.html</trackback:ping><description><![CDATA[function GetIP:String;<BR>var<BR>WSData: TWSAData;<BR>Buffer: array[0..63] of Char;<BR>HostEnt: PHostEnt;<BR>PPInAddr: ^PInAddr;<BR>IPString: String;<BR>begin<BR>IPString:='\';<BR>try<BR>&nbsp; WSAStartUp($101, WSData);<BR>&nbsp; GetHostName(Buffer, SizeOf(Buffer));<BR>&nbsp; HostEnt:=GetHostByName(Buffer);<BR>&nbsp; if Assigned(HostEnt) then<BR>&nbsp; begin<BR>&nbsp;&nbsp; PPInAddr:=@(PInAddr(HostEnt.H_Addr_List^));<BR>&nbsp;&nbsp; while Assigned(PPInAddr^) do<BR>&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; IPString:=StrPas(INet_NToA(PPInAddr^^));<BR>&nbsp;&nbsp;&nbsp; Inc(PPInAddr);<BR>&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>&nbsp; Result := IPString;<BR>finally<BR>&nbsp; try<BR>&nbsp;&nbsp; WSACleanUp;<BR>&nbsp; except<BR>&nbsp; end;<BR>end;<BR>end;<img src ="http://www.cnitblog.com/chessxu/aggbug/4438.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-14 08:55 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/14/4438.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>獲得電腦名稱的函數</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/14/4437.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Mon, 14 Nov 2005 00:54:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/14/4437.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4437.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/14/4437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4437.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4437.html</trackback:ping><description><![CDATA[function GetPCName:AnsiString;<BR>var CNameBuffer:PChar;<BR>&nbsp;&nbsp;&nbsp; fl_loaded:Boolean;<BR>&nbsp;&nbsp;&nbsp; CLen:^DWord;<BR>&nbsp;&nbsp;&nbsp; SResult:AnsiString;<BR>begin<BR>GetMem(CNameBuffer,255);<BR>New(CLen);<BR>CLen^:= 255;<BR>fl_loaded := GetComputerName(CNameBuffer,CLen^);<BR>if fl_loaded then<BR>&nbsp;&nbsp; SResult:= StrPas(CNameBuffer)<BR>else<BR>&nbsp;&nbsp; SResult:= 'Unkown';<BR>FreeMem(CNameBuffer,255);<BR>Dispose(CLen);<BR>Result:=SResult;<BR>end;<img src ="http://www.cnitblog.com/chessxu/aggbug/4437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-14 08:54 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/14/4437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>關於cxDBTreeList的父節點.</title><link>http://www.cnitblog.com/chessxu/archive/2005/11/12/4384.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Sat, 12 Nov 2005 01:18:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/12/4384.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/4384.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/12/4384.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/4384.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/4384.html</trackback:ping><description><![CDATA[看到一些財務軟件的科目樹狀結構中.經常抓取子科目時,也會把父科目的文本也加上,顯示出來,<BR>研究了幾個小時,終於得出.<BR><BR>var<BR>&nbsp;&nbsp;&nbsp; S:string;<BR>&nbsp;&nbsp;&nbsp; aNode:TcxTreeListNode;<BR>&nbsp;&nbsp;&nbsp; i:Integer;<BR>begin<BR>aNode:=cxDBTreeList1.FocusedNode;<BR>for i:=aNode.Level downto 0 do<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; if i=cxDBTreeList1.FocusedNode.Level then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S:=aNode.Values[1]<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S:=aNode.Values[1]+'-&gt;'+S;<BR>&nbsp;&nbsp;&nbsp; aNode:=aNode.Parent;<BR>&nbsp; end;<BR>ShowMessage(S);<BR>end;<img src ="http://www.cnitblog.com/chessxu/aggbug/4384.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-12 09:18 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/12/4384.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DELPHI中的键盘操作 </title><link>http://www.cnitblog.com/chessxu/archive/2005/11/04/3860.html</link><dc:creator>貝殼兒</dc:creator><author>貝殼兒</author><pubDate>Fri, 04 Nov 2005 01:45:00 GMT</pubDate><guid>http://www.cnitblog.com/chessxu/archive/2005/11/04/3860.html</guid><wfw:comment>http://www.cnitblog.com/chessxu/comments/3860.html</wfw:comment><comments>http://www.cnitblog.com/chessxu/archive/2005/11/04/3860.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chessxu/comments/commentRss/3860.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chessxu/services/trackbacks/3860.html</trackback:ping><description><![CDATA[<DIV class=postTitle><FONT style="BACKGROUND-COLOR: #ffffff" color=#000080><STRONG>#用程序模拟键盘和鼠标键盘</STRONG></FONT> <BR><FONT color=#386490>在Windows大行其道的今天，windows界<FONT style="BACKGROUND-COLOR: #ffffff">面程序受到广大用户的欢迎</FONT>。对这些程序的操作不外乎两种，键盘输入控制和鼠标输入控制。有时，对于繁杂的，或重复性的操作，我们能否通过编制程序来代替手工输入，而用程序来模拟键盘及鼠标的输入呢？答案是肯定的。这主要是通过两个API函数来实现的。 　　</FONT> </DIV>
<P align=left><SPAN class=line><FONT color=#386490>　　下面以Delphi为例来介绍一下如何实现这两个功能。模拟键盘我们用Keybd_event这个api函数，模拟鼠标按键用mouse_event函数。大家不用担心，在delphi里调用api函数是很方便的事。 　　 <BR>　　先介绍一下Keybd_event函数。Keybd_event能触发一个按键事件，也就是说回产生一个WM_KEYDOWN或WM_KEYUP消息。当然也可以用产生这两个消息来模拟按键，但是没有直接用这个函数方便。Keybd_event共有四个参数，第一个为按键的虚拟键值，如回车键为vk_return,　tab键为vk_tab。第二个参数为扫描码，一般不用设置，用0代替就行。第三个参数为选项标志，如果为keydown则置0即可，如果为keyup则设成“KEYEVENTF_KEYUP”，第四个参数一般也是置0即可。用如下代码即可实现模拟按下i键，其中的$49表示i键的虚拟键值： </FONT></SPAN><SPAN class=line><FONT color=#386490>keybd_event($49,0,0,0); </FONT></SPAN><SPAN class=line><FONT color=#386490><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keybd_event($49,0,KEYEVENTF_KEYUP,0); ... 　　 <BR></FONT></SPAN></P>
<P align=left><SPAN class=line><FONT color=#386490>　　mouse_event最好配合setcursorpos(x,y)函数一起使用，与Keybd_event类似，mouse_event有五个参数，第一个为选项标志，为MOUSEEVENTF_LEFTDOWN时表示左键按下，为MOUSEEVENTF_LEFTUP表示左键松开，向系统发送相应消息。第二三个参数分别表示x,y相对位置，一般可设为0,0,第四五个参数并不重要，一般也可设为0,0。若要得到Keybd_event和mouse_event函数的更详细的用法，可以查阅msdn或delphi帮助。下面是关于mouse_event的示例代码： <BR>　</FONT></SPAN><SPAN class=line><FONT color=#386490>setcursorpos(20,132); <BR>&nbsp;&nbsp; mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); <BR>&nbsp;&nbsp; mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); <BR>&nbsp;&nbsp; mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); <BR>&nbsp;&nbsp; mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); ... <BR></FONT></SPAN><SPAN class=line><FONT color=#386490>上面的代码表示鼠标的双击，若要表示单击，用两个mouse_event即可(一次放下，一次松开)。 　 <BR>　注意，不管是模拟键盘还是鼠标事件，都要注意还原，即按完键要松开，一个keydown对应一个keyup；鼠标单击　完也要松开， 不然可能影响程序的功能。 　　 <BR>　　　好了，希望本文能让你对模拟键盘和鼠标按键有个初步的了解，如果想更深入的了解其中的奥妙，可以查阅详实的msdn在线帮助，并多多动手实践。 <BR><BR><BR></FONT><FONT color=#000000><SPAN class=article_title><STRONG><FONT color=#000080>#解决锁定键盘鼠标的方法</FONT> <BR></STRONG>如果你不需要屏蔽Ctrl+Alt+Del组合键,可以使用低级键盘钩子(WH_KEYBOARD_LL)与低级鼠标钩子(WH_MOUSE_LL),这两种消息钩子的好处是不需要放在动态链接库中就可以作全局钩子,将键盘消息与鼠标消息截获. <BR>unit uHookKeyAndMouse; <BR>{ 该单元利用WH_KEYBOARD_LL与WH_MOUSE_LL两种类型的钩子分别截获键盘消息与鼠标消息} <BR>{ 由于这里只是需要将消息屏蔽,故只需对钩子函数的返回结果设为1即可. } <BR>{ 提供两个函数StartHookKeyMouse与StopHookKeyMouse两个函数. } <BR><BR>interface <BR><BR>uses <BR>Windows, Messages, SysUtils; <BR><BR>const <BR>WH_KEYBOARD_LL =13; <BR>WH_MOUSE_LL =14; <BR><BR>procedure StartHookKeyMouse; <BR>procedure StopHookKeyMouse; <BR><BR>implementation <BR><BR>var <BR>hhkLowLevelKybd:HHook=0; <BR>hhkLowLevelMouse:HHook=0; <BR><BR>function LowLevelKeyboardProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall; <BR>begin <BR>Result:=1; <BR>if nCode&lt;&gt;0 then Result:=CallNextHookEx(0,nCode,WParam,LParam); <BR>end; <BR><BR>function LowLevelMouseProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall; <BR>begin <BR>Result:=1; <BR>if nCode&lt;&gt;0 then Result:=CallNextHookEx(0,nCode,WParam,LParam); <BR>end; <BR><BR>procedure StartHookKeyMouse; <BR>begin <BR>if hhkLowLevelKybd = 0 then <BR>begin <BR>hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0); <BR>end; <BR>if hhkLowLevelMouse = 0 then <BR>begin <BR>hhkLowLevelMouse:=SetWindowsHookEx(WH_MOUSE_LL,LowlevelMouseProc,HInstance,0); <BR>end; <BR>end; <BR><BR>procedure StopHookKeyMouse; <BR>begin <BR>if hhkLowLevelKybd &lt;&gt; 0 then <BR>begin <BR>UnhookWindowsHookEx(hhkLowLevelKybd); <BR>hhkLowLevelKybd:=0; <BR>end; <BR>if hhkLowLevelMouse &lt;&gt; 0 then <BR>begin <BR>UnHookWindowsHookEx(hhkLowLevelMouse); <BR>hhkLowLevelMouse:=0; <BR>end; <BR>end; <BR><BR>initialization <BR>hhkLowLevelKybd:=0; <BR>hhkLowLevelMouse:=0; <BR>finalization <BR>if hhkLowLevelKybd &lt;&gt; 0 then UnhookWindowsHookEx(hhkLowLevelKybd); <BR>if hhkLowLevelMouse &lt;&gt; 0 then UnhookWindowsHookEx(hhkLowLevelMouse); <BR>end. <BR><BR><BR>你的方法确实实现了锁定鼠标，但是我想达到的效果是： <BR>1.锁定键盘 <BR>2.鼠标只能在我的程序窗口中操作 <BR>谢谢 <BR><BR><BR>来自：smokingroom, 时间：2005-2-24 17:01:12, ID：2996381 <BR>要求2(鼠标只能在我的程序窗口中操作)的实现: <BR>修改LowLevelMouseProc过程如下: <BR><BR>type <BR>PMSLLHOOKSTRUCT=^MSLLHOOKSTRUCT; <BR>MSLLHOOKSTRUCT = record <BR>pt:TPoint; <BR>mouseData:DWORD; <BR>flags:DWORD; <BR>time:DWORD; <BR>dwExtraInfo:DWORD; <BR>end; <BR><BR>var <BR>MouseRect:TRect; //这是你需要限制的Mouse活动范围. <BR><BR>function LowLevelMouseProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall; <BR>var <BR>p:PMSLLHOOKSTRUCT; <BR>begin <BR>Result:=0; <BR>if nCode=HC_ACTION then <BR>begin <BR>p:=PMSLLHOOKSTRUCT(LParam); <BR>if (p.pt.X &lt; MouseRect.Left) or (p.pt.X &gt; MouseRect.Right) or <BR>(p.pt.Y &lt; MouseRect.Top) or (p.pt.Y &gt; MouseRect.Bottom) then <BR>Result:=1; <BR>end else <BR>if nCode&lt;&gt;0 then Result:=CallNextHookEx(0,nCode,WParam,LParam); <BR>end; <BR><BR>附取得MouseRect的代码,假定你的主窗体体为MainFrm <BR>MouseRect:=MainFrm.ClientRect; <BR>MouseRect.TopLeft:=MainFrm.ClientToScreen(MouseRect.TopLeft); <BR>MouseRect.BottomRight:=MainFrm.ClientToScreen(MouseRect.BottomRight); <BR><BR><BR>另在Result:=1之前加多一个ClipCursor(@MouseRect)效果会更好,可以有效解决当按下Ctrl+Alt+Del后将Mouse移出窗体后,Mouse失效的情况. <BR>if (p.pt.X &lt; MouseRect.Left) or (p.pt.X &gt; MouseRect.Right) or <BR>(p.pt.Y &lt; MouseRect.Top) or (p.pt.Y &gt; MouseRect.Bottom) then <BR>begin <BR>ClipCursor(@MouseRect) <BR>Result:=1; <BR>end <BR><BR><BR><BR></P>
<DIV align=left><FONT color=#000080><STRONG>#Delphi键盘码表</STRONG></FONT><BR>
<TABLE borderColor=#000080 cellSpacing=0 borderColorDark=#000080 cellPadding=0 width="100%" bgColor=#c0c0c0 borderColorLight=#000080 border=1>
<TBODY>
<TR>
<TD width="100%" colSpan=2>
<P align=center>Delphi 键盘码表 </P></TD></TR>
<TR>
<TD width="100%" colSpan=2>
<P align=center>资料来源：Delphi Windows 单元文件 </FONT></A></P></TD></TR>
<TR>
<TD width="40%">VK_LBUTTON = 1; <BR>VK_RBUTTON = 2; <BR>VK_CANCEL = 3; <BR>VK_MBUTTON = 4; { NOT contiguous with L &amp; RBUTTON } <BR>VK_BACK = 8; <BR>VK_TAB = 9; <BR>VK_CLEAR = 12; <BR>VK_RETURN = 13; <BR>VK_SHIFT = $10; <BR>VK_CONTROL = 17; <BR>VK_MENU = 18; <BR>VK_PAUSE = 19; <BR>VK_CAPITAL = 20; <BR>VK_KANA = 21; <BR>VK_HANGUL = 21; <BR>VK_JUNJA = 23; <BR>VK_FINAL = 24; <BR>VK_HANJA = 25; <BR>VK_KANJI = 25; <BR>VK_CONVERT = 28; <BR>VK_NONCONVERT = 29; <BR>VK_ACCEPT = 30; <BR>VK_MODECHANGE = 31; <BR>VK_ESCAPE = 27; <BR>VK_SPACE = $20; <BR>VK_PRIOR = 33; <BR>VK_NEXT = 34; <BR>VK_END = 35; <BR>VK_HOME = 36; <BR>VK_LEFT = 37; <BR>VK_UP = 38; <BR>VK_RIGHT = 39; <BR>VK_DOWN = 40; <BR>VK_SELECT = 41; <BR>VK_PRINT = 42; <BR>VK_EXECUTE = 43; <BR>VK_SNAPSHOT = 44; <BR>VK_INSERT = 45; <BR>VK_DELETE = 46; <BR>VK_HELP = 47; <BR>{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) } <BR>{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) } <BR>VK_LWIN = 91; <BR>VK_RWIN = 92; <BR>VK_APPS = 93; <BR>VK_NUMPAD0 = 96; <BR>VK_NUMPAD1 = 97; <BR>VK_NUMPAD2 = 98; <BR>VK_NUMPAD3 = 99; <BR>VK_NUMPAD4 = 100; <BR>VK_NUMPAD5 = 101; <BR>VK_NUMPAD6 = 102; <BR>VK_NUMPAD7 = 103; <BR>VK_NUMPAD8 = 104; </TD>
<TD width="49%">VK_NUMPAD9 = 105; <BR>VK_MULTIPLY = 106; <BR>VK_ADD = 107; <BR>VK_SEPARATOR = 108; <BR>VK_SUBTRACT = 109; <BR>VK_DECIMAL = 110; <BR>VK_DIVIDE = 111; <BR>VK_F1 = 112; <BR>VK_F2 = 113; <BR>VK_F3 = 114; <BR>VK_F4 = 115; <BR>VK_F5 = 116; <BR>VK_F6 = 117; <BR>VK_F7 = 118; <BR>VK_F8 = 119; <BR>VK_F9 = 120; <BR>VK_F10 = 121; <BR>VK_F11 = 122; <BR>VK_F12 = 123; <BR>VK_F13 = 124; <BR>VK_F14 = 125; <BR>VK_F15 = 126; <BR>VK_F16 = 127; <BR>VK_F17 = 128; <BR>VK_F18 = 129; <BR>VK_F19 = 130; <BR>VK_F20 = 131; <BR>VK_F21 = 132; <BR>VK_F22 = 133; <BR>VK_F23 = 134; <BR>VK_F24 = 135; <BR>VK_NUMLOCK = 144; <BR>VK_SCROLL = 145; <BR>{ VK_L &amp; VK_R - left and right Alt, Ctrl and Shift virtual keys. <BR>Used only as parameters to GetAsyncKeyState() and GetKeyState(). <BR>No other API or message will distinguish left and right keys in this way. } <BR>VK_LSHIFT = 160; <BR>VK_RSHIFT = 161; <BR>VK_LCONTROL = 162; <BR>VK_RCONTROL = 163; <BR>VK_LMENU = 164; <BR>VK_RMENU = 165; <BR>VK_PROCESSKEY = 229; <BR>VK_ATTN = 246; <BR>VK_CRSEL = 247; <BR>VK_EXSEL = 248; <BR>VK_EREOF = 249; <BR>VK_PLAY = 250; <BR>VK_ZOOM = 251; <BR>VK_NONAME = 252; <BR>VK_PA1 = 253; <BR>VK_OEM_CLEAR = 254;　 </TD></TR>
<TR>
<TD width="40%"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-ascii-font-family: Times New Roman; mso-hansi-font-family: Times New Roman"><A href="http://www.csdn.net/editor/Down.htm" target=_blank></A></SPAN></TD>
<TD width="40%"></TD></TR></TBODY></TABLE></DIV></SPAN></FONT></SPAN><BR><BR><BR><FONT style="BACKGROUND-COLOR: #000080"><FONT color=#000080><FONT style="BACKGROUND-COLOR: #ffffff" face=Georgia color=#000080><STRONG>#编写后台监控软件</STRONG></FONT><BR></FONT></FONT>
<DIV align=left>
<DIV class=diaryContent id=diary955861 style="DISPLAY: block">&nbsp;后台监控软件，为了达到隐蔽监控的目的，应该满足正常运行时，不显示在任务栏上，在按Ctrl+Alt+Del出现的任务列表中也不显示，管理员可以通过热键调出隐藏的运行界面。要作到这些，必须把当前进程变为一个系统服务，并且定义全局热键。 <BR>一、把当前进程变为一个系统服务: <BR>目的是在任务列表中把程序隐藏起来。调用API函数RegisterServiceProcess实现。 <BR>二、定义全局热键（本例中定义热键Ctrl+Del+R），步骤： <BR>1、定义捕获Windows消息WM_HOTKEY的钩子函数，即： <BR>procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY; <BR>2、向Windows加入一个全局原子 Myhotkey: GlobalAddAtom('MyHotkey'), <BR>并保留其句柄。 <BR>3、向Windows登记热键：调用API函数RegisterHotKey实现。 <BR><BR>&nbsp; 后台监控软件，为了达到隐蔽监控的目的，应该满足正常运行时，不显示在任务栏上，在按Ctrl+Alt+Del出现的任务列表中也不显示，管理员可以通过热键调出隐藏的运行界面。要作到这些，必须把当前进程变为一个系统服务，并且定义全局热键。 <BR>一、把当前进程变为一个系统服务: <BR>目的是在任务列表中把€程序隐藏起来。调用API函数RegisterServiceProcess实现。 <BR>二、定义全局热键（本例中定义热键Ctrl+Del+R），步骤： <BR>1、定义捕获Windows消息WM_HOTKEY的钩子函数，即： <BR>procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY; <BR>2、向Windows加入一个全局原子 Myhotkey: GlobalAddAtom('MyHotkey'), <BR>并保留其句柄。 <BR>3、向Windows登记热键：调用API函数RegisterHotKey实现。 <BR>三、源程序： <BR><BR><BR><BR>unit Unit1; <BR>interface <BR>uses <BR>Windows, Messages, Forms, Dialogs, Classes, Controls, StdCtrls; <BR>type <BR>TForm1 = class(TForm) <BR>Button1: TButton; <BR>Button2: TButton; <BR>procedure FormCreate(Sender: TObject); <BR>procedure Button1Click(Sender: TObject); <BR>procedure Button2Click(Sender: TObject); <BR>procedure FormClose(Sender: TObject; var Action: TCloseAction); <BR>private <BR>{热键标识ID} <BR>id: Integer; <BR>procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY; <BR>{ Privat-Declarations} <BR>public <BR>{ Public-Declarations} <BR>end; <BR>var <BR>Form1 : TForm1; <BR>implementation <BR>const RSP_SIMPLE_SERVICE=1; <BR>function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord; stdcall; external 'KERNEL32.DLL'; <BR>{$R *.DFM} <BR><BR>{捕获热键消息} <BR>procedure TForm1.WMHotKey (var Msg : TWMHotKey); <BR>begin <BR>if msg.HotKey = id then <BR>ShowMessage('Ctrl+Alt+R键被按下!'); <BR>form1.Visible :=true; <BR>end; <BR><BR>procedure TForm1.FormCreate(Sender: TObject); <BR>Const <BR>{ALT、CTRL和R键的虚拟键值} <BR>MOD_ALT = 1; <BR>MOD_CONTROL = 2; <BR>VK_R = 82; <BR>begin <BR>{首先判断程序是否已经运行} <BR>if GlobalFindAtom('MyHotkey') = 0 then <BR>begin <BR>{注册全局热键Ctrl + Alt + R} <BR>id:=GlobalAddAtom('MyHotkey'); <BR>RegisterHotKey(handle,id,MOD_CONTROL+MOD_Alt,VK_R); <BR>end <BR>else <BR>halt; <BR>end; <BR><BR>{把当前进程变为一个系统服务，从而在任务列表中把程序隐藏起来} <BR>procedure TForm1.Button1Click(Sender: TObject); <BR>begin <BR>RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE); <BR>form1.Hide; <BR>end; <BR><BR>procedure TForm1.Button2Click(Sender: TObject); <BR>begin <BR>close; <BR>end; <BR>{退出时释放全局热键} <BR>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); <BR>begin <BR>UnRegisterHotKey(handle,id); <BR>GlobalDeleteAtom(id); <BR>end; <BR>end. <BR>四、说明： <BR>在后台监控软件中使用以上功能，可真正实现隐蔽运行，热键调出，便于管理员进行管理。程序在Win98，Delphi5.0中运行通过。&nbsp; </DIV><BR><BR><FONT color=#0000ff><FONT color=#000080><STRONG>#利用Hook技术实现键盘监控</STRONG></FONT> <BR></FONT>
<DIV align=left><BR>在许多系统中，出于安全或其它原因，常常要求随时对键盘进行监控，一个专业的监控程序必须具备两点，一是实时；二是作为指示图标运行。实际应用中把利用 Hook（即钩子）技术编写的应用程序添加到Windows的任务栏的指示区中就能够很好的达到这个目的。我在参考了API帮助文档基础上，根据在 Delphi开发环境中的具体实现分别对这两部分进行详细论述。 <BR>一、Hook(钩子)的实现：Hook是应用程序在MicrosoftWindows消息处理过程中设置的用来监控消息流并且处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程序中实现，若应用程序不是当前窗口时，该Hook就不起作用；如果Hook在DLL中实现，程序在运行中动态调用它，它能实时对系统进行监控。根据需要，我们采用的是在DLL中实现Hook的方式。 <BR><BR><BR>在许多系统中，出于安全或其它原因，常常要求随时对键盘进行监控，一个专业的监控程序必须具备两点，一是实时；二是作为指示图标运行。实际应用中把利用 Hook（即钩子）技术编写的应用程序添加到Windows的任务栏的指示区中就能够很好的达到这个目的。我在参考了API帮助文档基础上，根据在 Delphi开发环境中的具体实现分别对这两部分进行详细论述。 <BR>一、Hook(钩子)的实现：Hook是应用程序在MicrosoftWindows消息处理过程中设置的用来监控消息流并且处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程序中实现，若应用程序不是当前窗口时，该Hook就不起作用；如果Hook在DLL中实现，程序在运行中动态调用它，它能实时对系统进行监控。根据需要，我们采用的是在DLL中实现Hook的方式。 <BR>1．新建一个导出两个函数的DLL文件，在hookproc.pas中定义了钩子具体实现过程。代码如下：librarykeyspy; <BR>useswindowsmessageshookprocin'hookproc.pas'; <BR>exportssetkeyhookendkeyhook; <BR>beginnexthookproc:=0; <BR>procsaveexit:=exitproc; <BR>exitproc:=@keyhookexit; <BR>end. <BR>2.在Hookproc.pas中实现了钩子具体过程：unithookproc; <BR>interfaceusesWindowsMessagesSysUtilsControlsStdCtrls; <BR>varnexthookproc:hhook; <BR>procsaveexit:pointer; <BR>functionkeyboardhook(icode:integer;wparam:wparam;lparam:lparam):lresult; <BR>stdcall; <BR>export; <BR>functionsetkeyhook:bool;export;//加载钩子functionendkeyhook:bool;export;//卸载钩子procedurekeyhookexit; <BR>far; <BR>constafilename='c:\debug.txt';//将键盘输入动作写入文件中vardebugfile:textfile; <BR>implementationfuncionkeyboardhookhandler(icode:integer;wparam:wparam;lparam:lparam):lresult; <BR>stdcall; <BR>export; <BR>beginificode&lt;0thenbeginresult:=callnexthookex(hnexthookprocicodewparamlparam); <BR>exit; <BR>end; <BR>assignfile(debugfileafilename); <BR>append(debugfile); <BR>ifgetkeystate(vk_return)&lt;0thenbeginwriteln(debugfile''); <BR>write(debugfilechar(wparam)); <BR>endelsewrite(debugfilechar(wparam)); <BR>closefile(debugfile); <BR>result:=0; <BR>end; <BR>functionendkeyhook:bool; <BR>export; <BR>beginifnexthookproc&lt;&gt;0thenbeginunhookwindowshookex(nexthookproc); <BR>nexthookproc:=0; <BR>messagebeep(0); <BR>end; <BR>result:=hnexthookproc=0; <BR>end; <BR>procedurekeyhookexit; <BR>far; <BR>beginifnexthookproc&lt;&gt;0thenendkeyhook; <BR>exitproc:=procsaveexit; <BR>end; <BR>end. <BR>二、Win95/98使用任务栏右方指示区来显示应用程序或工具图标对指示区图标的操作涉及了一个API函数Shell_NotifyIcon，它有两个参数，一个是指向TnotifyIconData结构的指针，另一个是要添加、删除、改动图标的标志。通过该函函数将应用程序的图标添加到指示区中，使其作为图标运行增加专业特色。当程序起动后，用鼠标右键点击图标，则弹出一个菜单，可选择sethook或endhook。 <BR>unitkb; <BR>interfaceusesWindowsMessagesSysUtilsClassesGraphicsControlsFormsDialogsStdCtrlsMenusshellapi; <BR>consticon_id=1; <BR>MI_iconevent=wm_user+1;//定义一个用户消息typeTForm1=class(TForm) <BR>PopupMenu1:TPopupMenu; <BR>sethook1:TMenuItem; <BR>endhook1:TMenuItem; <BR>N1:TMenuItem; <BR>About1:TMenuItem; <BR>Close1:TMenuItem; <BR>Gettext1:TMenuItem; <BR>procedureFormCreate(Sender:TObject); <BR>proceduresethook1Click(Sender:TObject); <BR>procedureendhook1Click(Sender:TObject); <BR>procedureFormDestroy(Sender:TObject); <BR>procedureClose1Click(Sender:TObject); <BR>private{Privatedeclarations} <BR>nid:tnotifyicondata; <BR>normalicon:ticon; <BR>public{Publicdeclarations} <BR>procedureicontray(varmsg:tmessage); <BR>messagemi_iconevent; <BR>end; <BR>varForm1:TForm1; <BR>implementation{$R*.DFM} <BR>functionsetkeyhook:bool; <BR>external'keyspy.dll'; <BR>functionendkeyhook:bool; <BR>external'keyspy.dll'; <BR>proceduretform1.icontray(varmsg:tmessage); <BR>varpt:tpoint; <BR>beginifmsg.lparam=wm_lbuttondownthensethook1click(self); <BR>ifmsg.LParam=wm_rbuttondownthenbegingetcursorpos(pt); <BR>setforegroundwindow(handle); <BR>popupmenu1.popup(pt.xpt.y); <BR>end; <BR>end; <BR>procedureTForm1.FormCreate(Sender:TObject); <BR>beginnormalicon:=ticon.create; <BR>application.title:=caption; <BR>nid.cbsize:=sizeof(nid); <BR>nid.wnd:=handle; <BR>nid.uid:=icon_id; <BR>nid.uflags:=nif_iconornif_messageornif_tip; <BR>nid.ucallbackmessage:=mi_iconevent; <BR>nid.hIcon:=normalicon.handle; <BR>strcopy(nid.sztippchar(caption)); <BR>nid.uFlags:=nif_messageornif_iconornif_tip;shell_notifyicon(nim_add@nid); <BR>SetWindowLong(Application.HandleGWL_EXSTYLEWS_EX_TOOLWINDOW); <BR>end; <BR>procedureTForm1.sethook1Click(Sender:TObject); <BR>beginsetkeyhook; <BR>end; <BR>procedureTForm1.endhook1Click(Sender:TObject); <BR>beginendkeyhook; <BR>end; <BR>procedureTForm1.FormDestroy(Sender:TObject); <BR>beginnid.uFlags:=0; <BR>shell_notifyicon(nim_delete@nid); <BR>end; <BR>procedureTForm1.Close1Click(Sender:TObject); <BR>beginapplication.terminate; <BR>end; <BR>该程序虽然只用了几个shellai函数，但是它涉及到了在Delphi中对DLL的引用、钩子实现、对指示区的操作、用户定义消息的处理、文件的读写等比较重要的内容，我相信这篇文章能对许多Delphi的初学者有所帮助。 <BR>该程序在Win98、Delphi4.0中正常运行。<BR><BR><A id=Comments1_CommentList__ctl2_DeleteLink href="javascript:__doPostBack('Comments1$CommentList$_ctl2$DeleteLink','')"></A>&nbsp;&nbsp;<BR><STRONG><FONT color=#000080>#建立应用软件注册安全机制</FONT></STRONG> 
<DIV align=left><BR>软件的安全性是个永恒的话题，一直困扰着人们，程序员们常常因此熬夜。Novell 公司的网络操作系统NetWare <BR>以其完善可靠的安全管理机制著称，为实现用户登录的安全性和防止非法入侵者闯入，系统提供了几种有效的限制措施和检测功能。本文讨论如何建立完善的软件注 <BR>册安全机制，有效地管理用户登录，为应用软件提供可靠的保护，并在Delphi下实现。 <BR>软件的安全性是个永恒的话题，一直困扰着人们，程序员们常常因此熬夜。Novell 公司的网络操作系统NetWare 以其完善可靠的安全管理机制著称，为实现用户登录的安全性和防止非法入侵者闯入，系统提供了几种有效的限制措施和检测功能。本文讨论如何建立完善的软件注册安全机制，有效地管理用户登录，为应用软件提供可靠的保护，并在Delphi下实现。 <BR><BR>　　Delphi 作为一个优秀的编程工具，功能极其强大，令开发周期大大缩短，生成的代码运行速度快，使得Windows 软件设计成为一种乐趣。基本思路是：建立Paradox 格式的用户帐户表Users.DB，并加密保护，由管理员维护。预先在此表中登记并有注册权限的用户，可持本人有效口令登录进入系统。 <BR><BR>　　实现步骤如下： <BR><BR>　　1 ．在应用软件主目录（假设为C:\Register）下创建两个子目录，分别命名为Login 和Data。 <BR><BR>　　2 ．从Delphi 的Tools 菜单中启动Database Desktop，并选择Tools/AliasManager ...创建一个新别名。方法是：在Alias Manager 对话框中点New，给出新别名UserInfo，并指向C:\Register\Data 目录，按Keep New、OK 等按钮保存新别名。 <BR><BR>　　3 ．利用Database Desktop 创建Paradox 5.0 for Windows 数据表Users.DB，结构见表1。事实上，对于商业软件还需要更多的域。 <BR><BR>表1 表Users.DB 的结构 <BR>域名类型大小关键字说明 <BR>Name A 16 * 用户名 <BR>Password A 16 * 注册口令 <BR>Fullname A 32 用户全名 <BR>Group I 用户组号 <BR>Enabled L 注册允许 <BR><BR>　　4 ．设定Name, Group 和Enabled 为非空域(Required Field)，Group 的最小值为0，最大值为3，默认值为3，Enabled 的默认值为True。用户组0 为系统管理员，可进行几乎任何操作；用户组1 为软件操作人员，可进行除用户帐户管理以外的操作，包括建立和删除数据；用户组2 为普通用户，可使用系统资源，无修改权限，而用户组3 仅可浏览系统有限信息。 <BR><BR>　　5 ．用口令"UserPassword" 保护Users.DB 表, 存于Data 目录下。利用Database Desktop 向表中输入若干样本用户（见表2），然后关闭DatabaseDesktop。 <BR><BR>表2 表Users.DB 的样本用户 <BR>Name Password Fullname Group Enabled <BR>Supervisor AAAAAA Super User 0 True <BR>Director BBBBBB Director User 1 True <BR>Guest 3 True <BR>User1 CCCCCC General User1 1 False <BR>User1 DDDDDD General User1 2 True <BR>User2 EEEEEE General User2 3 True <BR><BR>　　表2 显示，允许同一用户以不同身份（用户组）登录进入系统，享有不同的权限。如用户User1，以不同密码登记，分属用户组1 和2。 <BR><BR>　　6 ．返回Delphi IDE，新建项目Register.dpr 并存入C:\Register 目录，主窗体单元命名为Main.pas。用户注册成功后，方能启动主窗体进入系统。本系统仅显示用户信息及登录的日期和时间以表示注册成功（见图5，并不做其它任何事情，这里便是读者应用系统的接口。为此，在主窗体上放入一个TGroupBox 组件，TLabel 和TEdit 组件各五个。将所有TEdit 的Text 属性清空，ReadOnly 属性值设为True，窗体和组件其余属性按表3 设置。 <BR><BR>　　7 ．创建主窗体的OnActivate 事件处理程序，登录成功并激活主窗体时执行，显示用户信息、登录日期和时间。过程如下： <BR><BR>procedure TMainForm.FormActivate(Sender: TObject); <BR>begin <BR>if Time&lt; =StrToTime('12:00:00') then <BR>Caption:=' 上午好，您已成功登录！' <BR>else <BR>Caption:=' 下午好，您已成功登录！'; <BR>// 显示登录用户信息、登录日期和时间 <BR>NameEdit.Text:=LoginUser.UserName; <BR>FullNameEdit.Text:=LoginUser.UserFullName; <BR>GroupEdit.Text:=IntToStr(LoginUser.UserGroup); <BR>DisplayDateEdit.Text:=DateToStr(Date); <BR>DisplayTimeEdit.Text:=TimeToStr(Time); <BR>end; <BR><BR>表3 主窗体中组件属性设置 <BR>组件属性设置 <BR>Form1 Caption <BR>Name MainForm <BR>Position poScreenCenter <BR>GroupBox1 Caption 用户信息 <BR>Label1 Caption 用户名称： <BR>Label2 Caption 用户全名： <BR>Label3 Caption 用户组号： <BR>Label4 Caption 登录日期： <BR>Label5 Caption 登录时间： <BR>Edit1 Name NameEdit <BR>Edit2 Name FullnameEdit <BR>Edit3 Name GroupEdit <BR>Edit4 Name DisplayDateEdit <BR>Edit5 Name DisplayTimeEdit <BR>　　8 ．数据模板。选择File/New Data Module 建立数据模板LogDataModule，放入一个Ttable 组件，如图1。属性按表4 设置，命名其单元为MD.pas，存入Login 目录。 <BR><BR>表4 数据模板中组件属性设置 <BR>组件属性设置 <BR>Table1 DatabaseName UserInfo <BR>Name UsersTable <BR>TableName Users.DB <BR>　　9 ．创建注册窗体。选择File/New Form 创建一个新窗体，并加入组件TLabel、TEdit 和TbitBtn 各两个。窗体和组件属性按表5 设置，命名其单元为Login.pas，存于Login 目录。 <BR><BR>表5 注册窗体中组件属性设置 <BR>组件属性设置 <BR>Form2 ActiveControl UserNameEdit <BR>Caption 请注册 <BR>Name LoginDialogForm <BR>Position poScreenCenter <BR>Label1 Caption 用户： <BR>Label2 Caption 密码： <BR>Edit1 Name LoginNameEdit <BR>Text <BR>Edit2 Name PasswordEdit <BR>PasswordChar * <BR>Text <BR>BitBtn1 Caption 注册(&amp;L) <BR>Enabled False <BR>Kind bkOK <BR>Name LoginBitBtn <BR>BitBtn2 Caption 取消(&amp;C) <BR>Enabled True <BR>Kind bkCancel <BR>Name CancelBitBtn <BR><BR>　　10 ．选择File/New 命令，从New Items 的New 页中选择Unit 项建一个无窗体单元Addition.pas 存入Login 目录，在其interface 段输入下列语句： <BR><BR>uses Classes, Forms, Sysutils; <BR>type <BR>TApplicationUser=class(TComponent) <BR>private <BR>FUserName: string; <BR>FFullName: string; <BR>FUserGroup: Integer; <BR>public <BR>property UserName: string read <BR>FUserName write FUserName; <BR>property UserFullName: string read <BR>FFullName write FFullName; <BR>property UserGroup: Integer read <BR>FUserGroup write FUserGroup; <BR>end; <BR>var <BR>LoginUser: TApplicationUser; <BR>　　在implementation 段加入下列语句： <BR><BR>initialization <BR>LoginUser:=TapplicationUser.Create(Application); <BR>　　11 ．创建注册窗体(LoginDialogForm) 的OnCreate 事件处理程序。在 <BR>private 段中声明CountDown 为Integer 变量，作为计数器，并将下述语句 <BR>插入begin 与end 之间： <BR><BR>CountDown:=3; // 允许尝试注册3 次 <BR><BR>　　12 ．建立用户名输入框(LoginNameEdit) 的OnChange 事件处理程序，使窗体LoginDialogForm 创建时注册按钮为灰色，用户输入登录名后才可用。不难实现，源代码略。 <BR><BR>　　13 ．创建注册过程。双击注册按钮(LoginBitBtn)，在begin 和end 之间输入其OnClick 事件处理程序代码： <BR><BR>ModalResult:=mrNone; <BR>try <BR>Session.AddPassword('UserPassword'); <BR>LogDataModule.UsersTable.Open; <BR>//注册信息处理 <BR>if not LogDataModule.UsersTable.FindKey <BR>([LoginNameEdit.Text,PasswordEdit.Text]) then <BR>begin <BR>//处理注册次数 <BR>dec(CountDown); <BR>if CountDown&lt; =0 then ModalResult:=mrCancel else <BR>begin <BR>　MessageBeep($FFFF); <BR>　Application.MessageBox('请您重试一次!', <BR>　'无效注册信息',mb_iconstop+mb_OK); <BR>　//重设输入焦点 <BR>　LoginNameEdit.SetFocus; <BR>end; <BR>　exit; <BR>end; <BR>//检查注册允许值 <BR>if not LogDataModule.UsersTable.FieldByName <BR>('Enabled').AsBoolean then <BR>begin <BR>　MessageBeep($FFFF); <BR>　Application.MessageBox( '抱歉，您不能进入系统！', <BR>　'使用权限终止',mb_iconexclamation+mb_OK); <BR>　ModalResult:=mrCancel; <BR>　exit; <BR>end; <BR>//获取注册用户信息 <BR>with LogDataModule do <BR>begin <BR>　LoginUser.UserName：=UsersTable.FieldByName（'Name'）。AsString； <BR>　LoginUser.UserFullName:=UsersTable.FieldByName('Fullname').AsString; <BR>　LoginUser.UserGroup:=UsersTable.FieldByName('Group').AsInteger; <BR>End; <BR>ModalResult:=mrOK; <BR>//关闭表，清除密码 <BR>finally <BR>LogDataModule.UsersTable.Close; <BR>Session.RemovePassword('UserPassword'); <BR>end; <BR>　　14 ．在Main.pas 单元的uses 中加入"Addition"，在Login.pas 单元的implementation 段增加uses 语句： <BR><BR>uses <BR>MD, Addition; <BR><BR>　　15 ．打开过程文件Register.dpr，在uses 语句中加入"Controls"，将"Application.run;" 改为下列语句，只有注册成功时才启动系统，同时释放注册窗体, 以确保安全和减少内存占用。 <BR><BR>if LoginDialogForm.ShowModal=mrOK then <BR>begin <BR>LoginDialogForm.Free; <BR>Application.Run; <BR>end; <BR><BR>　　16 ．编译、运行。注册窗口见图2，输入用户名和密码后点" 注册" 按钮。 <BR><BR>　　如果注册信息有误，系统出现提示框，允许重新注册，见图3。若三次尝试均不成功，注册窗口将保护性关闭。 <BR><BR>　　访问权已被管理员终止的用户（Enabled 的值为False），例如用户User1 以口令CCCCCC 注册至用户组1，系统拒绝进入，见图4。 <BR><BR>　　注册成功后将启动系统主程序。例如用户Supervisor 以口令AAAAAA 注册，见图5。 <BR><BR>　　上述方案具有以下特点： <BR><BR>　　1 ．Supervisor 用户具有至高无上的权利，由系统创建，不能删除，仅Password 和Fullname 字段的值允许自行修改。主要负责系统维护和用户帐户管理等工作，是系统的永久性特权用户。 <BR><BR>　　2 ．确保用户帐户表Users.DB 的安全致关重要，本方案采用了较为可靠的关键性技术，就是注册过程（见步骤13）中"try ...finally ...end"结构的应用。仅当用户按下" 注册" 按钮时，注册模块转送表Users.DB 的密码并将其打开，以处理注册信息。而无论成功与否，在finally 部分都能及时关闭表Users.DB，并立即清除内存中的密码。 <BR><BR>　　3 ．用户帐户管理模块设在主程序中，完成表Users.DB 的维护功能，包括建立用户帐户和用户组，增、删用户，分配访问权限等。只有Supervisor 或其授权用户才可进入。 <BR><BR>　　4 ．组帐户管理：用户按不同级别分组，再按组进行权限分配，以简化管理。如系统可根据登录用户所属的用户组，十分方便地屏蔽相应菜单项，以达到限制用户使用权限的目的。对用户群大的系统，此项技术的优越性尤为显著。 <BR><BR>　　5 ．默认帐户限制：建立用户帐户时，系统默认其Group=3，仅具备浏览有限信息之权限，不致因管理员一时疏忽使新建用户越权使用，危及系统安全。 <BR><BR>　　6 ．单独帐户限制：用户注册时，必须提供完整的个人有效信息才能进入系统。 <BR><BR>　　7 ．注册允许限制：管理员可随时取消或恢复单个用户、用户组甚至系统所有用户（Supervisor 除外）的注册权限，只需设置注册允许字段Enabled 的值为False 或True。 <BR><BR>　　8 ．非法入侵限制：三次注册尝试不成功便保护性关闭注册窗口，以防非法非法入侵者无限制尝试。 <BR><BR>　　本文介绍了应用软件注册安全性问题的综合解决方案，容易实现，便于扩充-- 如增加注册时间限制和记帐收费功能等，可建立较完善的注册安全机制，适合大型商用软件开发。 <BR></DIV><BR><BR><STRONG><FONT color=#000080>#玩转keybd_event</FONT></STRONG> <BR>模拟键盘平时不是很常用, 但是当调用某些快捷键执行某项功能时, 它真的是那么的方便呀. 你不信? 看看下面的实现, 你就会大呼: 为什么不早点告诉我? 呵呵, 原来没有blog呀, 都靠这些挣分呢. <BR><BR>1) 显示桌面: <BR><BR>很多软件有显示桌面的功能, 并且大家的方法都是遍历窗口, 然后让它们最小化, 其实 win系统给咱们了一个非常方便的WIN键(就是键盘上在CTRL键和ALT键之间的那个带win标志的按键), 利用它, 可以轻松的完成显示桌面的功能. <BR><BR>keybd_event(VK_LWIN, 0, 0 ,0); <BR>keybd_event('M', 0, 0 ,0); <BR>keybd_event('M', 0, KEYEVENTF_KEYUP ,0); <BR>keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0); <BR><BR>其他的操作也类似, 比如直接显示开始的运行,就把上面的'M'换成'R'即可。 <BR><BR>直接 keybd_event(VK_LWIN, 0, 0 ,0); <BR>keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0); <BR><BR>直接显示“开始”对话框了。 <BR><BR>2) 实现快速的全选 <BR><BR>很多的时候，比如listctrl实现全选，你可以用listctrl循环设置每一项的状态为选中，多罗索的事情呀。用快捷键试一试CTRL＋A，其他的快捷键一样的用法，呵呵，你知道怎么办了吧？ <BR><BR>keybd_event(VK_CONTROL, (BYTE)0, 0 ,0); <BR>keybd_event('A',(BYTE)0, 0 ,0); //此处可以用 'A', (BYTE)65, 用'a'不起作用. <BR>keybd_event('A', (BYTE)0, KEYEVENTF_KEYUP,0); <BR>keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0); <BR><BR>3) 执行某些特殊的键，比如数字键，大小写，下面是数字键的例子 <BR><BR>bool bState=true; //true为按下NumLock,false反之 <BR>BYTE keyState[256]; <BR><BR>GetKeyboardState((LPBYTE)&amp;keyState); <BR>if( (bState &amp;&amp; !(keyState[VK_NUMLOCK] &amp; 1)) || <BR>(!bState &amp;&amp; (keyState[VK_NUMLOCK] &amp; 1)) ) <BR>{ <BR>// Simulate a key press <BR>keybd_event( VK_NUMLOCK, <BR>0x45, <BR>KEYEVENTF_EXTENDEDKEY | 0, <BR>0 ); <BR><BR>// Simulate a key release <BR>keybd_event( VK_NUMLOCK, <BR>0x45, <BR>KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, <BR>0); <BR>} <BR><BR>4） 你想CTRL＋ALT＋DELETE三键一起按下， <BR><BR>keybd_event(VK_CONTROL, 0, 0 ,0); <BR>keybd_event(VK_MENU,0, 0 ,0); <BR>keybd_event(VK_DELETE,0, 0 ,0); <BR><BR>keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP ,0); <BR>keybd_event(VK_MENU,0, KEYEVENTF_KEYUP ,0); <BR>keybd_event(VK_DELETE,0, KEYEVENTF_KEYUP ,0); <BR>呵呵，这样不会成功呀，因为这几个键直接是操作系统来截获执行的，而模拟键盘只能发向应用程序，所以这种方法不行的（想显示锁定对话框，用 LockWorkStation();） <BR><BR>5） Window2000/NT/XP已经不提倡用这个函数了，上面的方法只是为了让大家开阔一下思路，怎么替代呢，呵呵，看下面，所以上面的所有代码都可以用这个来完成 <BR><BR>//2000下用这个代替 ,包含 "winable.h" <BR>INPUT input[4]; <BR>memset(input, 0, sizeof(input)); <BR><BR>input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD; <BR><BR>input[0].ki.wVk = input[3].ki.wVk = VK_LWIN; <BR>input[1].ki.wVk = input[2].ki.wVk = 'R'; <BR><BR><BR>//接下来释放它，这一点很重要。 <BR>input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP; <BR>input[0].ki.time = input[1].ki.time = input[2].ki.time = input[3].ki.time = GetTickCount(); <BR><BR>SendInput(4, input, sizeof(INPUT)); <BR><BR>感觉比那个有点罗索，呵呵。 <BR><BR>＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝ <BR><BR>附WIN键的部分快捷键: <BR><BR>WIN键+D=快速的切到桌面，再次点击返回 <BR><BR>WIN键+E=快速打开资源管理器 <BR><BR>WIN键+R=“运行”。 <BR><BR>WIN键+M=全部视窗最小化。 <BR><BR>WIN键+Shift+M=取消全部视窗最小化。 <BR><BR>WIN键+F1=Help。 <BR><BR>WIN键+F=“寻找”。 <BR><BR>WIN键+Ctrl+F=显示“查找电脑”。 <BR><BR>WIN键+Tab=切换工作列的程式。 <BR><BR>WIN键+Break=显示系统内容。 <BR><A id=Comments1_CommentList__ctl2_EditLink></A></DIV></DIV><img src ="http://www.cnitblog.com/chessxu/aggbug/3860.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chessxu/" target="_blank">貝殼兒</a> 2005-11-04 09:45 <a href="http://www.cnitblog.com/chessxu/archive/2005/11/04/3860.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>