How create a fullscreen view of a group of controls, that belongs to a tabpage

前端 未结 1 702
[愿得一人]
[愿得一人] 2020-12-10 21:48

i would like to know what is the correct way to create a fullscreen view of a tabpage control. This page has other controls, and that controls has various events that are su

相关标签:
1条回答
  • 2020-12-10 22:21

    This can be elegantly done by Winforms' support for reparenting a control. You could move it to a temporary form that's displayed full-screen. All the normal event handlers still work as usual. Here's a sample implementation, it works for any control:

        public static void ShowFullScreen(Control ctl) {
            // Setup host form to be full screen
            var host = new Form();
            host.FormBorderStyle = FormBorderStyle.None;
            host.WindowState = FormWindowState.Maximized;
            host.ShowInTaskbar = false;
            // Save properties of control
            var loc = ctl.Location;
            var dock = ctl.Dock;
            var parent = ctl.Parent;
            var form = parent;
            while (!(form is Form)) form = form.Parent;
            // Move control to host
            ctl.Parent = host;
            ctl.Location = Point.Empty;
            ctl.Dock = DockStyle.Fill;
            // Setup event handler to restore control back to form
            host.FormClosing += delegate {
                ctl.Parent = parent;
                ctl.Dock = dock;
                ctl.Location = loc;
                form.Show();
            };
            // Exit full screen with escape key
            host.KeyPreview = true;
            host.KeyDown += (KeyEventHandler)((s, e) => {
                if (e.KeyCode == Keys.Escape) host.Close();
            });
            // And go full screen
            host.Show();
            form.Hide();
        }
    

    Sample usage:

        private void button1_Click(object sender, EventArgs e) {
            ShowFullScreen(tabControl1);
        }
    
    0 讨论(0)
提交回复
热议问题