Esempio n. 1
0
        private ControlState(Control control, ControlStateOptions options)
        {
            if (options.HasFlag(ControlStateOptions.StoreLayoutProperties))
            {
                layoutState = new LayoutState()
                {
                    Anchor   = control.Anchor,
                    AutoSize = control.AutoSize,
                    Dock     = control.Dock,
                    Location = control.Location,
                    Size     = control.Size,
                };

                if (control.Parent != null)
                {
                    layoutState.BottomRightPadding = new Point(
                        control.Parent.Width - (control.Location.X + control.Width),
                        control.Parent.Height - (control.Location.Y + control.Height));
                }
            }

            if (options.HasFlag(ControlStateOptions.StoreVisualProperties))
            {
                visualState = new VisualState()
                {
                    BackColor = control.BackColor,
                    ForeColor = control.ForeColor,
                    Styles    = ControlUtilities.GetStyles(control)
                };

                if (TryGetResizeRedraw(control, out bool resizeRedraw))
                {
                    visualState.ResizeRedraw = resizeRedraw;
                }

                if (TryGetDrawMode(control, out DrawMode drawMode))
                {
                    visualState.DrawMode = drawMode;
                }

                if (TryGetBorderStyle(control, out BorderStyle borderStyle))
                {
                    visualState.BorderStyle = borderStyle;
                }

                if (TryGetFlatStyle(control, out FlatStyle flatStyle))
                {
                    visualState.FlatStyle = flatStyle;
                }

                if (TryGetOwnerDraw(control, out bool ownerDraw))
                {
                    visualState.OwnerDraw = ownerDraw;
                }

                // This property is important for restoring the visual style of controls like Buttons and TabPages.

                if (TryGetUseVisualStyleBackColor(control, out bool useVisualStyleBackColor))
                {
                    visualState.UseVisualStyleBackColor = useVisualStyleBackColor;
                }

                switch (control)
                {
                case DataGridView dataGridView:

                    visualState.BackColor = dataGridView.BackgroundColor;
                    visualState.EnableHeadersVisualStyles = dataGridView.EnableHeadersVisualStyles;

                    break;

                case ListView listView:

                    visualState.GridLines = listView.GridLines;

                    break;

                case ToolStrip toolStrip:

                    visualState.ToolStripRenderer = toolStrip.Renderer;

                    break;

                case TreeView treeView:

                    visualState.TreeViewDrawMode = treeView.DrawMode;

                    break;
                }
            }
        }