Magnetic window

zhaozj2021-02-16  52

Magnetic window

Winamp users know that Winamp's playlists or equalizers are moved when they are moved, as if they are magnetic, whenever close to the main window, they are "adsorbed" in the past, aligned along the side. I want my WINAMP plugin to have this wonderful character, so I figure out a method of "magnetization" window. This method is suitable for all versions of Delphi. To demonstrate this technology, please make a sample program that will be "attracted" by WinAmp. First create an application project, make the main window Form1, and set BorderStyle to BSNOne. Put a button component, double-click it and write "close;" in the onclick event. The procedure will be completed at the time of it. Now switch to the code editing area, define several global variables. Var form1: tform1; // "Magnetic" window Lastx, Lasty: integer; // Remove the previous coordinate WINAMPRECT: TRECT; / / Save the WinAmp window's rectangular area hwnd_winamp: hwnd; // WinAmp window control handle then write Form1 OnMouseDown and onmousemove events. Procedure TForm1.FormMouseDown (Sender: Tobject; Button: TMouseButton; Shift: TshiftState; x, y: integer; const classname = 'winamp v1.x'; // WINAMP main window class name // If you change to classname = ' TAPPBUILDER ', you will find that even Delphi is also gravitated! Begin // Record the current coordinate Lastx: = X; Lasty: = Y; // Find WINAMP HWND_WINAMP: = FINDWINDOW (ClassName, NIL); if hwnd_winamp> 0 THEN / SEN // Record its window area getWindowRect (hwnd_winamp, winamprect) End; Procedure TFORM1.FORMMMOMEMOVE (Sender: Tobject; Shift: TshiftState; x, y: integer); var NLEFT, NTOP: INTEGER; // Record new position temporary variable begin // Check if the left mouse button is pressed if HiWord (GetasyncKeyState)> 0 Then Begin // Calculate New Coordinate NLEFT: = Left X - Lastx; Ntop: = TOP Y - Lasty; // If you find WinAmp, correct the above coordinates, generate "magnetization" effect IF HWnd_WINAMP> 0 THEN MAGNETIZE (NLEFT, NTOP); // Reset window location setbounds (NLEFT, NTOP, WIDTH, Height); end; end; don't worry, look at the magnetize () process, first understand the principle of correcting coordinates . According to the observation of WinAmp implementation, I fight for the so-called "magnetization" next simple definition, that is, "close to a certain level of a certain level in the original window, by correcting the coordinates of the original window, so that the two windows are in the same plane and have The process of public side. According to this definition, I designed the following "magnetization" steps. The first step is to determine whether the target window (ie Winamp) and our Form1 are overlapping in the horizontal and vertical direction. "A directional projection line has overlap" is a necessary non-sufficient condition "need to be corrected". The judgment is based on the difference between the two projection line segments to the leftmost boundary difference to the width and value of the width and value.

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

New Post(0)