如何用鼠标点中窗体中某个控件(例如TLabel, TPanel等)来使窗体移动,(100分)

  • 主题发起人 主题发起人 小咪
  • 开始时间 开始时间

小咪

Unregistered / Unconfirmed
GUEST, unregistred user!
我已经知道如何将窗体设成全部窗体可使用鼠标点中移动的方法,如下:<br>&nbsp; .<br>&nbsp; .<br>&nbsp; .<br>private<br>&nbsp; procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;<br>&nbsp; .<br>&nbsp; .<br>&nbsp; .<br>implementation<br><br>procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);<br>begin<br>&nbsp; inherited; &nbsp;{ call the inherited message handler }<br>&nbsp; if M.Result = htClient then &nbsp;{ is the click in the client area? &nbsp; }<br>&nbsp; &nbsp; M.Result := htCaption; &nbsp;{ if so, make Windows think it's &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; { on the caption bar. &nbsp; &nbsp;}<br>end;<br><br>但是我想做到如何用鼠标点中窗体中某个控件(例如TLabel, TPanel等),<br>来使窗体移动。<br><br>哪位高手能触类旁通,指点迷津,在下奉送100分。
 
var<br>&nbsp; mousekey: Integer;<br><br>procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton; <br>&nbsp; &nbsp; Shift: TShiftState; X, Y: Integer);<br>var<br>&nbsp; p: TPoint;<br>&nbsp; v: Integer;<br>begin<br>&nbsp; p := panel1.clienttoscreen(point(x,y));<br>&nbsp; mousekey := button;<br>&nbsp; v := (p.y shl 16) or word(p.x);<br>&nbsp; if ssShift in Shift then <br>&nbsp; &nbsp; mousekey := mousekey or MK_CONTROL<br>&nbsp; else<br>&nbsp; &nbsp; mousekey := mousekey and not MK_CONTROL;<br>&nbsp; if ssCtrl in Shift then <br>&nbsp; &nbsp; mousekey := mousekey or MK_SHIFT<br>&nbsp; else<br>&nbsp; &nbsp; mousekey := mousekey and not MK_SHIFT;<br>&nbsp; form1.perform(WM_NCHITTEST, mousekey, v);<br>end;<br><br>procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton; <br>&nbsp; &nbsp; Shift: TShiftState; X, Y: Integer);<br>var<br>&nbsp; p: TPoint;<br>&nbsp; v: Integer;<br>begin<br>&nbsp; p := panel1.clienttoscreen(point(x,y));<br>&nbsp; mousekey := button;<br>&nbsp; v := (p.y shl 16) or word(p.x);<br>&nbsp; if ssShift in Shift then <br>&nbsp; &nbsp; mousekey := mousekey or MK_CONTROL<br>&nbsp; else<br>&nbsp; &nbsp; mousekey := mousekey and not MK_CONTROL;<br>&nbsp; if ssCtrl in Shift then <br>&nbsp; &nbsp; mousekey := mousekey or MK_SHIFT<br>&nbsp; else <br>&nbsp; &nbsp; mousekey := mousekey and not MK_SHIFT;<br>&nbsp; form1.perform(WM_NCHITTEST, mousekey, v);<br>end;<br><br>procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; <br>&nbsp; &nbsp; X, Y: Integer);<br>var<br>&nbsp; p: TPoint;<br>&nbsp; v: Integer;<br>begin<br>&nbsp; p := panel1.clienttoscreen(point(x,y));<br>&nbsp; v := (p.y shl 16) or word(p.x);<br>&nbsp; if ssShift in Shift then mousekey := mousekey or MK_CONTROL<br>&nbsp; else mousekey := mousekey and not MK_CONTROL;<br>&nbsp; if ssCtrl in Shift then mousekey := mousekey or MK_SHIFT<br>&nbsp; else<br>&nbsp; &nbsp; mousekey := mousekey and not MK_SHIFT;<br>&nbsp; form1.perform(WM_NCHITTEST, mousekey, v);<br>end;<br>
 
呜.....分被抢了..........<br>....呜....<br>你可以搜索一下嘛,这个问题以前问过的...
 
我有最简单的办法:<br>想知道请Email我:shangrw@263.net
 
srw:方便的话贴出来吗,大家共享!!
 
在控件的OnMouseDown事件中记录鼠标的位置lastx:=x;lasty:=y;<br>在OnMouseMove事件中加入:<br>form1.left:=form1.left+x-lastx;form1.top:=form1.top+y-lasty;<br><br>具体程序:<br>procedure TForm1.FormMouseDown(Sender: TObject; Button: TmouseButton;<br>&nbsp; Shift: TShiftState; X, Y: Integer);<br>Begin &nbsp;<br>&nbsp; Ylx:=x; &nbsp; &nbsp; &nbsp; //记录鼠标原来的坐标值 &nbsp;<br>&nbsp; Yly:=y;<br>end;<br><br>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TshiftState; X,<br>&nbsp; Y: Integer);<br>Begin <br>&nbsp; If ssleft in shift then begin &nbsp; &nbsp; &nbsp; //按下鼠标左键拖动form1<br>&nbsp; &nbsp; &nbsp; form1.left:=form1.left+x-ylx; &nbsp;<br>&nbsp; &nbsp; &nbsp; form1.top:=form1.top+y-yly; &nbsp;<br>&nbsp; &nbsp;end;<br>end;<br><br>将其中的formMousedown和formmousemove换成labelmousedown和 labelmousemove就行了。试好了可得给我加分啊.
 
//---------------------------------------------------------------------------<br>#ifndef Unit1H<br>#define Unit1H<br>//---------------------------------------------------------------------------<br>#include &lt;Classes.hpp&gt;<br>#include &lt;Controls.hpp&gt;<br>#include &lt;StdCtrls.hpp&gt;<br>#include &lt;Forms.hpp&gt;<br>//---------------------------------------------------------------------------<br>class TForm1 : public TForm<br>{<br>__published: // IDE-managed Components<br>&nbsp; &nbsp;TLabel *Label1;<br>&nbsp; &nbsp;void __fastcall Label1MouseDown(TObject *Sender, TMouseButton Button,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TShiftState Shift, int X, int Y);<br>&nbsp; &nbsp;void __fastcall Label1MouseMove(TObject *Sender, TShiftState Shift,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int X, int Y);<br>&nbsp; &nbsp;void __fastcall Label1MouseUp(TObject *Sender, TMouseButton Button,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TShiftState Shift, int X, int Y);<br>private:<br>&nbsp; &nbsp; bool mousedown;<br>&nbsp; &nbsp; TPoint origin; // User declarations<br>public: // User declarations<br>&nbsp; &nbsp;__fastcall TForm1(TComponent* Owner);<br>};<br>//---------------------------------------------------------------------------<br>extern PACKAGE TForm1 *Form1;<br>//---------------------------------------------------------------------------<br>#endif<br><br><br>//---------------------------------------------------------------------------<br>#include &lt;vcl.h&gt;<br>#pragma hdrstop<br><br>#include "Unit1.h"<br>//---------------------------------------------------------------------------<br>#pragma package(smart_init)<br>#pragma resource "*.dfm"<br>TForm1 *Form1;<br>//---------------------------------------------------------------------------<br>__fastcall TForm1::TForm1(TComponent* Owner)<br>&nbsp; &nbsp;: TForm(Owner)<br>{<br>&nbsp; &nbsp;mousedown = false;<br>}<br>//---------------------------------------------------------------------------<br><br>void __fastcall TForm1::Label1MouseDown(TObject *Sender,<br>&nbsp; &nbsp; &nbsp; TMouseButton Button, TShiftState Shift, int X, int Y)<br>{<br>&nbsp; &nbsp;mousedown = true;<br>&nbsp; &nbsp;origin.x = X;<br>&nbsp; &nbsp;origin.y = Y;<br>}<br>//---------------------------------------------------------------------------<br><br>void __fastcall TForm1::Label1MouseMove(TObject *Sender, TShiftState Shift,<br>&nbsp; &nbsp; &nbsp; int X, int Y)<br>{<br>&nbsp; &nbsp;if (mousedown) {<br>&nbsp; &nbsp; &nbsp; Form1-&gt;Left = Form1-&gt;Left + (X-origin.x);<br>&nbsp; &nbsp; &nbsp; Form1-&gt;Top &nbsp;= Form1-&gt;Top + (Y-origin.y);<br>&nbsp; &nbsp;}<br>}<br>//---------------------------------------------------------------------------<br>void __fastcall TForm1::Label1MouseUp(TObject *Sender, TMouseButton Button,<br>&nbsp; &nbsp; &nbsp; TShiftState Shift, int X, int Y)<br>{<br>&nbsp; &nbsp;mousedown = false;<br>}<br>//---------------------------------------------------------------------------
 
&nbsp;我同意Srw的方法,我以前也是这样做的.鼠标按下时设一个标志,并记录鼠标按下<br>的位置,拖动的时候直接改Left和Top就可以了,鼠标松开的时候清一下标志.很简单.<br><br>Left:=Left+x-Oldx;<br>Top:=Top+y-Oldy;
 
多人接受答案了。
 
后退
顶部