W
wk_knife
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了下面一段测试代码。单击鼠标开始SETCAPTURE,进入一个消息循环,绘制多边形。<br>双击退出循环。在消息循环中,我点击窗体的关闭按钮,想关闭窗体,结果只是结束了循环。如何才能实现关闭窗体呢?<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs;<br><br>type<br> TShape=class<br> private<br> Pts:array of TPoint;<br> public<br> function AddPoint(Hint:Integer;const CoordX,CoordY:Integer):Boolean;virtual;<br> procedure Draw(Canvas:TCanvas);<br> end;<br><br><br> TForm1 = class(TForm)<br> procedure FormMouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br> procedure FormCreate(Sender: TObject);<br> procedure FormPaint(Sender: TObject);<br> private<br> { Private declarations }<br> P:array of TShape;<br> public<br> { Public declarations }<br> IsCreating:Boolean;<br> continue:Boolean;<br><br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>//uses csintf;<br><br>{$R *.dfm}<br><br>procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br>var<br> Msg:TMsg;<br> AShape:TShape;<br> K:integer;<br> AP:TPoint;<br>begin<br> if Button=mbLeft then<br> begin<br> if not continue then //如果没有开始循环<br> begin<br> continue:=True;<br> AShape:=TShape.Create; //创建个新的多边形对象<br> K:=Length(P);<br> SetLength(P,K+1);<br> p[k]:=AShape; //加入到列表<br> end;<br> end;<br> SetCapture(Handle); <br> while Continue do //进入循环<br> begin<br> while not PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do<br> begin<br> end;<br> if (GetCapture<>Handle) or (Msg.message = WM_CANCELMODE) then<br> continue:=False<br> else if (Msg.message >= WM_MOUSEFIRST) and (Msg.message <= WM_MOUSELAST) then<br> begin<br> if PtInRect(ClientRect,Point(LOWORD(Msg.lParam),HIWORD(msg.lParam))) then<br> begin<br> continue:=AShape.AddPoint(Msg.message,LOWORD(Msg.lParam),HIWORD(msg.lParam));<br> invalidate;<br> end<br> else<br> begin<br> DispatchMessage(msg);<br> end;<br> end<br> else<br> DispatchMessage(msg);<br> end; // while<br> ReleaseCapture;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> IsCreating:=False;<br> continue:=False;<br> DoubleBuffered:=True;<br>end;<br><br>procedure TForm1.FormPaint(Sender: TObject);<br>var<br> I: Integer;<br>begin<br> for I := Low(p) to High(P) do // Iterate<br> p.Draw(Canvas);<br>end;<br><br>{ TShape }<br><br>function TShape.AddPoint(Hint:Integer; const CoordX,<br> CoordY: Integer): Boolean;<br>var<br> A:Integer;<br>begin<br> result:=True;<br> if Hint=WM_LBUTTONDBLCLK then<br> begin<br> //codeSite.SendInteger('WM_LBUTTONDBLCLK',WM_LBUTTONDBLCLK);<br> result:=False;<br> A:=Length(pts);<br> SetLength(pts,A+1);<br> pts[A]:=Point(CoordX,CoordY);<br> end<br> else if Hint=WM_LBUTTONUP then<br> begin<br> //codeSite.SendInteger('WM_LBUTTONUP',WM_LBUTTONUP);<br> A:=Length(pts);<br> SetLength(pts,A+1);<br> pts[A]:=Point(CoordX,CoordY);<br> end;<br>end;<br><br>procedure TShape.Draw(Canvas: TCanvas);<br>begin<br> if Length(pts)>1 then<br> canvas.Polygon(pts);<br>end;<br><br><br>end.