Jimi2000

EmbeddedForms

Jul 23rd, 2018
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.46 KB | None | 0 0
  1. //Form designer partial class.
  2. //CHANGE NAMESPACE NAME
  3. namespace EmbeddedForms
  4. {
  5.     partial class frmEmbeddedForms
  6.     {
  7.         private System.ComponentModel.IContainer components = null;
  8.  
  9.         protected override void Dispose(bool disposing)
  10.         {
  11.             if (disposing && (components != null))
  12.             {
  13.                 components.Dispose();
  14.             }
  15.             base.Dispose(disposing);
  16.         }
  17.  
  18.         #region Windows Form Designer generated code
  19.  
  20.         private void InitializeComponent()
  21.         {
  22.             this.panTitleBar = new System.Windows.Forms.Panel();
  23.             this.lblTitle = new System.Windows.Forms.Label();
  24.             this.panContainer = new System.Windows.Forms.Panel();
  25.             this.panSideBar = new System.Windows.Forms.Panel();
  26.             this.btnHideSideBar = new System.Windows.Forms.Button();
  27.             this.btnNoBorder = new System.Windows.Forms.Button();
  28.             this.btnEmbedForm = new System.Windows.Forms.Button();
  29.             this.panTitleBar.SuspendLayout();
  30.             this.panSideBar.SuspendLayout();
  31.             this.SuspendLayout();
  32.             //
  33.             // panTitleBar
  34.             //
  35.             this.panTitleBar.BackColor = System.Drawing.Color.OliveDrab;
  36.             this.panTitleBar.Controls.Add(this.lblTitle);
  37.             this.panTitleBar.Dock = System.Windows.Forms.DockStyle.Top;
  38.             this.panTitleBar.Location = new System.Drawing.Point(0, 0);
  39.             this.panTitleBar.Name = "panTitleBar";
  40.             this.panTitleBar.Size = new System.Drawing.Size(626, 56);
  41.             this.panTitleBar.TabIndex = 1;
  42.             //
  43.             // lblTitle
  44.             //
  45.             this.lblTitle.Font = new System.Drawing.Font("Segoe UI", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  46.             this.lblTitle.Location = new System.Drawing.Point(27, 9);
  47.             this.lblTitle.Name = "lblTitle";
  48.             this.lblTitle.Size = new System.Drawing.Size(231, 38);
  49.             this.lblTitle.TabIndex = 0;
  50.             this.lblTitle.Text = "Title Bar";
  51.             //
  52.             // panContainer
  53.             //
  54.             this.panContainer.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
  55.             this.panContainer.Dock = System.Windows.Forms.DockStyle.Fill;
  56.             this.panContainer.Location = new System.Drawing.Point(145, 56);
  57.             this.panContainer.Name = "panContainer";
  58.             this.panContainer.Size = new System.Drawing.Size(481, 347);
  59.             this.panContainer.TabIndex = 2;
  60.             this.panContainer.Resize += new System.EventHandler(this.panContainer_Resize);
  61.             //
  62.             // panSideBar
  63.             //
  64.             this.panSideBar.Controls.Add(this.btnHideSideBar);
  65.             this.panSideBar.Controls.Add(this.btnNoBorder);
  66.             this.panSideBar.Controls.Add(this.btnEmbedForm);
  67.             this.panSideBar.Dock = System.Windows.Forms.DockStyle.Left;
  68.             this.panSideBar.Location = new System.Drawing.Point(0, 56);
  69.             this.panSideBar.Name = "panSideBar";
  70.             this.panSideBar.Size = new System.Drawing.Size(145, 347);
  71.             this.panSideBar.TabIndex = 3;
  72.             //
  73.             // btnHideSideBar
  74.             //
  75.             this.btnHideSideBar.FlatAppearance.BorderSize = 0;
  76.             this.btnHideSideBar.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  77.             this.btnHideSideBar.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
  78.             this.btnHideSideBar.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  79.             this.btnHideSideBar.Font = new System.Drawing.Font("Wingdings", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
  80.             this.btnHideSideBar.ForeColor = System.Drawing.Color.White;
  81.             this.btnHideSideBar.Location = new System.Drawing.Point(117, 0);
  82.             this.btnHideSideBar.Name = "btnHideSideBar";
  83.             this.btnHideSideBar.Size = new System.Drawing.Size(29, 31);
  84.             this.btnHideSideBar.TabIndex = 50;
  85.             this.btnHideSideBar.Text = "ï";
  86.             this.btnHideSideBar.UseVisualStyleBackColor = true;
  87.             this.btnHideSideBar.Click += new System.EventHandler(this.btnHideSideBar_Click);
  88.             this.btnHideSideBar.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnHideSideBar_MouseDown);
  89.             this.btnHideSideBar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btnHideSideBar_MouseUp);
  90.             //
  91.             // btnNoBorder
  92.             //
  93.             this.btnNoBorder.Enabled = false;
  94.             this.btnNoBorder.FlatAppearance.BorderSize = 0;
  95.             this.btnNoBorder.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  96.             this.btnNoBorder.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  97.             this.btnNoBorder.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  98.             this.btnNoBorder.Font = new System.Drawing.Font("Segoe UI", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  99.             this.btnNoBorder.ForeColor = System.Drawing.Color.White;
  100.             this.btnNoBorder.Location = new System.Drawing.Point(0, 126);
  101.             this.btnNoBorder.Name = "btnNoBorder";
  102.             this.btnNoBorder.Size = new System.Drawing.Size(145, 35);
  103.             this.btnNoBorder.TabIndex = 49;
  104.             this.btnNoBorder.Text = "No Border";
  105.             this.btnNoBorder.UseVisualStyleBackColor = true;
  106.             this.btnNoBorder.Click += new System.EventHandler(this.btnNoBorder_Click);
  107.             //
  108.             // btnEmbedForm
  109.             //
  110.             this.btnEmbedForm.FlatAppearance.BorderColor = System.Drawing.Color.White;
  111.             this.btnEmbedForm.FlatAppearance.BorderSize = 0;
  112.             this.btnEmbedForm.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  113.             this.btnEmbedForm.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  114.             this.btnEmbedForm.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  115.             this.btnEmbedForm.Font = new System.Drawing.Font("Segoe UI", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  116.             this.btnEmbedForm.ForeColor = System.Drawing.Color.White;
  117.             this.btnEmbedForm.Location = new System.Drawing.Point(0, 70);
  118.             this.btnEmbedForm.Name = "btnEmbedForm";
  119.             this.btnEmbedForm.Size = new System.Drawing.Size(145, 35);
  120.             this.btnEmbedForm.TabIndex = 48;
  121.             this.btnEmbedForm.Text = "Embed Form";
  122.             this.btnEmbedForm.UseVisualStyleBackColor = true;
  123.             this.btnEmbedForm.Click += new System.EventHandler(this.btnEmbedForm_Click);
  124.             //
  125.             // frmEmbeddedForms
  126.             //
  127.             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
  128.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  129.             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  130.             this.ClientSize = new System.Drawing.Size(626, 403);
  131.             this.Controls.Add(this.panContainer);
  132.             this.Controls.Add(this.panSideBar);
  133.             this.Controls.Add(this.panTitleBar);
  134.             this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  135.             this.ForeColor = System.Drawing.Color.White;
  136.             this.Name = "frmEmbeddedForms";
  137.             this.ShowInTaskbar = false;
  138.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  139.             this.Text = "frmEmbeddedForms";
  140.             this.panTitleBar.ResumeLayout(false);
  141.             this.panSideBar.ResumeLayout(false);
  142.             this.ResumeLayout(false);
  143.  
  144.         }
  145.  
  146.         #endregion
  147.         private System.Windows.Forms.Panel panTitleBar;
  148.         private System.Windows.Forms.Label lblTitle;
  149.         private System.Windows.Forms.Panel panContainer;
  150.         private System.Windows.Forms.Panel panSideBar;
  151.         private System.Windows.Forms.Button btnEmbedForm;
  152.         private System.Windows.Forms.Button btnNoBorder;
  153.         private System.Windows.Forms.Button btnHideSideBar;
  154.     }
  155. }
  156.  
  157.  
  158. //Form code
  159. using System;
  160. using System.Collections.Generic;
  161. using System.Data;
  162. using System.Drawing;
  163. using System.Linq;
  164. using System.Text;
  165. using System.Windows.Forms;
  166.  
  167. //CHANGE NAMESPACE NAME
  168. namespace EmbeddedForms
  169. {
  170.     public partial class frmEmbeddedForms : Form
  171.     {
  172.         System.Windows.Forms.Timer timer;
  173.         //Define here the Form class to be embedded
  174.         [Your Form Class] EmbeddedForm;
  175.  
  176.         public frmEmbeddedForms(Form MainForm) => InitializeComponent();
  177.  
  178.         private void btnEmbedForm_Click(object sender, EventArgs e)
  179.         {
  180.             EmbeddedForm = new [Your Form Class]() {
  181.                 TopLevel = false,
  182.                 Parent = panContainer,
  183.                 Location = new Point(4, 4),
  184.                 Enabled = true
  185.             };
  186.             EmbeddedForm.Show();
  187.         }
  188.  
  189.         private void btnNoBorder_Click(object sender, EventArgs e)
  190.         {
  191.             //Position (to compensate) the embedded Form when its borders are hidden
  192.             EmbeddedForm.Location = new Point(6, 6);
  193.             EmbeddedForm.FormBorderStyle = FormBorderStyle.None;
  194.         }
  195.  
  196.         private void btnHideSideBar_Click(object sender, EventArgs e)
  197.         {
  198.             //This timer slides the SideBar left. Another one is needed to slide right (if required)
  199.             timer = new Timer();
  200.             timer.Interval = 50;
  201.             timer.Tick += (s, ev) =>
  202.             {
  203.                 //Simply resize the SideBar
  204.                 //panSideBar.Width -= 5;
  205.                 //Or, slide the Panel's controls
  206.                 SideBar_SlideControls();
  207.                 if (panSideBar.Width <= 6)
  208.                 {
  209.                     panSideBar.Width = 6;
  210.                     timer.Stop();
  211.                     timer.Dispose();  //!important
  212.                 }
  213.             };
  214.             timer.Start();
  215.         }
  216.  
  217.         private void SideBar_SlideControls()
  218.         {
  219.             this.panSideBar.Controls.OfType<Control>().Select(ctl => ctl.Left -= 5).ToList();
  220.             this.panSideBar.Width -= 5;
  221.             panSideBar.Update();
  222.         }
  223.  
  224.         private void panContainer_Resize(object sender, EventArgs e)
  225.         {
  226.             //Resizes the embedded Form when the host panel resizes
  227.             Rectangle rect = panContainer.ClientRectangle;
  228.             rect.Inflate(-3, -3);
  229.             EmbeddedForm.Size = rect.Size;
  230.         }
  231.  
  232.         private void btnHideSideBar_MouseDown(object sender, MouseEventArgs e)
  233.         {
  234.             btnHideSideBar.FlatAppearance.BorderSize = 1;
  235.         }
  236.  
  237.         private void btnHideSideBar_MouseUp(object sender, MouseEventArgs e)
  238.         {
  239.             btnHideSideBar.FlatAppearance.BorderSize = 0;
  240.         }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment