Exemplo n.º 1
0
        // ------------------------------------------------------
        //
        // Internal Methods
        //
        // ------------------------------------------------------

        #region Internal Methods

        // Create a WindowsToolbar instance.  Needs to be internal because
        // ApplicationWindow pattern needs to call this so needs to be internal
        internal ProxySimple CreateToolbarItem(int item)
        {
            NativeMethods.TBBUTTON tbb = new NativeMethods.TBBUTTON();

            // During the FocusChanged WinEvent (EVENT_OBJECT_FOCUS),
            // some "ToolbarWindow32" children report an item ID (child id)
            // of 0x80000001, 0x80000002, etc. instead of 1, 2, etc.
            // However, when created as children of the parent toolbar,
            // these same items are assigned IDs of 1, 2, etc.
            // Therefore, map negative item IDs of the form 0x80000001,
            // 0x80000002, etc. to 1, 2, etc.
            item = (int)(~0x80000000 & (uint)item);

            if (!XSendMessage.GetItem(_hwnd, item, ref tbb))
            {
                // If failed to get button infromation the button must not exist, so return null.
                return(null);
            }

            if (Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONHIDDEN, new IntPtr(tbb.idCommand), IntPtr.Zero) == 0)
            {
                Accessible acc = Accessible.CreateNativeFromEvent(_hwnd, NativeMethods.OBJID_CLIENT, item + 1);
                if (acc != null)
                {
                    if (acc.Role == AccessibleRole.MenuItem)
                    {
                        return(new ToolbarItemAsMenuItem(_hwnd, this, item, tbb.idCommand, acc));
                    }
                }

                return(new ToolbarItem(_hwnd, this, item, tbb.idCommand));
            }
            return(null);
        }
Exemplo n.º 2
0
 public NativeMethods.TBBUTTON GetTBBUTTON(int command)
 {
     NativeMethods.TBBUTTON btn = new NativeMethods.TBBUTTON();
     btn.fsStyle   = NativeMethods.TBSTYLE_BUTTON | NativeMethods.TBSTYLE_AUTOSIZE;//| NativeMethods.TBSTYLE_DROPDOWN;
     btn.dwData    = (IntPtr)0;
     btn.idCommand = command;
     btn.iString   = (IntPtr)(-1);
     if (!string.IsNullOrEmpty(text))
     {
         btn.iString = Marshal.StringToHGlobalAuto(text + "\0\0");
     }
     if (mchecked)
     {
         btn.fsState |= NativeMethods.TBSTATE_CHECKED;
     }
     if (menable)
     {
         btn.fsState |= NativeMethods.TBSTATE_ENABLED;
     }
     if (mpressed)
     {
         btn.fsState |= NativeMethods.TBSTATE_PRESSED;
     }
     if (mhidden)
     {
         btn.fsState |= NativeMethods.TBSTATE_HIDDEN;
     }
     return(btn);
 }
Exemplo n.º 3
0
        public NativeMethods.TBBUTTON GetTBBUTTON(int command)
        {
            NativeMethods.TBBUTTON button = new NativeMethods.TBBUTTON();
            button.iBitmap = imageIndex;

            button.fsStyle = 0;
            switch (appearance)
            {
            case TBBAppearance.Button:
                button.fsStyle = NativeMethods.TBSTYLE_BUTTON;
                break;

            case TBBAppearance.Separator:
                button.fsStyle = NativeMethods.TBSTYLE_SEP;
                break;

            case TBBAppearance.DropDown:
                button.fsStyle = NativeMethods.TBSTYLE_DROPDOWN;
                break;

            case TBBAppearance.Check:
                button.fsStyle = NativeMethods.TBSTYLE_CHECK;
                break;

            case TBBAppearance.WholeDropDown:
                button.fsStyle = NativeMethods.BTNS_WHOLEDROPDOWN;
                break;
            }

            button.fsState = 0;
            if (mchecked)
            {
                button.fsState |= NativeMethods.TBSTATE_CHECKED;
            }
            if (menable)
            {
                button.fsState |= NativeMethods.TBSTATE_ENABLED;
            }
            if (mpressed)
            {
                button.fsState |= NativeMethods.TBSTATE_PRESSED;
            }
            if (mhidden)
            {
                button.fsState |= NativeMethods.TBSTATE_HIDDEN;
            }

            button.dwData    = (IntPtr)0;
            button.idCommand = command;
            if (!string.IsNullOrEmpty(text))
            {
                button.iString = Marshal.StringToHGlobalAuto(text + "\0\0");
            }
            else
            {
                button.iString = (IntPtr)(-1);
            }
            return(button);
        }
Exemplo n.º 4
0
        // ------------------------------------------------------
        //
        // Constructors
        //
        // ------------------------------------------------------

        #region Constructors

        internal ToolbarItem(IntPtr hwnd, ProxyFragment parent, int item, int idCommand)
            : base(hwnd, parent, item)
        {
            _idCommand = idCommand;

            NativeMethods.TBBUTTON tbb = new NativeMethods.TBBUTTON();
            int buttonStyle            = 0;

            if (XSendMessage.GetItem(_hwnd, _item, ref tbb))
            {
                buttonStyle = tbb.fsStyle;
            }

            // Set the strings to return properly the properties.
            bool hasImageList = Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_GETIMAGELIST, IntPtr.Zero, IntPtr.Zero) != 0;
            int  exStyle      = Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_GETEXTENDEDSTYLE, IntPtr.Zero, IntPtr.Zero);

            _isToggleButton = false;
            _cControlType   = ControlType.Button;

            // If a separator, say so
            if (Misc.IsBitSet(buttonStyle, NativeMethods.BTNS_SEP))
            {
                _cControlType = ControlType.Separator;
            }
            else if (Misc.IsBitSet(buttonStyle, NativeMethods.BTNS_CHECK))
            {
                // Special case for task list - they use the checked style, but only for visuals...
                IntPtr hwndParent = Misc.GetParent(_hwnd);
                if (Misc.GetClassName(hwndParent) != "MSTaskSwWClass")
                {
                    _isToggleButton = true;
                }
            }
            else if (Misc.IsBitSet(buttonStyle, NativeMethods.BTNS_DROPDOWN) &&
                     Misc.IsBitSet(exStyle, NativeMethods.TBSTYLE_EX_DRAWDDARROWS))
            {
                // if its a drop down and it has an arrow its a split button
                _cControlType = ControlType.SplitButton;
            }
            else if (!hasImageList || tbb.iBitmap == NativeMethods.I_IMAGENONE)
            {
                // Text-only, no bitmap, so it's effectively a menu item.
                // (eg. as used in MMC)
                _cControlType = ControlType.MenuItem;
            }

            _fIsContent = _cControlType != ControlType.Separator;

            // The Start Menu's "Shut Down" and "Log Off" buttons are toolbar items.  They need to have the
            // KeyboardFocusable property be set to true.
            _fIsKeyboardFocusable = (bool)parent.GetElementProperty(AutomationElement.IsKeyboardFocusableProperty);

            GetItemId(ref _sAutomationId);
        }
Exemplo n.º 5
0
        private void GetItemId(ref string itemId)
        {
            NativeMethods.TBBUTTON tbb = new NativeMethods.TBBUTTON();

            if (XSendMessage.GetItem(_hwnd, _item, ref tbb))
            {
                if (tbb.idCommand > 0)
                {
                    itemId = "Item " + tbb.idCommand.ToString(CultureInfo.CurrentCulture);
                }
            }
        }
Exemplo n.º 6
0
 internal void NativeInsert(ToolBoxButton value, int index)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     if (IsHandleCreated)
     {
         if ((value.Appearance & TBBAppearance.Separator) != 0)
         {
             value.Width = 0;
         }
         else
         {
             Size edge = SystemInformation.Border3DSize;
             Size size = Size.Empty;
             if (!string.IsNullOrEmpty(value.Text))
             {
                 using (Graphics g = Graphics.FromHwnd(Handle))
                 {
                     if (!string.IsNullOrEmpty(value.Text))
                     {
                         size        = Size.Ceiling(g.MeasureString(value.Text, Font));
                         size.Width += 10;
                     }
                 }
             }
             if ((appearance & TBAppearance.List) != 0)//如果有List属性
             {
                 value.Width = size.Width + ImageSize.Width + edge.Width * 3;
             }
             else
             {
                 value.Width = size.Width > ImageSize.Width ? size.Width + edge.Width * 4 : ImageSize.Width + edge.Width * 4;
             }
             if ((value.Appearance & TBBAppearance.DropDown) != 0)
             {
                 value.Width += 16;
             }
             if ((value.Appearance & TBBAppearance.WholeDropDown) != 0)
             {
                 value.Width += 8;
             }
         }
         NativeMethods.TBBUTTON tbbutton = value.GetTBBUTTON(index);
         UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_INSERTBUTTON, index, ref tbbutton);
         NativeMethods.TBBUTTONINFO tbbuttonINFO = value.GetTBBUTTONINFO(index);
         UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.TB_SETBUTTONINFO, index, ref tbbuttonINFO);
     }
 }
Exemplo n.º 7
0
 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);
     }
 }
Exemplo n.º 8
0
        private void AddItems()
        {
            NativeMethods.SendMessage(Handle, NativeMethods.TB_BUTTONSTRUCTSIZE, Marshal.SizeOf(typeof(NativeMethods.TBBUTTON)), 0);

            int extendedStyle = NativeMethods.TBSTYLE_EX_HIDECLIPPEDBUTTONS | NativeMethods.TBSTYLE_EX_DOUBLEBUFFER;
            if (style == CommandBarStyle.ToolBar)
            {
                extendedStyle |= NativeMethods.TBSTYLE_EX_DRAWDDARROWS;
            }

            NativeMethods.SendMessage(Handle, NativeMethods.TB_SETEXTENDEDSTYLE, 0, extendedStyle);

            this.UpdateImageList();

            for (int i = 0; i < items.Count; i++)
            {
                NativeMethods.TBBUTTON button = new NativeMethods.TBBUTTON();
                button.idCommand = i;
                NativeMethods.SendMessage(this.Handle, NativeMethods.TB_INSERTBUTTON, i, ref button);

                NativeMethods.TBBUTTONINFO buttonInfo = this.GetButtonInfo(i);
                NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETBUTTONINFO, i, ref buttonInfo);
            }

            // Add ComboBox controls.
            this.Controls.Clear();
            for (int i = 0; i < items.Count; i++)
            {
                CommandBarComboBox comboBox = this.items[i] as CommandBarComboBox;
                if (comboBox != null)
                {
                    NativeMethods.RECT rect = new NativeMethods.RECT();
                    NativeMethods.SendMessage(this.Handle, NativeMethods.TB_GETITEMRECT, i, ref rect);

                    rect.top = rect.top + (((rect.bottom - rect.top) - comboBox.Height) / 2);

                    comboBox.ComboBox.Location = new Point(rect.left, rect.top);
                    this.Controls.Add(comboBox.ComboBox);
                }
            }

            this.UpdateSize();
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows Vista and earlier.)
        /// </summary>
        /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param>
        /// <returns>The location of the specified NotifyIcon.</returns>
        public static Rect? GetNotifyIconRectLegacy(NotifyIcon notifyicon)
        {
            Rect? nirect = null;

            FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            int niid = (int)idFieldInfo.GetValue(notifyicon);

            FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon);
            IntPtr nihandle = nativeWindow.Handle;
            if (nihandle == null || nihandle == IntPtr.Zero)
                return null;

            // find the handle of the task bar
            IntPtr taskbarparenthandle = NativeMethods.FindWindow("Shell_TrayWnd", null);

            if (taskbarparenthandle == (IntPtr)null)
                return null;

            // find the handle of the notification area
            IntPtr naparenthandle = NativeMethods.FindWindowEx(taskbarparenthandle, IntPtr.Zero, "TrayNotifyWnd", null);

            if (naparenthandle == (IntPtr)null)
                return null;

            // make a list of toolbars in the notification area (one of them should contain the icon)
            List<IntPtr> natoolbarwindows = NativeMethods.GetChildToolbarWindows(naparenthandle);

            bool found = false;

            for (int i = 0; !found && i < natoolbarwindows.Count; i++)
            {
                IntPtr natoolbarhandle = natoolbarwindows[i];

                // retrieve the number of toolbar buttons (i.e. notify icons)
                int buttoncount = NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();

                // get notification area's process id
                uint naprocessid;
                NativeMethods.GetWindowThreadProcessId(natoolbarhandle, out naprocessid);

                // get handle to notification area's process
                IntPtr naprocesshandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.All, false, naprocessid);

                if (naprocesshandle == IntPtr.Zero)
                    return null;

                // allocate enough memory within the notification area's process to store the button info we want
                IntPtr toolbarmemoryptr = NativeMethods.VirtualAllocEx(naprocesshandle, (IntPtr)null, (uint)Marshal.SizeOf(typeof(NativeMethods.TBBUTTON)), NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);

                if (toolbarmemoryptr == IntPtr.Zero)
                    return null;

                try
                {
                    // loop through the toolbar's buttons until we find our notify icon
                    for (int j = 0; !found && j < buttoncount; j++)
                    {
                        int bytesread = -1;

                        // ask the notification area to give us information about the current button
                        NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETBUTTON, new IntPtr(j), toolbarmemoryptr);

                        // retrieve that information from the notification area's process
                        NativeMethods.TBBUTTON buttoninfo = new NativeMethods.TBBUTTON();
                        NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out buttoninfo, Marshal.SizeOf(buttoninfo), out bytesread);

                        if (bytesread != Marshal.SizeOf(buttoninfo))
                            return null;

                        if (buttoninfo.dwData == IntPtr.Zero)
                            return null;

                        // the dwData field contains a pointer to information about the notify icon:
                        // the handle of the notify icon (an 4/8 bytes) and the id of the notify icon (4 bytes)
                        IntPtr niinfopointer = buttoninfo.dwData;

                        // read the notify icon handle
                        IntPtr nihandlenew;
                        NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer, out nihandlenew, Marshal.SizeOf(typeof(IntPtr)), out bytesread);

                        if (bytesread != Marshal.SizeOf(typeof(IntPtr)))
                            return null;

                        // read the notify icon id
                        uint niidnew;
                        NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer + Marshal.SizeOf(typeof(IntPtr)), out niidnew, Marshal.SizeOf(typeof(uint)), out bytesread);

                        if (bytesread != Marshal.SizeOf(typeof(uint)))
                            return null;

                        // if we've found a match
                        if (nihandlenew == nihandle && niidnew == niid)
                        {
                            // check if the button is hidden: if it is, return the rectangle of the 'show hidden icons' button
                            if ((byte)(buttoninfo.fsState & NativeMethods.TBSTATE_HIDDEN) != 0)
                            {
                                nirect = GetNotifyAreaButtonRectangle();
                            }
                            else
                            {
                                NativeMethods.RECT result = new NativeMethods.RECT();

                                // get the relative rectangle of the toolbar button (notify icon)
                                NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETITEMRECT, new IntPtr(j), toolbarmemoryptr);

                                NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out result, Marshal.SizeOf(result), out bytesread);

                                if (bytesread != Marshal.SizeOf(result))
                                    return null;

                                // find where the rectangle lies in relation to the screen
                                NativeMethods.MapWindowPoints(natoolbarhandle, (IntPtr)null, ref result, 2);

                                nirect = result;
                            }

                            found = true;
                        }
                    }
                }
                finally
                {
                    // free memory within process
                    NativeMethods.VirtualFreeEx(naprocesshandle, toolbarmemoryptr, 0, NativeMethods.FreeType.Release);

                    // close handle to process
                    NativeMethods.CloseHandle(naprocesshandle);
                }
            }

            return nirect;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows Vista and earlier.)
        /// </summary>
        /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param>
        /// <returns>The location of the specified NotifyIcon.</returns>
        public static Rectangle?GetNotifyIconRectLegacy(NotifyIcon notifyicon)
        {
            Rectangle?nirect = null;

            FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            int       niid        = (int)idFieldInfo.GetValue(notifyicon);

            FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);

            System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon);
            IntPtr nihandle = nativeWindow.Handle;

            if (nihandle == null || nihandle == IntPtr.Zero)
            {
                return(null);
            }

            // find the handle of the task bar
            IntPtr taskbarparenthandle = NativeMethods.FindWindow("Shell_TrayWnd", null);

            if (taskbarparenthandle == (IntPtr)null)
            {
                return(null);
            }

            // find the handle of the notification area
            IntPtr naparenthandle = NativeMethods.FindWindowEx(taskbarparenthandle, IntPtr.Zero, "TrayNotifyWnd", null);

            if (naparenthandle == (IntPtr)null)
            {
                return(null);
            }

            // make a list of toolbars in the notification area (one of them should contain the icon)
            List <IntPtr> natoolbarwindows = NativeMethods.GetChildToolbarWindows(naparenthandle);

            bool found = false;

            for (int i = 0; !found && i < natoolbarwindows.Count; i++)
            {
                IntPtr natoolbarhandle = natoolbarwindows[i];

                // retrieve the number of toolbar buttons (i.e. notify icons)
                int buttoncount = NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();

                // get notification area's process id
                uint naprocessid;
                NativeMethods.GetWindowThreadProcessId(natoolbarhandle, out naprocessid);

                // get handle to notification area's process
                IntPtr naprocesshandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.All, false, naprocessid);

                if (naprocesshandle == IntPtr.Zero)
                {
                    return(null);
                }

                // allocate enough memory within the notification area's process to store the button info we want
                IntPtr toolbarmemoryptr = NativeMethods.VirtualAllocEx(naprocesshandle, (IntPtr)null, (uint)Marshal.SizeOf(typeof(NativeMethods.TBBUTTON)), NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);

                if (toolbarmemoryptr == IntPtr.Zero)
                {
                    return(null);
                }

                try
                {
                    // loop through the toolbar's buttons until we find our notify icon
                    for (int j = 0; !found && j < buttoncount; j++)
                    {
                        int bytesread = -1;

                        // ask the notification area to give us information about the current button
                        NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETBUTTON, new IntPtr(j), toolbarmemoryptr);

                        // retrieve that information from the notification area's process
                        NativeMethods.TBBUTTON buttoninfo = new NativeMethods.TBBUTTON();
                        NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out buttoninfo, Marshal.SizeOf(buttoninfo), out bytesread);

                        if (bytesread != Marshal.SizeOf(buttoninfo))
                        {
                            return(null);
                        }

                        if (buttoninfo.dwData == IntPtr.Zero)
                        {
                            return(null);
                        }

                        // the dwData field contains a pointer to information about the notify icon:
                        // the handle of the notify icon (an 4/8 bytes) and the id of the notify icon (4 bytes)
                        IntPtr niinfopointer = buttoninfo.dwData;

                        // read the notify icon handle
                        IntPtr nihandlenew;
                        NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer, out nihandlenew, Marshal.SizeOf(typeof(IntPtr)), out bytesread);

                        if (bytesread != Marshal.SizeOf(typeof(IntPtr)))
                        {
                            return(null);
                        }

                        // read the notify icon id
                        uint niidnew;
                        NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer + Marshal.SizeOf(typeof(IntPtr)), out niidnew, Marshal.SizeOf(typeof(uint)), out bytesread);

                        if (bytesread != Marshal.SizeOf(typeof(uint)))
                        {
                            return(null);
                        }

                        // if we've found a match
                        if (nihandlenew == nihandle && niidnew == niid)
                        {
                            // check if the button is hidden: if it is, return the rectangle of the 'show hidden icons' button
                            if ((byte)(buttoninfo.fsState & NativeMethods.TBSTATE_HIDDEN) != 0)
                            {
                                nirect = GetNotifyAreaButtonRectangle();
                            }
                            else
                            {
                                NativeMethods.RECT result = new NativeMethods.RECT();

                                // get the relative rectangle of the toolbar button (notify icon)
                                NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETITEMRECT, new IntPtr(j), toolbarmemoryptr);

                                NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out result, Marshal.SizeOf(result), out bytesread);

                                if (bytesread != Marshal.SizeOf(result))
                                {
                                    return(null);
                                }

                                // find where the rectangle lies in relation to the screen
                                NativeMethods.MapWindowPoints(natoolbarhandle, (IntPtr)null, ref result, 2);

                                nirect = result;
                            }

                            found = true;
                        }
                    }
                }
                finally
                {
                    // free memory within process
                    NativeMethods.VirtualFreeEx(naprocesshandle, toolbarmemoryptr, 0, NativeMethods.FreeType.Release);

                    // close handle to process
                    NativeMethods.CloseHandle(naprocesshandle);
                }
            }

            return(nirect);
        }