⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rebar.cs

📁 c#编写的仿OUTLOOK工具条的Winform菜单
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UtilityLibrary.Collections;
using UtilityLibrary.Win32;
using UtilityLibrary.General;

namespace UtilityLibrary.CommandBars
{
	/// <summary>
	/// Summary description for Rebar.
	/// </summary>
	public class ReBar : Control, IMessageFilter
	{
		RebarBandCollection bands = new RebarBandCollection();
		static bool needsColorUpdate = false;
		bool bGotIsCommonCtrl6 = false;
		bool isCommonCtrl6 = false;
			
		public ReBar()
		{
			SetStyle(ControlStyles.UserPaint, false);
			TabStop = false;
			Dock = DockStyle.Top;
			bands.Changed += new EventHandler(Bands_Changed);

		}

		private bool IsCommonCtrl6()
		{
			// Cache this value for efficenty
			if ( bGotIsCommonCtrl6 == false )
			{			
				DLLVERSIONINFO dllVersion = new DLLVERSIONINFO();
				// We are assummng here that anything greater or equal than 6
				// will have the new XP theme drawing enable
				dllVersion.cbSize = Marshal.SizeOf(typeof(DLLVERSIONINFO));
				WindowsAPI.GetCommonControlDLLVersion(ref dllVersion);
				bGotIsCommonCtrl6 = true;
				isCommonCtrl6 = (dllVersion.dwMajorVersion >= 6);
			}
			return isCommonCtrl6;
		}

		public void UpdateBackgroundColor()
		{
			for ( int i = 0; i < bands.Count; i++ )
			{
				// Update Rebar band information
				// This make sure that the background color and foreground color
				// of the bands are set to the new system colors
				UpdateBand(i);
				ToolBarEx toolBar = (ToolBarEx)bands[i];
				toolBar.Invalidate();
			}
			Invalidate();
		}
	
		private void PaintBackground()
		{
			if ( needsColorUpdate)
			{
				needsColorUpdate = false;
				for ( int i = 0; i < bands.Count; i++ )
				{
					// Update toolbar specific information
					// This update is to guarantee that the toolbar can resize
					// the buttons appropiately in case the SystemMenuFont was
					// changed along with the system colors
					ToolBarEx toolBar = (ToolBarEx)bands[i];
					toolBar.UpdateToolBarItems();
				}
				for ( int i = 0; i < bands.Count; i++ )
				{
					// Update Rebar band information
					// This make sure that the background color and foreground color
					// of the bands are set to the new system colors
					UpdateBand(i);
				}
			}

			// We don't need to paint the gripper if we are going
			// to let the operating system do the painting
			if ( IsCommonCtrl6() )
                return;
						
			Control c = null;
			Rectangle rc;
			for ( int i = 0; i < bands.Count; i++ )
			{
				Graphics g = CreateGraphics();
				c = bands[i];
				RectangleF rf = g.ClipBounds;
				rc = c.Bounds;
				if ( rf.Contains(rc) )
				{
				    ToolBarEx toolBar = (ToolBarEx)bands[i];
					if ( toolBar.BarType == BarType.MenuBar ) 
					{
						// The menu bar height is smaller that the other toolbars
						// and if the menubar is in the same row with a toolbar that is bigger in height
						// the toolbar gripper will not be painted correctly if we use the actual height
						// of the menubar. Instead ajust the rectangle to compensate for the actual height
						// of the band
                        Rectangle menuRect = GetBandRect(i);
						int offset = (menuRect.Height - rc.Height)/2-1;
						rc = new Rectangle(rc.Left, rc.Top-offset, menuRect.Width, menuRect.Height-2);
					}
					DrawGripper(g, rc);
				}
				g.Dispose();
			}
		}

		private void DrawGripper(Graphics g, Rectangle bounds)
		{
			bounds.X = bounds.Left - 7;
			bounds.Width = 7;

			g.FillRectangle(new SolidBrush(ColorUtil.VSNetControlColor), bounds);
			int nHeight = bounds.Height;
			for ( int i = 2; i < nHeight-1; i++) 
			{
				if ( ColorUtil.UsingCustomColor )
					g.DrawLine(new Pen(ColorUtil.VSNetBorderColor, 1), bounds.Left, bounds.Top+i, bounds.Left+3, bounds.Top+i);
				else 
					g.DrawLine(new Pen(SystemColors.ControlDark, 1), bounds.Left, bounds.Top+i, bounds.Left+3, bounds.Top+i);
				i++;
			}
		}

		protected bool HitGripper(MouseEventArgs e)
		{
			// Find out if we hit the gripper
			Point mousePoint = new Point(e.X, e.Y);
			Control c = null;
			Rectangle bounds;
			for ( int i = 0; i < bands.Count; i++ )
			{
				c = bands[i];
				bounds = c.Bounds;
				
				// adjust to gripper area
				bounds.X = bounds.Left - 7;
				bounds.Width = 7;
				
				if ( bounds.Contains(mousePoint) )
					return true;
			}
			return false;

		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if ( Capture )
				return;

			bool hit = HitGripper(e);
			if ( hit )
			{
				if ( ShowMoveCursor(e) )
					Cursor.Current = Cursors.SizeAll;
				else
					Cursor.Current = Cursors.SizeWE;
			}
			else
				Cursor.Current = Cursors.Default;
		}

		private bool ShowMoveCursor(MouseEventArgs e)
		{
			// Even though we can actually move the toolbars around always
			// sometimes it is more intuive to show the "Move" cursor depending
			// how many bars are in the same row that always showing the resize cursor
			Point mousePoint = new Point(e.X, e.Y);
			Control c = null;
			Rectangle bounds;
			for ( int i = 0; i < bands.Count; i++ )
			{
				c = bands[i];
				bounds = c.Bounds;
				
				// adjust to gripper area
				bounds.X = bounds.Left - 7;
				bounds.Width = 7;
				
				if ( bounds.Contains(mousePoint) )
				{
					if ( bounds.Left <= 5 )
					{   // The left value would be actually at least 2 if the toolbar
						// is on the edge of the main window as opossed to be somewhere in the middle of the
						// strip. The assumption here is that the gripper starts approximately 2 pixel from the edge
						return true;
					}
				}
			}
			
			return false;
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			bool hit = HitGripper(e);
			if ( hit ) 
			{
					Capture = true;
				if ( ShowMoveCursor(e) )
					Cursor.Current = Cursors.SizeAll;
				else
					Cursor.Current = Cursors.VSplit;
			}
			else
				Cursor.Current = Cursors.Default;

		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp(e);
			bool hit = HitGripper(e);
			Capture = false;
			if ( hit )
			{
				if ( ShowMoveCursor(e) )
					Cursor.Current = Cursors.SizeAll;
				else
					Cursor.Current = Cursors.SizeWE;
			}
			else
				Cursor.Current = Cursors.Default;
			
		}

		protected override void Dispose(bool disposing)
		{
			bands.Changed -= new EventHandler(Bands_Changed);
		}
	
		public RebarBandCollection Bands
		{
			get { return bands; }
		}
	
		public override bool PreProcessMessage(ref Message msg)
		{
			foreach (Control band in bands)
			{
				if (band.PreProcessMessage(ref msg))
					return true;
			}
			return false;
		}
		
		protected override void OnParentChanged(EventArgs e)
		{
			if (Parent != null)
				Application.AddMessageFilter(this);
			else
				Application.RemoveMessageFilter(this);	
		}

		protected override Size DefaultSize
		{
			get { return new Size(100, 22 * 2); }
		}

		protected override void CreateHandle() 
		{
			if (!RecreatingHandle)
			{
				INITCOMMONCONTROLSEX icex = new INITCOMMONCONTROLSEX();
				icex.dwSize = Marshal.SizeOf(typeof(INITCOMMONCONTROLSEX));
				icex.dwICC = (int)(CommonControlInitFlags.ICC_BAR_CLASSES | CommonControlInitFlags.ICC_COOL_CLASSES);
				bool  fail = WindowsAPI.InitCommonControlsEx(icex);
			}
			base.CreateHandle();
		}
	
		protected override CreateParams CreateParams
		{
			get
			{
				CreateParams createParams = base.CreateParams;
				createParams.ClassName = WindowsAPI.REBARCLASSNAME;
				createParams.Style = (int)(WindowStyles.WS_CHILD | WindowStyles.WS_VISIBLE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -