示例#1
0
        private MenuCaptionBarForm CreateReadOnlyTextForm(string fileName, int width, int height)
        {
            string text;

            try
            {
                text = FileUtilities.ReadAllText(fileName);
            }
            catch (Exception exception)
            {
                // Just ignore and do nothing.
                exception.Trace();
                return(null);
            }

            var textBox = new RichTextBoxEx
            {
                Dock        = DockStyle.Fill,
                BorderStyle = BorderStyle.None,
                ScrollBars  = RichTextBoxScrollBars.Vertical,
                DetectUrls  = true,

                ForeColor = Color.FromArgb(32, 32, 32),
                BackColor = Color.LightGray,
                Font      = new Font("Consolas", 10),

                Text     = text,
                ReadOnly = true,
            };

            textBox.BindStandardEditUIActions();

            UIMenu.AddTo(textBox);

            // Immediately dispose the handle to the process after creating it.
            // Won't kill the process, just release its unmanaged resource in this one.
            textBox.LinkClicked += (_, e) =>
            {
                var process = Process.Start(e.LinkText);
                if (process != null)
                {
                    process.Dispose();
                }
            };

            // Use a panel with padding to add some margin around the textBox.
            var readOnlyTextForm = new MenuCaptionBarForm <RichTextBoxExWithMargin>(
                new RichTextBoxExWithMargin(textBox, Path.GetFileName(fileName))
            {
                BackColor = Color.LightGray,
            })
            {
                ClientSize = new Size(width, height),
            };

            // Add a Close menu item to the context menu of the textBox.
            textBox.BindAction(SharedUIAction.Close, readOnlyTextForm.TryClose);

            return(readOnlyTextForm);
        }
示例#2
0
            public RichTextBoxExWithMargin(RichTextBoxEx textBox, string fileName)
            {
                Padding = new Padding(6);
                TextBox = textBox;
                Controls.Add(TextBox);

                DockProperties.CaptionHeight = 26;
                DockProperties.CaptionText   = fileName;

                ActiveControl = textBox;
            }