Esempio n. 1
0
        public static void DrawTextOnGlass(IntPtr hwnd, String text, Font font, Rectangle ctlrct, int iglowSize)
        {
            if (IsCompositionEnabled())
            {
                RECT rc  = new RECT();
                RECT rc2 = new RECT();

                rc.left   = ctlrct.Left;
                rc.right  = ctlrct.Right;  // + 2 * iglowSize;  //make it larger to contain the glow effect
                rc.top    = ctlrct.Top;
                rc.bottom = ctlrct.Bottom; // + 2 * iglowSize;

                //Just the same rect with rc,but (0,0) at the lefttop
                rc2.left   = 0;
                rc2.top    = 0;
                rc2.right  = rc.right - rc.left;
                rc2.bottom = rc.bottom - rc.top;

                IntPtr destdc = WinAPI.GetDC(hwnd);                //hwnd must be the handle of form,not control
                IntPtr Memdc  = WinAPI.CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text.
                IntPtr bitmap;
                IntPtr bitmapOld = IntPtr.Zero;
                IntPtr logfnotOld;

                int uFormat = Constants.DT_SINGLELINE | Constants.DT_CENTER | Constants.DT_VCENTER | Constants.DT_NOPREFIX;   //text format

                IntPtr ptPixels = IntPtr.Zero;

                BITMAPINFO dib = new BITMAPINFO();
                dib.bmiHeader.biHeight      = -(rc.bottom - rc.top);    // negative because DrawThemeTextEx() uses a top-down DIB
                dib.bmiHeader.biWidth       = rc.right - rc.left;
                dib.bmiHeader.biPlanes      = 1;
                dib.bmiHeader.biSize        = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
                dib.bmiHeader.biBitCount    = 32;
                dib.bmiHeader.biCompression = Constants.BI_RGB;
                dib.bmiColors.rgbBlue       = 255;
                if (!(WinAPI.SaveDC(Memdc) == 0))
                {
                    bitmap = WinAPI.CreateDIBSection(Memdc, ref dib, Constants.DIB_RGB_COLORS, out ptPixels, IntPtr.Zero, 0);   // Create a 32-bit bmp for use in offscreen drawing when glass is on
                    if (!(bitmap == IntPtr.Zero))
                    {
                        bitmapOld = WinAPI.SelectObject(Memdc, bitmap);
                        IntPtr hFont = font.ToHfont();
                        logfnotOld = WinAPI.SelectObject(Memdc, hFont);
                        try
                        {
                            System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);

                            DTTOPTS dttOpts = new DTTOPTS();

                            dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS));

                            dttOpts.dwFlags = Constants.DTT_COMPOSITED | Constants.DTT_GLOWSIZE;


                            dttOpts.iGlowSize = iglowSize;

                            for (int i = (-(dib.bmiHeader.biWidth * dib.bmiHeader.biHeight) - 1); i >= 0; i--)
                            {
                                Marshal.WriteInt32(
                                    (IntPtr)((long)ptPixels + Marshal.SizeOf(typeof(int)) * i),
                                    GetAeroBackgroundColor().ToArgb()
                                    );
                            }

                            WinAPI.DrawThemeTextEx(renderer.Handle, Memdc, 1, 1, text, -1, uFormat, ref rc2, ref dttOpts);

                            WinAPI.BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, Constants.SRCCOPY);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Trace.WriteLine(e.Message);
                        }


                        //Remember to clean up
                        WinAPI.SelectObject(Memdc, bitmapOld);
                        WinAPI.SelectObject(Memdc, logfnotOld);
                        WinAPI.DeleteObject(bitmap);
                        WinAPI.DeleteObject(hFont);

                        WinAPI.ReleaseDC(Memdc, -1);
                        WinAPI.DeleteDC(Memdc);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        public int ShowDialog(IntPtr Parent)
        {
            taskDialogConfig.hwndParent = Parent;

            taskDialogConfig.dwFlags = 0;


            if (EnableHyperLinks)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_ENABLE_HYPERLINKS;
            }

            if (_UseCustomIcon)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_USE_HICON_MAIN;
            }

            if (_UseCustomFooterIcon)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_USE_HICON_FOOTER;
            }

            if (ControlBox)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_ALLOW_DIALOG_CANCELLATION;
            }

            if (UseCommandLinks)
            {
                if (RemoveCommandLinksIcons)
                {
                    taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS_NO_ICON;
                }
                else
                {
                    taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS;
                }
            }

            if (ExpandFooterArea)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_EXPAND_FOOTER_AREA;
            }

            if (ExpandFooterAreaByDefault)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_EXPANDED_BY_DEFAULT;
            }

            if (_CheckBoxChecked)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_VERIFICATION_FLAG_CHECKED;
            }

            if (ShowProgressBar)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_SHOW_PROGRESS_BAR;
            }

            if (_MarqueeProgressBar)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_SHOW_MARQUEE_PROGRESS_BAR;
            }

            if (EnableCallbackTimer)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_CALLBACK_TIMER;
            }

            if (PositionRelativeToParent)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_POSITION_RELATIVE_TO_WINDOW;
            }

            if (RTL)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_RTL_LAYOUT;
            }

            if (NoDefaultRadioButton)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_NO_DEFAULT_RADIO_BUTTON;
            }

            if (MinimizeBox)
            {
                taskDialogConfig.dwFlags |= TASKDIALOG_FLAGS.TDF_CAN_BE_MINIMIZED;
            }



            int size = Marshal.SizeOf(typeof(TASKDIALOG_BUTTON));

            taskDialogConfig.cButtons = Buttons.Count;
            taskDialogConfig.pButtons = Marshal.AllocHGlobal(Buttons.Count * size);
            IEnumerator ie     = Buttons.GetEnumerator();
            int         offset = 0;

            while (ie.MoveNext())
            {
                Marshal.StructureToPtr(((TaskDialogButton)ie.Current).Config, (IntPtr)((long)taskDialogConfig.pButtons + offset), false);
                offset += size;
            }

            taskDialogConfig.cRadioButtons = RadioButtons.Count;
            taskDialogConfig.pRadioButtons = Marshal.AllocHGlobal(RadioButtons.Count * size);
            ie     = RadioButtons.GetEnumerator();
            offset = 0;
            while (ie.MoveNext())
            {
                Marshal.StructureToPtr(((TaskDialogRadioButton)ie.Current).Config, (IntPtr)((long)taskDialogConfig.pRadioButtons + offset), false);
                offset += size;
            }


            int nButtonPressed = 0;
            int pnRadioButton  = 0;

            int ret = WinAPI.TaskDialogIndirect(ref taskDialogConfig, ref nButtonPressed, ref pnRadioButton, ref _CheckBoxChecked);

            taskDialogConfig.nDefaultRadioButton = pnRadioButton;

            Marshal.FreeHGlobal(taskDialogConfig.pButtons);
            Marshal.FreeHGlobal(taskDialogConfig.pRadioButtons);

            return(nButtonPressed);
        }