Write a window with "magnetic" characteristics using BCB
Ma Minshen, Wu Dabai Chichi, Heilongjiang Province
Some famous sharing software not only have excellent function, but also in the design techniques of the program interface, WinAmp is one of the representatives. WinAMP has two extraordinary, one is to replace the form of the form, which is now commonly known as "skin"; the other is a timely magnetic form tip. So what is a magnetic form? Users who have used Winamp know that WinAmp playlists or equalizers are moved when they are moved, as if they are magnetic, whenever they are close to the main window, they are "adsorbed" in the past, and automatically aligned along the side. This is a magnetic form, then how do we achieve this wonderful feature in our own procedure? After the author exploits the method of implementing this "magnetization" window. This method is suitable for all versions of C Builder. The author introduces you this technology. The difficulty of achieving paste is when this operation is performed, assuming that there are two forms Form1 and Form2, moving Form2 is close to Form1, and when Form2 is less than a given value, it is less than one give value. Obviously, it should be judged during mobile form2, what is the program in inserting a judgment code in what location? The reasonable way is to use the message generated by the system. We know that the form generates WM_WindowPoschanging and WM_MOVING messages when moving, and WM_WindowPosChanged and WM_MOVE messages are generated after the move. We use the WM_WindowPosChanging message here to implement this feature.
The following author describes the method of implementing the magnetic form as follows: first build an application project, put the main window Form1, put a button component, modify the CAPTION property of the button component to "create a magnetic window" double-click it and Write the following code in the OnClick event: void __fastcall tform1 :: button1click (TOBJECT * Sender) {form2 = new tform2 (application); Form2-> show (); // Display Magnetic window} Select "File" -> "New "Create a new window FORM2, add an Edit component edit1 and a button component button on Form2, modify the Text property of the Edit is 50 (this value is adsorbed when the Form 2 is 50 pixels when Form2 is 50 pixels, which is generally set to 20 Come around, here is the effect of 50 pixels), the CAPTION attribute of the modified button is "magnetic pitch", followed by private in this new window: The following variable definition: hwnd snapwin; / / Define FORM2 adsorbed to which window of the handle Rect Work_area; Bool Snapped; // Is Adsorption Sign Bool WinPROCTHING; INT THRESH; / / Distance Start Adsorption Void __fastcall settingchanged (TMESSAGE & MSG); // Change the window larger time to generate void __fastcall WMWindowPosChanging (TWMWindowPosChanging & msg); generating void __fastcall UpdateWorkArea () // move when the window; then refresh the window in public: // Add the following message User declarations: BEGIN_MESSAGE_MAPMESSAGE_HANDLER (WM_WINDOWPOSCHANGING, TWMWindowPosChanging, WMWindowPosChanging); // define the window message when the mobile WmWindowPoschangingMessage_Handler (WM_SETTINGCHANGE, TMESSAGE, SETTINGETECHANGE, TMESSAGE, END_MESSAGE_MAP (TFORM); // When the window changes the size of the unit, the settingchanged unit file Unit.cpp is added to the unit file unit.cpp: void __ FastCall TFORM2 :: Settingchanged (TMESSAGE & MSG) {UpdateWorkarea ();} // --------------------------------- ------------------------------------------ void __fastcall tform2 :: wmwindowposchanging (TWMWindowPoschanging " & msg) {Rect SR; Snapped = false; // Set to not adsorbate // Test Windowsnapwin = form1-> handle; // Define Form2 adsorption to Form1 here, such as changed to other software, the window handle can also adsorb to another On the software window, if (Snapwin && IsWindowVisible (Snapwin) / * This segment obtains the current position of the window Form1, when the Form2 is moved, the function determines whether the FORM2 and FORM1 in the left and right directions are less than 50, such as less than 50, automatically change FORM2. The location, automatically adsorbed to Form1 * / {if (getWindowRect (Snapwin, & SR)) // Gets the position of Form1 {IF ((Msg.WindowPos-> x <= (sr.right thresh) &&
(Msg.WindowPos-> x> = (sr.right-thresh))) {if ((msg.windowpos-> y> sr.top) && (msg.windowpos-> y
(msg.windowpos-> y msg.windowpos-> CY) <= (sr.bottom thresh) {snapped = true; msg.WindowPos-> y = sr.bottom-msg.windowPos-> cy;}} / / -------------------------------------------------------------------------------------------- --------------------------- void __fastcall tform2 :: updateworkarea () {SystemParametersInfo (SPI_GetWorkarea, 0, & Work_Area, 0);} Double click " The magnetic distance button is added as follows: void __fastcall tform2 :: Button1click (TOBJECT * Sender) {thresh = strt (edit1-> text); // Set the magnetic window at the value of Form1 to EDIT1 to start adsorption} in the form The following code is added to the oncreate event: void __fastcall tform2 :: formcreate (TOBJECT * sender) {Snapped = false; // Set to not adsorb UpdateWorkarea (); thresh = strt (edit1-> text); snapwin = form1-> Handle;} The program is programs to be probatted under the C Builder 5.0, Windows98 system.