Floating Toolbar : ToolBar « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ToolBarScreenshots 
Floating Toolbar
Floating Toolbar

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FloatingToolbar
{
  /// <summary>
  /// Summary description for FloatingToolbar.
  /// </summary>
  public class FloatingToolbar : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.ImageList imgButtons;
    internal System.Windows.Forms.ToolBar toolBar1;
    internal System.Windows.Forms.ToolBarButton cmdNew;
    internal System.Windows.Forms.ToolBarButton cmdOpen;
    internal System.Windows.Forms.ToolBarButton cmdClose;
    internal System.Windows.Forms.ToolBarButton cmdSave;
    internal System.Windows.Forms.ToolBarButton ToolBarButton1;
    internal System.Windows.Forms.ToolBarButton cmdPreview;
    private System.ComponentModel.IContainer components;

    public FloatingToolbar()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FloatingToolbar));
      this.imgButtons = new System.Windows.Forms.ImageList(this.components);
      this.toolBar1 = new System.Windows.Forms.ToolBar();
      this.cmdNew = new System.Windows.Forms.ToolBarButton();
      this.cmdOpen = new System.Windows.Forms.ToolBarButton();
      this.cmdClose = new System.Windows.Forms.ToolBarButton();
      this.cmdSave = new System.Windows.Forms.ToolBarButton();
      this.ToolBarButton1 = new System.Windows.Forms.ToolBarButton();
      this.cmdPreview = new System.Windows.Forms.ToolBarButton();
      this.SuspendLayout();
      // 
      // imgButtons
      // 
      this.imgButtons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imgButtons.ImageSize = new System.Drawing.Size(1616);
      this.imgButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgButtons.ImageStream")));
      this.imgButtons.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // toolBar1
      // 
      this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.cmdNew,
                                            this.cmdOpen,
                                            this.cmdClose,
                                            this.cmdSave,
                                            this.ToolBarButton1,
                                            this.cmdPreview});
      this.toolBar1.DropDownArrows = true;
      this.toolBar1.ImageList = this.imgButtons;
      this.toolBar1.Name = "toolBar1";
      this.toolBar1.ShowToolTips = true;
      this.toolBar1.Size = new System.Drawing.Size(29241);
      this.toolBar1.TabIndex = 5;
      this.toolBar1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseUp);
      this.toolBar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseMove);
      this.toolBar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.toolBar1_MouseDown);
      // 
      // cmdNew
      // 
      this.cmdNew.ImageIndex = 3;
      this.cmdNew.Text = "New";
      // 
      // cmdOpen
      // 
      this.cmdOpen.ImageIndex = 0;
      this.cmdOpen.Text = "Open";
      // 
      // cmdClose
      // 
      this.cmdClose.ImageIndex = 1;
      this.cmdClose.Text = "Close";
      // 
      // cmdSave
      // 
      this.cmdSave.ImageIndex = 2;
      this.cmdSave.Text = "Save";
      // 
      // ToolBarButton1
      // 
      this.ToolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
      // 
      // cmdPreview
      // 
      this.cmdPreview.ImageIndex = 4;
      this.cmdPreview.Text = "Preview";
      // 
      // FloatingToolbar
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.toolBar1});
      this.IsMdiContainer = true;
      this.Name = "FloatingToolbar";
      this.Text = "Floating Toolbar";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FloatingToolbar());
    }

    private bool draggingToolbar;
    private Point draggedFrom;


    private void toolBar1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      if (draggingToolbar)
      {
        //if (toolBar1.Dock == DockStyle.Top || toolBar1.Dock == DockStyle.Left) 
        if (toolBar1.Dock == DockStyle.Top)
        {
          // Check it the dragging has reached the threshold.
          if (draggedFrom.X < (e.X - 20|| draggedFrom.Y < (e.Y - 20))
          {
            draggingToolbar = false;
            
            // Disconnect the toolbar.
            toolBar1.Dock = DockStyle.None;
            toolBar1.Location = new Point(1010);
            toolBar1.Size = new Size(200100);
            toolBar1.BorderStyle = BorderStyle.FixedSingle;
          }

        }
        else if (toolBar1.Dock == DockStyle.None)
        {
          toolBar1.Left = e.X + toolBar1.Left - draggedFrom.X;
          toolBar1.Top = e.Y + toolBar1.Top - draggedFrom.Y;
          
          if (toolBar1.Top < 5)
          {
            draggingToolbar = false;
            
            // Re-dock the control.
            toolBar1.Dock = DockStyle.Top;
            toolBar1.BorderStyle = BorderStyle.None;
          }
          else if (toolBar1.Left < 5)
          {
            draggingToolbar = false;
            
            // Re-dock the control.
            toolBar1.Dock = DockStyle.Left;
            toolBar1.BorderStyle = BorderStyle.None;
          }
        }
      }
    }

    private void toolBar1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      draggingToolbar = true;
      draggedFrom = new Point(e.X, e.Y);
      toolBar1.Capture = true;

    }

    private void toolBar1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      draggingToolbar = false;
      toolBar1.Capture = false;

    }

  }
}


           
       
FloatingToolbar.zip( 30 k)
Related examples in the same category
1.ImageList for ToolBar
2.Using ToolBarButton
3.ToolBar float windowToolBar float window
4.ToolBar Example
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.