public void DrawText(string text, Point point, Font font, Color foreColor)
        {
            IntPtr fontHandle = font.ToHfont();
            IntPtr oldFontHandle = NativeMethods.SelectObject(this.graphicsHandle, fontHandle);

            int oldBkMode = NativeMethods.SetBkMode(this.graphicsHandle, NativeMethods.TRANSPARENT);
            int oldTextColor = NativeMethods.SetTextColor(this.graphicsHandle, Color.FromArgb(0, foreColor.R, foreColor.G, foreColor.B).ToArgb());

            Size size = this.MeassureTextInternal(text);

            NativeMethods.RECT clip = new NativeMethods.RECT();
            clip.left = point.X;
            clip.top = point.Y;
            clip.right = clip.left + size.Width;
            clip.bottom = clip.top + size.Height;

            // ExtTextOut does not show Mnemonics highlighting.
            NativeMethods.DrawText(this.graphicsHandle, text, text.Length, ref clip, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT);

            NativeMethods.SetTextColor(this.graphicsHandle, oldTextColor);
            NativeMethods.SetBkMode(this.graphicsHandle, oldBkMode);

            NativeMethods.SelectObject(this.graphicsHandle, oldFontHandle);
            NativeMethods.DeleteObject(fontHandle);
        }
        private Size MeassureTextInternal(string text)
        {
            NativeMethods.RECT rect = new NativeMethods.RECT();
            rect.left = 0;
            rect.right = 0;
            rect.top = 0;
            rect.bottom = 0;

            // ExtTextOut does not show Mnemonics highlighting.
            NativeMethods.DrawText(this.graphicsHandle, text, text.Length, ref rect, NativeMethods.DT_SINGLELINE | NativeMethods.DT_LEFT | NativeMethods.DT_CALCRECT);

            return new Size(rect.right, rect.bottom);
        }
Exemple #3
0
        private void TrackDropDown(int index)
        {
            while (index >= 0)
            {
                this.trackNextItem = -1;

                this.BeginUpdate();

                CommandBarMenu menu = this.items[index] as CommandBarMenu;
                if (menu != null)
                {
                    menu.PerformDropDown(EventArgs.Empty);
                    this.contextMenu.Items.Clear();
                    this.contextMenu.Items.AddRange(menu.Items);                     // = menu.Items;
                    this.contextMenu.Mnemonics = true;
                }
                else
                {
                    this.contextMenu.Items.Clear();                     // .Items = new CommandBarItemCollection();
                    this.contextMenu.Mnemonics = true;
                }

                // Item state
                NativeMethods.SendMessage(this.Handle, NativeMethods.TB_PRESSBUTTON, index, -1);

                // Trick to get the first menu item selected
                NativeMethods.PostMessage(this.Handle, NativeMethods.WM_KEYDOWN, (int)Keys.Down, 1);
                NativeMethods.PostMessage(this.Handle, NativeMethods.WM_KEYUP, (int)Keys.Down, 1);

                this.SetState(State.HotTracking, index);

                // Hook
                NativeMethods.HookProc hookProc = new NativeMethods.HookProc(DropDownHook);
                GCHandle hookProcHandle         = GCHandle.Alloc(hookProc);
                this.hookHandle = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MSGFILTER, hookProc, IntPtr.Zero, NativeMethods.GetCurrentThreadId());
                if (this.hookHandle == IntPtr.Zero)
                {
                    throw new SecurityException();
                }

                // Ask for position
                NativeMethods.RECT rect = new NativeMethods.RECT();
                NativeMethods.SendMessage(Handle, NativeMethods.TB_GETRECT, index, ref rect);
                Point position = new Point(rect.left, rect.bottom);

                this.EndUpdate();
                this.Update();

                this.contextMenu.Show(this, position);

                // Unhook
                NativeMethods.UnhookWindowsHookEx(hookHandle);
                hookProcHandle.Free();
                this.hookHandle = IntPtr.Zero;

                // Item state
                NativeMethods.SendMessage(Handle, NativeMethods.TB_PRESSBUTTON, index, 0);
                this.SetState(trackEscapePressed ? State.Hot : State.None, index);

                index = trackNextItem;
            }
        }