Create a window with adsorption effect with C ++ Builder

zhaozj2021-02-16  45

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 (Source-> Control)! = NULL); // (1) IF (accept) // (2) {Windows :: TPOINT TOPLEFT = DockPanel-> ClientToscreen (Point (0, 0)); // (3) Windows :: TPoint Bottomright = DockPanel -> ClientToscreen (Point (this-> ClientWidth / 3, DockPanel-> Height); // (4) Source-> DockRect = Windows :: TRECT (Topleft, Bottomright); (5)}} // --- -------------------------------------------------- --------------------------------- Explain: The onDockover event is generated when the adsorption window is dragged through the adsorption panel That is, this event is generated when the user drags the adsorbed window through the DockPanel component. (1) The sentence is first to force the parameters Source-> Control accepting the handle to the TDockableForm type (can be attached); then determine if the forced conversion is successful or whether the Source contains the adsorption object; Save the judgment result into the ACCEPT variable. The effect of subsection (2) is that if the conversion is successful, the virtual box can be provided. Section (3) ~ (4) is used to set the adsorption region to be displayed, in which the Client to Screen method of the Panel component can be used, which can convert the panel to the point in the current screen coordinate system. (5) The sentence is to assign the area set above to the adsorbed object.

/ / -------------------------------------------------------------------------------------------- -------------------------------------- Void __fastcall tMainwin :: DockPanelgetsiteInfo (Tobject * Sender, Tcontrol * DockClient, Treat & InfluenceRect, TPOINT & Mousepos, Bool & Candock = (Dynamic_cast (DockClient)! = Null);} //---------------- -------------------------------------------------- ------- Explanation: When a Docksite property of an adsorption component is True, the OnGetSiteInfo event will be generated before the onDockdrop event, where the code can be initialized with the attached object. / / -------------------------------------------------------------------------------------------- -------------------------------------- Void __fastcall tMainwin :: DockPanelDockDrop (Tobject * Sender, TDRAGDOCKOBJECT * Source, int X, int y) {TPANEL * SenderPanel = Dynamic_cast (sender); // will call the object to be called to TPANEL Type Component IF (SenderPanel == Null) // Judgment Whether the conversion is successful, if it is not successful, give a prompt throw einvalidcast ("senderPanel-> DockClientCount == 1) // determine if it contains the attached window, if there is, call ShowPanel to resign the Panel component showpanel ( Senderpanel, true, null; senderpanel-> DockManager-> resetbounds (true); // Resembled adsorption window} // ---------------------- -------------------------------------------------- --- Explanation: The onDockdrop event is an event generated after the adsorption window is attached to the panel. After adsorbing, the program should call the SHOWPANEL function later, so that the Panel component is displayed in the new size (including the attached window), then Use its DockManager to redraw the adsorbed window.

/ / -------------------------------------------------------------------------------------------- -------------------------------------- void __fastcall tMainwin :: DockPanelundock (Tobject * Sender, Tcontrol * Client, TWinControl * newTarget, bool & Allow) {TPanel * SenderPanel = dynamic_cast (Sender); if (SenderPanel == NULL) throw EInvalidCast ( ""); if (SenderPanel-> DockClientCount == 1) // Make sure the current adsorption panel contains the adsorbed object, then call the ShowPanel method "liberation" it ShowPanel (Senderpanel, false, null);} //----------------- -------------------------------------------------- ------ Onundock event is called before the adsorption object is "liberated", so this can provide users with a chance to "liberate" adsorption objects. Next, a function showPanel () is used to control the adsorption object wherein the parameter description: APANEL - Adsorbed panel object, makevisible - panel visible sign, Client - The adsorption object to be displayed when the panel is replaced. / / -------------------------------------------------------------------------------------------- --------------------------- Void Tmainwin :: ShowPanel (TPANEL * APANEL, BOOL Makevisible, Tcontrol * Client) {IF (! Makevisible &&) (APANEL-> VisibleDockClientCount> 1)) // Judgment If the panel is invisible, and contains more than one adsorbed object, then return return; if (APANEL == DockPanel) // Judgment If the PAANEL object obtained when the function is called In the adsorption panel, the visibility of the SPlliter component is consistent with the Panel object. Vsplitter-> Visible = makevisible; if (makevisible) / / When the adsorption panel is visible, the width of the panel is set to 1/3 of the window client area. And simultaneously moving splliter to the right side of the panel {apanel-> width = clientwidth / 3; vsplitter-> left = apanel-> width vsplitter-> width;} else apanel-> width = 0; // Panel is not available , Maintenance panel width is 0 if (Makevisible && ()) Client-> show (); // Displays the adsorbed object, and to ensure the panel visible} // ----------- -------------------------------------------------- -------------------------------- The following work is to realize the function of the menu command, the "Red Window" command to generate a red window The "The Lan Color Window" command generates a blue window, and the "Close" command turns off the entire program.

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 (HostDocksite), false, null; action = cahide;} Next in the attached window DockWindow Add the following code in the OnDockover event: // ----------------------------------------- ---------------------------------- Void __fastcall tdockableform :: formdockover (TOBJECT * Sender, TDRAGDOCKOBJECT * SOURCE, INT X, int y, tdragstate state, bool & accept) {TRECT ARECT

Accept = (Dynamic_cast (Source-> Control)! = Null);

// 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

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

New Post(0)