下面的代码用线程钩子传递在Form1上的Image1里鼠标左键的点击,为什么运行结果不对?(100分)

Y

YL_YAN

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码用来在Form1上的Image1里用鼠标左键选取一点,如果有鼠标点击,则退出循环,否则循环直到有鼠标左键的点击,为什么运行结果不对?<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;ExtCtrls, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Image1: TImage;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;mhook: HHOOK;<br> &nbsp;leftm: boolean; //用来判断是否有鼠标左键的点击<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br><br>function MouseHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;export;<br>begin<br> &nbsp;if iCode &lt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := CallNextHookEx(mHook, iCode, wParam, lParam);<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp;end;<br> &nbsp;if (wParam=WM_LButtonDown) then<br> &nbsp;begin<br> &nbsp; &nbsp; postmessage(edit1.handle,WM_KeyDown,65,0); &nbsp; &nbsp;//设edit1.txt='a'<br> &nbsp; &nbsp; iCode:=-1;<br> &nbsp;end;<br> &nbsp;Result := CallNextHookEx(mHook, iCode, wParam, lParam); &nbsp; &nbsp;//调用下一个函数<br><br>end;<br><br>begin<br> &nbsp;leftm:=false;<br> &nbsp;mhook:=SetWindowsHookEx(WH_MOUSE, @mouseHookProc, 0, GetCurrentThreadID);<br> &nbsp;repeat<br> &nbsp; &nbsp;if edit1.text='a' then leftm:=true;<br> &nbsp;until leftm; &nbsp; &nbsp; //如果有鼠标点击,则退出循环。<br><br> &nbsp;UnhookWindowsHookEx(mHook);<br> &nbsp;showmessage('first');<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;With image1,Canvas do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MoveTo(image1.left,image1.top);<br> &nbsp; &nbsp; &nbsp;LineTo(image1.left+image1.Width-1,image1.top+image1.Height-1);<br> &nbsp; &nbsp;end;<br>end;<br><br>end.
 
想要的运行结果是这样的:先点button, 如果有在Form1上的Image1里用鼠标左键选取一点,就 showmessage('first'),否则就一直循环。
 
点button后的实际运行结果却是:如果有在Form1上的Image1里点击鼠标左键,在Form1范围内鼠标图标就变成沙漏,程序没有响应,可能是死循环。
 
很明显,在Form1上的Image1里点击鼠标左键后,没有向edit1的text中添加&quot;a&quot;, 即postmessage(edit1.handle,WM_KeyDown,65,0)没运行,该行没运行,就说明wParam不等于WM_LButtonDown,问题出在哪里?
 
为了解postmessage(edit1.handle,WM_KeyDown,65,0)的运行情况,把Button1Click的代码修改如下,<br>procedure TForm1.Button1Click(Sender: TObject);<br>.................................................<br>begin<br> &nbsp;mhook:=SetWindowsHookEx(WH_MOUSE, @mouseHookProc, 0, GetCurrentThreadID); <br>end;<br><br>并在Form1退出时卸载钩子,其它都没变。点button后,如果有点击鼠标左键,先弹出错误:&quot;project1.exe&quot;遇到问题需要关闭,我们对此引起的不便表示抱歉。&quot;,对错误报告接着点&quot;不发送&quot;,弹出&quot;Application error: Exception EAccessViolation in module project1.exe at 00042D2A. Access violation at address 00042D2A in module 'Project1.exe'. Read of address FFFFFFFC.&quot;, 为什么?
 
unit Unit1;<br>interface<br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;ExtCtrls, StdCtrls;<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Image1: TImage;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;mhook: HHOOK;<br>implementation<br>{$R *.DFM}<br>procedure TForm1.Button1Click(Sender: TObject);<br>function MouseHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;export;<br>begin<br> &nbsp;if (wParam=WM_LButtonDown) then<br> &nbsp;begin<br> &nbsp; &nbsp; UnhookWindowsHookEx(mHook);<br> &nbsp; &nbsp; showmessage('first'); &nbsp; <br> &nbsp; &nbsp; iCode:=-1;<br> &nbsp; &nbsp; exit;<br> &nbsp;end;<br> &nbsp;Result := CallNextHookEx(mHook, iCode, wParam, lParam); &nbsp; &nbsp;//调用下一个函数<br>end;<br><br>begin<br> &nbsp;mhook:=SetWindowsHookEx(WH_MOUSE, @mouseHookProc, 0, GetCurrentThreadID);<br>end;<br><br>end.
 
接受答案了.
 

Similar threads

I
回复
0
查看
565
import
I
I
回复
0
查看
638
import
I
I
回复
0
查看
849
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部