In many programs, the window can be dragged and dropped into another window, and it is integrated, for example, Class Explorer in C Builder and other window is a typical example. In C Builder, this feature is called "window adsorption ". Many components in BCB have properties related to the "adsorption" function, such as: DockSite properties, UsedockManage properties, and Dragking properties, which can be set to DKDOCK. If you simply set these attribute values, the window will automatically have adsorption, but this adsorption function is very dead, no practical value. The following author gives you how to achieve a true window adsorption effect in C Builder. First, design a main window to create a new project, the main form is named Mainform, and the unit file is named main. Add a TMAINMENU named MainMenu1, double-click the main menu to add two submenus to "Red Window" and "Blue Window", which are used to display different colors. The width is then added to the rightmost end of the window and the high level of the Panel component is high, and is named DockPanel, and the PANEL component is established because the "adsorption" window must have a "adsorption area". This adsorption area can It is a window, or a windowization component, such as Panel. Add a splliter component on the right side of Panel, named the vsplliter width set to 4, and the height is the height of the form. The establishment of the SPlliter component is to effectively establish the spacers of the adsorbed window, and can make it easy to adjust the width occupied by the adsorbed window. (As shown in Figure 1) Second, the Design "Adsorption" window is the design form of the main window. In addition to the general property settings, there is no need to modify the attributes of the Dock, as long as the "adsorbed" form is modified. Yes. Select the "File | New Form" command to create a new form, modify the new form command to DockWindow and adsorption, modified the following: Property Docksite Truedockkind DkdockDockMode Dmautomatic places a MEMO component in the form and puts it Align The attribute is modified to AlClient to make it full of the entire window area. The purpose of placing the MEMO component is to make the window have significant boundary features when they are adsorbed, and can give different colors of the window. Third, the design implementation "Adsorption" function is actually, which is not a main form, but the Panel component in the main form, so the components associated with the adsorption function must be modified as the adsorption window in the Panel component. The modified value is shown in the table below.
Then add the following events to Panel: OnDockover, OngetsiteInfo, OnDockDrop, Onundock Code and Explanation as follows: // ----------------------------- -------------------------------------------------- ------- void __fastcall TMainWin :: DockPanelDockOver (TObject * Sender, TDragDockObject * Source, int X, int Y, TDragState State, bool & Accept) {Accept = (dynamic_cast
/ / -------------------------------------------------------------------------------------------- -------------------------------------- Void __fastcall tMainwin :: DockPanelgetsiteInfo (Tobject * Sender, Tcontrol * DockClient, Treat & InfluenceRect, TPOINT & Mousepos, Bool & Candock = (Dynamic_cast
/ / -------------------------------------------------------------------------------------------- -------------------------------------- void __fastcall tMainwin :: DockPanelundock (Tobject * Sender, Tcontrol * Client, TWinControl * newTarget, bool & Allow) {TPanel * SenderPanel = dynamic_cast
The following only give the event handle to generate the red window as follows: (the blue window is similar to this code, omitted here) // --------------------- -------------------------------------------------- ------------------ // "Red Window" command of the onclick event handle: void __fastcall tmainwin :: cmredwindowclick (TOBJECT * Sender) {TDOCKLORM * Redform = New TDOCKLEFORM (this); redform-> memo1-> color = CLRED; // To implement the dynamic generation of the blue window, change the above sentence to redform-> memo1-> color = CLBLUE; redform-> show ();} Next Add the following code to the onClose event of the adsorption window DockWindow: // ------------------------------------------------------------------------------------------------ --------------------------------------- void __fastcall tdockableform :: formclose (TOBJECT * SENDER, TcloseAction & action) {if (HostDocksite)! = Null) Mainwin-> showpanel (static_cast
Accept = (Dynamic_cast
// Draw Dock Preview Depending on Where The Cursor Is Relative To Our Client Area IF (ACCEPT &CKALIGN (ARECT, POINT (X, Y))! = Alnone)) Source-> DockRect = all; -------------------------------------------------- ---------------------- Next to add the following code in the onDockAlign event attached to the adsorption window Dockwindow: Talign TdockAlign (TRECT & DOCKRECT, Const Tpoint & DockAlign) MousePos) {Windows :: TRect DockTopRect, DockLeftRect, DockBottomRect, DockRightRect, DockCenterRect; Windows :: TPoint TopLeft, BottomRight; TAlign Result = alNone; // Divide form up into docking "Zones" TopLeft = Windows :: TPoint (0, 0 ); BottomRight = Windows :: TPoint (ClientWidth / 5, ClientHeight); DockLeftRect = Windows :: TRect (TopLeft, BottomRight); TopLeft = Windows :: TPoint (ClientWidth / 5, 0); BottomRight = Windows :: TPoint (ClientWidth / 5 * 4, ClientHeight / 5); DockTopRect = Windows :: TRect (TopLeft, BottomRight); TopLeft = Windows :: TPoint (ClientWidth / 5 * 4, 0); BottomRight = Windows :: TPoint (ClientWidth, ClientHe ight); DockRightRect = Windows :: TRect (TopLeft, BottomRight); TopLeft = Windows :: TPoint (ClientWidth / 5, ClientHeight / 5 * 4); BottomRight = Windows :: TPoint (ClientWidth / 5 * 4, ClientHeight); DockBottomRect = Windows :: TRect (TopLeft, BottomRight); TopLeft = Windows :: TPoint (ClientWidth / 5, ClientHeight / 5); BottomRight = Windows :: TPoint (ClientWidth / 5 * 4, ClientHeight / 5 * 4); DockCenterRect = Windows :: TRect (TopLeft, BottomRight); // Find out where the mouse cursor is, // to decide where to draw dock preview if (PtInRect (& DockLeftRect, MousePos)) {Result = alLeft;. DockRect = DockLeftRect;
DockRect.Right = ClientWidth / 2;} else if (PtInRect (& DockTopRect, MousePos)) {Result = alTop; DockRect = DockTopRect; DockRect.Left = 0; DockRect.Right = ClientWidth; DockRect.Bottom = ClientHeight / 2;} else if (PtInRect (& DockRightRect, MousePos)) {Result = alRight; DockRect = DockRightRect; DockRect.Left = ClientWidth / 2;} else if (PtInRect (& DockBottomRect, MousePos)) {Result = alBottom; DockRect = DockBottomRect; DockRect.Left = 0; DockRect.Right = ClientWidth; DockRect.Top = ClientHeight / 2;} else if (PtInRect (& DockCenterRect, MousePos)) {Result = alClient; DockRect = DockCenterRect;} if (! Result = alNone) {// DockRect is in Screen Coordinates. Topleft = ClientToscreen (Windows :: TPOINT (DockRect.Top); Bottomright = ClientToscreen (Windows :: TPOINT (DO CkRect.right, DockRect.Bottom));
DockRect = TRECT (Topleft, Bottomright);} RETURN RESULT;} To this, the basic function of the window adsorption can be achieved. In the C Builder routine, there is a routine that implements complex "adsorption" functions. You can spend more time to study research. Path: Program Files / Borland / C Builder 5 / Examples / Docking