public int Add(string text) { MenuBoxItem value = new MenuBoxItem(text); int index = owner.itemsCount; if (owner.items == null) { owner.items = new MenuBoxItem[5]; } else if (owner.items.Length == owner.itemsCount) { MenuBoxItem[] newItems = new MenuBoxItem[owner.itemsCount + 5]; Array.Copy(owner.items, 0, newItems, 0, owner.itemsCount); owner.items = newItems; } if (index < owner.itemsCount) { Array.Copy(owner.items, index, owner.items, index + 1, owner.itemsCount - index); } value.owner = owner; owner.items[index] = value; owner.itemsCount++; owner.NativeInsert(value, index); return(index); }
internal void NativeUpdateButtonAt(MenuBoxItem value, int index) { value.owner = this; items[index] = value; NativeMethods.TBBUTTONINFO btnINFO = value.GetTBBUTTONINFO(index); UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TB_SETBUTTONINFO, index, ref btnINFO); }
public int Add(MenuBoxItem value) { if (value == null) { throw new ArgumentNullException("button"); } int index = owner.itemsCount; if (owner.items == null) { owner.items = new MenuBoxItem[5]; } else if (owner.items.Length == owner.itemsCount) { MenuBoxItem[] newItems = new MenuBoxItem[owner.itemsCount + 5]; Array.Copy(owner.items, 0, newItems, 0, owner.itemsCount); owner.items = newItems; } if (index < owner.itemsCount) { Array.Copy(owner.items, index, owner.items, index + 1, owner.itemsCount - index); } value.owner = owner; owner.items[index] = value; owner.itemsCount++; owner.NativeInsert(value, index); return(index); }
public void Insert(int index, MenuBoxItem value) { if (value == null) { throw new ArgumentNullException("无效 value"); } if (index < 0 || ((owner.items != null) && (index > owner.itemsCount))) { throw new ArgumentOutOfRangeException("无效 index"); } if (owner.items == null) { owner.items = new MenuBoxItem[5]; } else if (owner.items.Length == owner.itemsCount) { MenuBoxItem[] newItems = new MenuBoxItem[owner.itemsCount + 5]; Array.Copy(owner.items, 0, newItems, 0, owner.itemsCount); owner.items = newItems; } if (index < owner.itemsCount) { Array.Copy(owner.items, index, owner.items, index + 1, owner.itemsCount - index); } owner.items[index] = value; owner.itemsCount++; owner.NativeInsert(value, index); }
public void Remove(MenuBoxItem value) { int index = IndexOf(value); if (index != -1) { RemoveAt(index); } }
protected virtual object[] GetItems() { MenuBoxItem[] result = new MenuBoxItem[itemsCount]; if (itemsCount > 0) { Array.Copy(items, 0, result, 0, itemsCount); } return(result); }
public int FindIndex(MenuBoxItem value) { for (int x = 0; x < owner.Items.Count; x++) { if (owner.Items[x] == this) { return(x); } } return(-1); }
public int IndexOf(MenuBoxItem value) { for (int index = 0; index < Count; ++index) { if (this[index] == value) { return(index); } } return(-1); }
internal void NativeInsert(MenuBoxItem value, int index) { if (value == null) { throw new ArgumentNullException("value"); } if (IsHandleCreated) { NativeMethods.TBBUTTON tbbutton = value.GetTBBUTTON(index); int result = (int)UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_INSERTBUTTON, index, ref tbbutton); if (result != 1) { throw new Win32Exception("insert button fault"); } NativeMethods.TBBUTTONINFO tbbuttonINFO = value.GetTBBUTTONINFO(index); UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_SETBUTTONINFO, index, ref tbbuttonINFO); } }
private void TrackPopupMenu(int index) { while (index >= 0) { npopupIndex = -1; cpopupIndex = index; UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_PRESSBUTTON, index, 1); UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_SETHOTITEM, index, IntPtr.Zero); MenuBoxItem tbb = items[index]; tbb.mpressed = true; //安装钩子 NativeMethods.WindowsHookProc hookProc = new NativeMethods.WindowsHookProc(MessageHook); messageHookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_MSGFILTER, hookProc, NativeMethods.NullHandleRef, UnsafeNativeMethods.GetCurrentThreadId()); if (messageHookHandle == IntPtr.Zero) { throw new Win32Exception("SetWindowsHookEx Failt"); } Menu menu = tbb.DropDownMenu; if (menu != null) { NativeMethods.RECT rc = new NativeMethods.RECT(); NativeMethods.TPMPARAMS tpm = new NativeMethods.TPMPARAMS(); UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_GETRECT, index, ref rc); if ((menu.GetType()).IsAssignableFrom(typeof(ContextMenu))) { ((ContextMenu)menu).Show(this, new Point(rc.left, rc.bottom)); } } base.Update(); //卸载钩子 UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(null, messageHookHandle)); messageHookHandle = IntPtr.Zero; if (menu != null) { UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TB_PRESSBUTTON, index, 0); } index = npopupIndex; } UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TB_SETHOTITEM, -1, IntPtr.Zero); }
private void WmPrepaint(ref Message m) { m.Result = (IntPtr)NativeMethods.CDRF_DODEFAULT; NativeMethods.NMTBCUSTOMDRAW tbcd = (NativeMethods.NMTBCUSTOMDRAW)m.GetLParam(typeof(NativeMethods.NMTBCUSTOMDRAW)); MenuBoxItem item = this.items[(int)tbcd.nmcd.dwItemSpec]; bool hot = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_HOT) != 0); bool selected = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_SELECTED) != 0); bool mchecked = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_CHECKED) != 0); bool disabled = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_DISABLED) != 0); bool focus = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_FOCUS) != 0); bool grayed = ((tbcd.nmcd.uItemState & NativeMethods.CDIS_GRAYED) != 0); if (hot || selected) { DrawIEMenuBarItem(item, ref tbcd); m.Result = (IntPtr)NativeMethods.CDRF_SKIPDEFAULT; } }
private bool MessageFilter(ref Message m) { if (IsHandleCreated) { UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TB_PRESSBUTTON, cpopupIndex, 1); UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), NativeMethods.TB_SETHOTITEM, cpopupIndex, IntPtr.Zero); MenuBoxItem tbb = Items[cpopupIndex]; tbb.mpressed = true; switch (m.Msg) { case NativeMethods.WM_KEYDOWN: return(HandleKeyMessage(ref m)); case NativeMethods.WM_LBUTTONDOWN: case NativeMethods.WM_MOUSEMOVE: return(HandleMouseMessage(ref m)); default: break; } } return(false); }
private void DrawIEMenuBarItem(MenuBoxItem item, ref NativeMethods.NMTBCUSTOMDRAW tbcd) { NativeMethods.RECT rect = tbcd.nmcd.rc; rect.top += 1; IntPtr hDC = tbcd.nmcd.hdc; string text = item.Text; ///FillRect IntPtr hBrush = UnsafeNativeMethods.CreateSolidBrush(hightLightColor); UnsafeNativeMethods.FillRect(hDC, ref rect, hBrush); ///MeasureText NativeMethods.RECT lprt = new NativeMethods.RECT(); UnsafeNativeMethods.DrawText(hDC, text, text.Length, ref lprt, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT | NativeMethods.DT_CALCRECT);//获得文字高度 ///Drawtext IntPtr hOldFont = UnsafeNativeMethods.SelectObject(hDC, hFont); int oldBkMode = UnsafeNativeMethods.SetBkMode(hDC, NativeMethods.TRANSPARENT); int oldTextColor = UnsafeNativeMethods.SetTextColor(hDC, hightLightTextColor); NativeMethods.RECT clip = new NativeMethods.RECT(); clip.left = rect.left + ((rect.right - rect.left - lprt.right) / 2) + 2; clip.top = rect.top + ((rect.bottom - rect.top - lprt.bottom) / 2) + 2; clip.right = clip.left + lprt.right; clip.bottom = clip.top + lprt.bottom; UnsafeNativeMethods.DrawText(hDC, text, text.Length, ref clip, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT); ///Free UnsafeNativeMethods.SetTextColor(hDC, oldTextColor); UnsafeNativeMethods.SetBkMode(hDC, oldBkMode); UnsafeNativeMethods.SelectObject(hDC, hOldFont); if (hBrush != IntPtr.Zero) { UnsafeNativeMethods.DeleteObject(hBrush); } }
public bool Contains(MenuBoxItem value) { return(IndexOf(value) != -1); }
private void WmReflectNotifyChevronPushed(ref Message m) { NativeMethods.NMREBARCHEVRON nrch = (NativeMethods.NMREBARCHEVRON)m.GetLParam(typeof(NativeMethods.NMREBARCHEVRON)); RebarBand band = Bands[nrch.wID]; ContextMenu childMenu = new ContextMenu(); if (band.Control != null && band.Control is ToolBox) { ToolBox toolBar = band.Control as ToolBox; NativeMethods.RECT lpRect = new NativeMethods.RECT(); UnsafeNativeMethods.GetClientRect(new HandleRef(band.Control, band.Control.Handle), ref lpRect); int count = (int)UnsafeNativeMethods.SendMessage(new HandleRef(band.Control, band.Control.Handle), NativeMethods.TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero); if (count != toolBar.buttonsCount) { throw new ArgumentOutOfRangeException("count"); } for (int i = 0; i < count; i++) { NativeMethods.RECT brc = new NativeMethods.RECT(); UnsafeNativeMethods.SendMessage(new HandleRef(band.Control, band.Control.Handle), NativeMethods.TB_GETITEMRECT, i, ref brc); if (brc.right >= (lpRect.right - lpRect.left)) { ToolBoxButton btn = toolBar.Buttons[i]; if ((btn.Appearance & TBBAppearance.Separator) == 0) { string text = string.Empty; if (!string.IsNullOrEmpty(btn.Text)) { text = btn.Text; } else if (!string.IsNullOrEmpty(btn.ToolTipText)) { text = btn.ToolTipText; } else { text = btn.MenuText; } MenuItem menuItem = null; if (btn.Enable) { menuItem = new MenuItem(text, OnMenuItemClick); menuItem.Tag = btn; toolBar.Renderer.SetEnable(menuItem, true); } else { menuItem = new MenuItem(text); toolBar.Renderer.SetEnable(menuItem, false); } toolBar.Renderer.SetImageIndex(menuItem, btn.ImageIndex); childMenu.MenuItems.Add(menuItem); if (btn.DropDownMenu != null) { foreach (MenuItem item in btn.DropDownMenu.MenuItems) { MenuItem newMenuItem = new MenuItem(item.Text, OnMenuSubItemClick); newMenuItem.Tag = item; menuItem.MenuItems.Add(newMenuItem); } } } } } } if (band.Control != null && band.Control is MenuBox) { MenuBox menuBar = band.Control as MenuBox; NativeMethods.RECT lpRect = new NativeMethods.RECT(); UnsafeNativeMethods.GetClientRect(new HandleRef(band.Control, band.Control.Handle), ref lpRect); int count = (int)UnsafeNativeMethods.SendMessage(new HandleRef(band.Control, band.Control.Handle), NativeMethods.TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero); if (count != menuBar.itemsCount) { throw new ArgumentOutOfRangeException("count"); } for (int i = 0; i < count; i++) { NativeMethods.RECT brc = new NativeMethods.RECT(); UnsafeNativeMethods.SendMessage(new HandleRef(band.Control, band.Control.Handle), NativeMethods.TB_GETITEMRECT, i, ref brc); if (brc.right >= (lpRect.right - lpRect.left)) { MenuBoxItem menuBarItem = menuBar.Items[i]; MenuItem menuItem = new MenuItem(menuBarItem.Text); childMenu.MenuItems.Add(menuItem); if (menuBarItem.DropDownMenu != null) { foreach (MenuItem item in menuBarItem.DropDownMenu.MenuItems) { MenuItem newMenuItem = new MenuItem(item.Text, OnMenuSubItemClick); newMenuItem.Tag = item; menuItem.MenuItems.Add(newMenuItem); } } } } } childMenu.Show(this, new Point(nrch.rc.left, nrch.rc.bottom)); }