Another Way to Drag A Window

zhaozj2021-02-11  221

The most common way to move an application window is to drag it by its title bar. The following article lets you provide dragging capabilities for Delphi forms without a caption, so the user can move them by clicking anywhere on their client area.

Step aside, juniorFor example, consider the case of a Windows application that does not have a title bar, how can we move such a window ?. In fact, it's possible to create windows with a nonstandard title bar and even non rectangular forms. In this case, How Could Windows Know Where The Borders and The Corners of the Window Are?

MessagesThe Windows operating system is heavily based on messages. For example, when you click on a window or a control, Windows sends it a wm_LButtonDown message, with additional information about where the mouse cursor is and which control keys are currently pressed. Sounds familiar? yes, this is nothing more than an OnMouseDown event in Delphi. Similarly, Windows sends a wm_NCHitTest message whenever a mouse event occurs, that is, when the cursor moves, or when a mouse button is pressed or released. If the mouse is not captured ........................

If we can make Windows think that the user is dragging (has clicked on) the title bar rather than the client area, then the user could drag the window by clicking in the client area. The easiest way to do this is to "fool" Windows Into Thinking That You're Actually Clicking On The Title Bar of A Form. We will do this by handling the wm_nchittest windows message. Here's what you have to do:

1. Insert The Following Line Into your form's "private declarations": Procedure Wmnchittest (VAR Msg: Twmnchittest);

Message WM_NCHITTEST;

2. Add The Following Code Into The "Implementation" Section of Your Form's Unit (Where Form1 is AssuMed Form Name):

Procedure TFORM1.WMNCHITTEST (VAR MSG: Tw mnchittest);

Begin

inherited;

if msg.result = htclient kil

Msg.result: = htcaption;

END;

The first line of code in the message handler calls the inherited method to obtain the default handling for the wm_NCHitTest message The if part in the procedure intercepts and changes your window's behavior This is what actually happens:.. When the operating system sends a wm_NCHitTest message to the window, together with the mouse coordinates, the window returns a code that states which portion of itself has been hit. The important piece of information, for our task, is in the value of the Msg.Result field. At this point, we have an opportunity to modify the message result.This is what we do: if the user has clicked in the form's client area we make Windows to think the user clicked on the title bar in Object Pascal "words":. if the message return Value Is Htclient, We Simply Change It To Htcaption.

Be carefull: mouse events, no more By changing the default behaviour of our forms we remove the ability of Windows to notify you when the mouse is over the client area One side effect of this trick is that your form will no longer generate events!. For mouse messages.

Captionless-Borderless WindowIf you want a captionless borderless window similar to a floating toolbar, set the Form's Caption to an empty string, disable all of the BorderIcons, and set the BorderStyle to bsNone.Show window contents while draggingTo show the window contents while dragging the Window We can Use the next api procedure calls:

// to show Window Contents While Dragging:

SystemParametersInfo

(SPI_SETDRAGFULLWINDOWS, 1, NIL, 0);

// to disable this option call the function:

SystemParametersInfo

(Spi_setdragfulwindows, 0, nil, 0);

More wm_NCHitTest tricksIf you look more carefully at the wm_NCHitTest message you'll see that return value of the function indicates the position of the cursor hot spot. This enables us to play some more with the message to create strange results.

. The Following Code Fragment Will Prevent Uses To Close Your Forms By Clicking on Their Close Buttons.

if msg.result = htclose the

Msg.Result: = htnowhere;

. If the user is trying to move the form by clicking on the caption bar and dragging, the code replaces the result of the message with a result which indicates the user clicked on the client area. This prevents the user from moving the window with the Mouse (Opposite to What Wee Doing In The Begging of the Article).

If msg.result = htcaption then

Msg.Result: = htclient;

A "Funny" Peace of Code, Minimize Is Maximize and Maximize Is Minimize.

if msg.result = htmaxbutton then

Msg.result: = htmaxbutton

Else if msg.result = htminButton then

Msg.Result: = htminButton;

BUT, I HAVE COMPONENTS ON A FORM!? IN MOST CASES We'll Have Some Components On A Form. Let's Say, for Example, That One Panel Object Is On A Form. If Align Property of A Panel IS set to alclient, the Panel fills the entire client area so that it is impossible to select the parent form by clicking on it. The code above will not work! Why? The code above will not work because the mouse is always moving over the Panel component not the form. To Move Our Form by Dragging A Panel On The Form We Have To Add Lines of Code in The onmousedown Event Procedure for the Panel Component:

Procedure TFORM1.Panel1MouseDown

(Sender: TOBJECT; button: TMOUSEBUTTON;

Shift: TshiftState; x, y: integer;

Begin

Releasecapture;

SendMessage (Form1.Handle, WM_SYSCOMMAND, 61458, 0);

END;

NOTE: This Code Will Not Work with Non-Window Controls Such as TLabel Components.

Moving, Moving ... That's it. If you have some discussion ideas on this topic, please post to the delphi programming forum.

转载请注明原文地址:https://www.9cbs.com/read-3834.html

New Post(0)