Two older people often talk about: 1. How do I achieve the mouse point to live a customer area? How to move the form without the title bar? 2, how to drag the control on the form during the program run?
In my here, these two issues are solved like this -
-------------------------------------------------- ------------------------------ ★ Drag the form ★ Classic practice: "Deception" system, let it think The title bar TYPE TFORM1 = Class (TFORM) ... private procedure Wmnchittest (VAR M: twmnchittest); Message WM_NCHITTEST; END;
Var Form1: TFORM1; Implementation
procedure TForm1.WMNCHitTest (var M: TWMNCHitTest); begin inherited; // call the inherited message handler if M.Result: = htClient then // is the click in the client area M.Result:? = htCaption; // if so Make Windows Think it's on the capensial bar.nd;
-------------------------------------------------- ---------------------------------------- this practice seems to be ingenious, but actually There is a defect, you will find that the form's client district cannot remove the screen up. Come, make the following code into a control, wonderful still behind ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------
unit MovePanel; interfaceuses Windows, Classes, Controls, ExtCtrls; type TMovePanel = class (TPanel) // This control is inherited Tpanel class private PrePoint: TPoint; Down: Boolean; {Private declarations} rotected {Protected declarations} public onstructor Create ( AOwner: TComponent); override; // overloaded mouse event, the first to process the message procedure MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; procedure MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: integer; procedure; shift: tshiftstate; x, y: integer; override; {public declarations}}}}
PROCEDURE register;
IMPLEMENTATION
Constructor TMOVEPANEL.CREATE (AOWNER: TComponent); Begin inherited Create (Aowner); // Inheriting the Create method of the parent class End;
Procedure TMOVEPANEL.MOUSEDOWN (BUTTON: TMOUSEBUTTON; Shift: TshiftState; x, y: integer; begin if (Button = mbleft) THEN becom (prePoint); END; // If the method already exists, trigger The corresponding event calls it. If this statement does not add this statement, it will cause access to anomalous if assigned (onMouseDown) THEN OnMOUSEDOWN (Self, Button, Shift, X, Y);
procedure TMovePanel.MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if (Button = MBLeft) and Down then Down: = False; if assigned (OnMouseUp) then OnMouseUp (Self, Button, shift, X Y); END;
procedure TMovePanel.MouseMove (Shift: TShiftState; X, Y: Integer); Var NowPoint: TPoint; begin if down then begin GetCursorPos (nowPoint); // self.Parent form in the Form MovePanel is located, or where the MovePanel Container like Panel Self.Parent.x; def.parent.top:/self.parent.top nowpoint.y-prepoint.y; preplay: = nopoint ; End; if Assigned (OnMousemove) THENOMOVE (Self, Shift, X, Y); END;
Procedure Register; Begin RegisterComponents ('MD3', [TMOVEPANEL]); END;
End.
---- Next, look at how to use it. ---- Usage 1: Drag a form, plus our MovePanel, Align property is set to AlClient, run, the effect of moving the form is not bad! To cancel this feature, set the MovePanel's Enabled property to False, simple!
---- Usage 2: Drag a form, plus ordinary panel, adjust the size, add MovePanel on Panel, Align property is set to AlClient, run, this time is not a form when we drag MOVEPANEL In Moving, Panel and MovePanel move together on the form, if we put other controls on the MovePanel, it will become an control that can move anywhere on the form, it is so simple!
(Author: Fuzhou University Wang Jun)
Did you reach? seems like it. Revitalize, each of the controls, including the form, can be used independently with the mouse point to drag, what should I do?
★ Mobile control! ★
Add a PANEL in a new form, add the following code:
procedure TForm1.Panel1MouseDown (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); const SC_DragMove = $ F012; // a magic number begin ReleaseCapture; panel1.perform (WM_SysCommand, SC_DragMove, 0); end I will try it out, how do you want to drag! This method is very good! Very effective when dragging a single control. MovePanel Source Code: 910 byte summary: Under normal circumstances, use MovePanel enough, if you want to drag a single control, use the last method above, as long as the control can respond to the mousedown event!
Posted in "Agan's Home" August 18, 2000