Beispiel #1
0
        protected void RefreshFakeWnd()
        {
            if (m_bIsRefreshing)
            {
                return;
            }

            if (!NativeMethods.IsWindow(m_FakeWndHandle))
            {
                return;
            }

            m_bIsRefreshing = true;
            POINT         ptSrc    = new POINT(0, 0);
            POINT         ptWinPos = new POINT(this.Left, this.Top);
            SIZE          szWin    = new SIZE(m_BgImg.Width, m_BgImg.Height);
            byte          biAlpha  = 0xFF;
            BLENDFUNCTION stBlend  = new BLENDFUNCTION(BlendOp.AC_SRC_OVER, 0, biAlpha, BlendOp.AC_SRC_ALPHA);

            IntPtr hDC = NativeMethods.GetDC(m_FakeWndHandle);

            if (hDC == IntPtr.Zero)
            {
                m_bIsRefreshing = false;
                Debug.Assert(false, "GetDC failed.");
                return;
            }

            IntPtr hdcMemory = NativeMethods.CreateCompatibleDC(hDC);

            int nBytesPerLine = ((m_BgImg.Width * 32 + 31) & (~31)) >> 3;
            BITMAPINFOHEADER stBmpInfoHeader = new BITMAPINFOHEADER();

            stBmpInfoHeader.Init();
            stBmpInfoHeader.biWidth       = m_BgImg.Width;
            stBmpInfoHeader.biHeight      = m_BgImg.Height;
            stBmpInfoHeader.biPlanes      = 1;
            stBmpInfoHeader.biBitCount    = 32;
            stBmpInfoHeader.biCompression = CompressionType.BI_RGB;
            stBmpInfoHeader.biClrUsed     = 0;
            stBmpInfoHeader.biSizeImage   = (uint)(nBytesPerLine * m_BgImg.Height);

            IntPtr pvBits  = IntPtr.Zero;
            IntPtr hbmpMem = NativeMethods.CreateDIBSection(hDC
                                                            , ref stBmpInfoHeader
                                                            , DIBColorTableIdentifier.DIB_RGB_COLORS
                                                            , out pvBits
                                                            , IntPtr.Zero
                                                            , 0
                                                            );

            Debug.Assert(hbmpMem != IntPtr.Zero, "CreateDIBSection failed.");

            if (hbmpMem != null)
            {
                IntPtr hbmpOld = NativeMethods.SelectObject(hdcMemory, hbmpMem);

                Graphics graphic = Graphics.FromHdcInternal(hdcMemory);

                graphic.DrawImage(m_BgImg, 0, 0, m_BgImg.Width, m_BgImg.Height);

                foreach (Control ctrl in this.Controls)
                {
                    if (!ctrl.Visible)
                    {
                        continue;
                    }

                    using (Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height))
                    {
                        Rectangle rect = new Rectangle(0, 0, ctrl.Width, ctrl.Height);
                        ctrl.DrawToBitmap(bmp, rect);

                        graphic.DrawImage(bmp, ctrl.Left, ctrl.Top, ctrl.Width, ctrl.Height);
                    }
                }

                GUITHREADINFO stGuiThreadInfo = new GUITHREADINFO();
                stGuiThreadInfo.Init();
                if (NativeMethods.GetGUIThreadInfo((uint)NativeMethods.GetCurrentThreadId(), ref stGuiThreadInfo))
                {
                    if (NativeMethods.IsWindow(stGuiThreadInfo.hwndCaret))
                    {
                        int   height  = stGuiThreadInfo.rcCaret.Bottom - stGuiThreadInfo.rcCaret.Top;
                        POINT ptCaret = new POINT(stGuiThreadInfo.rcCaret.Left, stGuiThreadInfo.rcCaret.Top);

                        NativeMethods.ClientToScreen(stGuiThreadInfo.hwndCaret, ref ptCaret);
                        NativeMethods.ScreenToClient(this.Handle, ref ptCaret);

                        graphic.DrawLine(new Pen(new SolidBrush(Color.Black))
                                         , ptCaret.X
                                         , ptCaret.Y
                                         , ptCaret.X
                                         , ptCaret.Y + height
                                         );
                    }
                }


                NativeMethods.UpdateLayeredWindow(m_FakeWndHandle
                                                  , hDC
                                                  , ref ptWinPos
                                                  , ref szWin
                                                  , hdcMemory
                                                  , ref ptSrc
                                                  , 0
                                                  , ref stBlend
                                                  , UpdateLayerWindowParameter.ULW_ALPHA
                                                  );

                graphic.Dispose();
                NativeMethods.SelectObject(hbmpMem, hbmpOld);
                NativeMethods.DeleteObject(hbmpMem);
            }

            NativeMethods.DeleteDC(hdcMemory);
            NativeMethods.DeleteDC(hDC);

            m_bIsRefreshing = false;
        }
        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;
            }
        }