Y
YL_YAN
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码用来在Form1上的Image1里用鼠标左键选取一点,如果有鼠标点击,则退出循环,否则循环直到有鼠标左键的点击,为什么运行结果不对?<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> ExtCtrls, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Image1: TImage;<br> Edit1: TEdit;<br> procedure Button1Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> mhook: HHOOK;<br> 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> if iCode < 0 then<br> begin<br> Result := CallNextHookEx(mHook, iCode, wParam, lParam);<br> exit;<br> end;<br> if (wParam=WM_LButtonDown) then<br> begin<br> postmessage(edit1.handle,WM_KeyDown,65,0); //设edit1.txt='a'<br> iCode:=-1;<br> end;<br> Result := CallNextHookEx(mHook, iCode, wParam, lParam); //调用下一个函数<br><br>end;<br><br>begin<br> leftm:=false;<br> mhook:=SetWindowsHookEx(WH_MOUSE, @mouseHookProc, 0, GetCurrentThreadID);<br> repeat<br> if edit1.text='a' then leftm:=true;<br> until leftm; //如果有鼠标点击,则退出循环。<br><br> UnhookWindowsHookEx(mHook);<br> showmessage('first');<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> With image1,Canvas do<br> begin<br> MoveTo(image1.left,image1.top);<br> LineTo(image1.left+image1.Width-1,image1.top+image1.Height-1);<br> end;<br>end;<br><br>end.