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

📄 rebar.cs

📁 c#编写的仿OUTLOOK工具条的Winform菜单
💻 CS
📖 第 1 页 / 共 2 页
字号:
					| WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
				createParams.Style |= (int)(CommonControlStyles.CCS_NODIVIDER | CommonControlStyles.CCS_NOPARENTALIGN);
				
				createParams.Style |= (int)(RebarStyles.RBS_VARHEIGHT | RebarStyles.RBS_AUTOSIZE); 

				return createParams;
			}
		}

		static public void UpdateBandsColors(object sender, EventArgs e)
		{
			needsColorUpdate = true;
		}

		protected override void OnHandleCreated(EventArgs e)
		{
			base.OnHandleCreated(e);
			RealizeBands();
		}

		protected override void WndProc(ref Message m) 
		{
			base.WndProc(ref m);

			switch (m.Msg)
			{
				case (int)Msg.WM_PAINT:
					PaintBackground();
					break;
				case (int)Msg.WM_NOTIFY:
				case (int)((int)Msg.WM_NOTIFY + (int)Msg.WM_REFLECT):
				{
					NMHDR note = (NMHDR)m.GetLParam(typeof(NMHDR));
					switch (note.code)
					{
						case (int)RebarNotifications.RBN_HEIGHTCHANGE:
							UpdateSize();
							break;

						case (int)RebarNotifications.RBN_CHEVRONPUSHED:
							NotifyChevronPushed(ref m);
							break;
						
						case (int)RebarNotifications.RBN_CHILDSIZE:
							NotifyChildSize();
							break;
						case (int)NotificationMessages.NM_NCHITTEST:
							break;
					}
				}
					break;
			}
		}

		void NotifyChevronPushed(ref Message m)
		{
			NMREBARCHEVRON nrch = (NMREBARCHEVRON) m.GetLParam(typeof(NMREBARCHEVRON));	
			REBARBANDINFO rb;
			int bandIndex = nrch.uBand;
			rb = GetRebarInfo(bandIndex);
			int actualIndex = rb.wID;
			Control band = (Control)bands[actualIndex];
			Point point = new Point(nrch.rc.left, nrch.rc.bottom);
			(band as IChevron).Show(this, point);
			
		}

		void NotifyChildSize()
		{
			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.ToolbarSizeChanged();
			}
		}

		void BeginUpdate()
		{
			WindowsAPI.SendMessage(Handle, (int)Msg.WM_SETREDRAW, 0, 0);
		}

		void EndUpdate()
		{
			WindowsAPI.SendMessage(Handle, (int)Msg.WM_SETREDRAW, 1, 0);
		}

		bool IMessageFilter.PreFilterMessage(ref Message message)
		{
			ArrayList handles = new ArrayList();
			IntPtr handle = Handle;
			while (handle != IntPtr.Zero)
			{
				handles.Add(handle);
				handle = WindowsAPI.GetParent(handle);	
			}

			handle = message.HWnd;
			while (handle != IntPtr.Zero)
			{
				Msg currentMessage = (Msg)message.Msg;
				if (handles.Contains(handle)) 
					return PreProcessMessage(ref message);
				handle = WindowsAPI.GetParent(handle);
			}
			
			return false;
		}

		void RealizeBands()
		{
			ReleaseBands();
			BeginUpdate();
		
			for (int i = 0; i < bands.Count; i++)
			{
				REBARBANDINFO bandInfo = GetBandInfo(i);
				WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_INSERTBANDW, i, ref bandInfo);
			}

			UpdateSize();
			EndUpdate();
			CaptureBands();
		}

		void UpdateBand(int index)
		{
			if (!IsHandleCreated) return;
				
			BeginUpdate();

			// Make sure we get the right index according to the band position in the rebar
			// and not to the index in the toolbar collections which can or cannot match the order
			// in the rebar control
			int actualIndex = GetBandActualIndex(index);

			REBARBANDINFO rbbi = GetBandInfo(actualIndex);
			ToolBarEx tb = (ToolBarEx)bands[actualIndex];
			int idealSize = tb.GetIdealSize();
			rbbi.cxIdeal = idealSize;
			WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_SETBANDINFOW, index, ref rbbi);

			UpdateSize();
			EndUpdate();
		}

		int GetBandActualIndex(int bandIndex)
		{
			// This maps between the indexes in the band collection and the actual
			// indexes in the rebar that can actually change as the user moves
			// the bands around
			REBARBANDINFO rb;
			rb = GetRebarInfo(bandIndex);
			return rb.wID;
		}

		void UpdateSize()
		{
			Height = WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_GETBARHEIGHT, 0, 0) + 1;
		}

		public int GetRebarHeight()
		{
			int height = WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_GETBARHEIGHT, 0, 0) + 1;
            return height;			
		}

		public Rectangle GetBandRect(int bandIndex)
		{
			RECT rect = new RECT();
			int index = GetBandActualIndex(bandIndex);
			WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_GETRECT, bandIndex, ref rect);
			return new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
		}


		REBARBANDINFO GetRebarInfo(int index)
		{
			REBARBANDINFO rbbi = new REBARBANDINFO();
			rbbi.cbSize = Marshal.SizeOf(typeof(REBARBANDINFO));
			rbbi.fMask = (int)(RebarInfoMask.RBBIM_ID|RebarInfoMask.RBBIM_IDEALSIZE);
			WindowsAPI.SendMessage(Handle, (int)RebarMessages.RB_GETBANDINFOW, index, ref rbbi);
			return rbbi;
		}
		
		REBARBANDINFO GetBandInfo(int index)
		{
			Control band = bands[index];
			REBARBANDINFO rbbi = new REBARBANDINFO();
			rbbi.cbSize = Marshal.SizeOf(typeof(REBARBANDINFO));
					
			if ( !IsCommonCtrl6() )
			{
				rbbi.fMask = (int)RebarInfoMask.RBBIM_COLORS;
				rbbi.clrBack = (int)ColorUtil.RGB(ColorUtil.VSNetControlColor.R,
					ColorUtil.VSNetControlColor.G, ColorUtil.VSNetControlColor.B);
				
				rbbi.clrFore = (int)ColorUtil.RGB(255,0,255);
			}

			rbbi.iImage = 0;
			rbbi.hbmBack = IntPtr.Zero;
			rbbi.lParam = 0;
			rbbi.cxHeader = 0;

			rbbi.fMask |= (int)RebarInfoMask.RBBIM_ID;
			rbbi.wID = index;
	
			if ((band.Text != null) && (band.Text != string.Empty))
			{
				rbbi.fMask |= (int)RebarInfoMask.RBBIM_TEXT;
				rbbi.lpText = Marshal.StringToHGlobalAnsi(band.Text);
				rbbi.cch = (band.Text == null) ? 0 : band.Text.Length;
			}
	
			rbbi.fMask |= (int)RebarInfoMask.RBBIM_STYLE;
			rbbi.fStyle = (int)(RebarStylesEx.RBBS_CHILDEDGE | RebarStylesEx.RBBS_FIXEDBMP | RebarStylesEx.RBBS_GRIPPERALWAYS);
			ToolBarEx tb = (ToolBarEx)band;
			if ( tb.UseNewRow == true)
				rbbi.fStyle |= (int)(RebarStylesEx.RBBS_BREAK);
			rbbi.fStyle |= (band is IChevron) ? (int)RebarStylesEx.RBBS_USECHEVRON : 0;

			rbbi.fMask |= (int)(RebarInfoMask.RBBIM_CHILD);
			rbbi.hwndChild = band.Handle; 

			rbbi.fMask |= (int)(RebarInfoMask.RBBIM_CHILDSIZE);
			rbbi.cyMinChild = band.Height;
			rbbi.cxMinChild = 0;
			rbbi.cyChild = 0;
			rbbi.cyMaxChild = 0; 
			rbbi.cyIntegral = 0;
			
			rbbi.fMask |= (int)(RebarInfoMask.RBBIM_SIZE);
			rbbi.cx = band.Width;
			rbbi.fMask |= (int)(RebarInfoMask.RBBIM_IDEALSIZE);
			rbbi.cxIdeal = band.Width;
			
			return rbbi;				
		}

		void UpdateBands()
		{
			if (IsHandleCreated) RecreateHandle();
		}

		void Bands_Changed(Object s, EventArgs e)
		{
			UpdateBands();
		}

		void Band_HandleCreated(Object s, EventArgs e)
		{
			ReleaseBands();
				
			Control band = (Control) s;
			UpdateBand(bands.IndexOf(band));
			
			CaptureBands();
		}
		
		void Band_TextChanged(Object s, EventArgs e)
		{
			Control band = (Control) s;
			UpdateBand(bands.IndexOf(band));
		}

		void CaptureBands()
		{
			foreach (Control band in bands)
			{
				band.HandleCreated += new EventHandler(Band_HandleCreated);
				band.TextChanged += new EventHandler(Band_TextChanged);
			}
		}
		
		void ReleaseBands()
		{
			foreach (Control band in bands)
			{
				band.HandleCreated -= new EventHandler(Band_HandleCreated);
				band.TextChanged -= new EventHandler(Band_TextChanged);
			}
		}
	}
		
}

⌨️ 快捷键说明

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