Esempio n. 1
0
        private void OnPaintThemed(PaintEventArgs e)
        {
            NativeMethods.RECT rect = new NativeMethods.RECT();

            rect.left   = ClientRectangle.Left;
            rect.top    = ClientRectangle.Top;
            rect.right  = ClientRectangle.Right;
            rect.bottom = ClientRectangle.Bottom;

            IntPtr hdc = new IntPtr();

            hdc = e.Graphics.GetHdc();

            if (BackColor.ToKnownColor() != KnownColor.Window)
            {
                e.Graphics.ReleaseHdc(hdc);

                using (SolidBrush backgroundBrush = new SolidBrush(BackColor))
                {
                    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
                }

                hdc = e.Graphics.GetHdc();

                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Edit");

                NativeMethods.DTBGOPTS options = new NativeMethods.DTBGOPTS();
                options.dwSize  = (uint)System.Runtime.InteropServices.Marshal.SizeOf(options);
                options.dwFlags = NativeMethods.DTBG_OMITCONTENT;

                int state = NativeMethods.ETS_NORMAL;

                if (!Enabled)
                {
                    state = NativeMethods.ETS_DISABLED;
                }
                else
                if (ReadOnly)
                {
                    state = NativeMethods.ETS_READONLY;
                }

                NativeMethods.DrawThemeBackgroundEx(hTheme, hdc,
                                                    NativeMethods.EP_EDITTEXT, state, ref rect, ref options);

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                }
            }
            else if (Enabled & !ReadOnly)
            {
                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Edit");

                NativeMethods.DrawThemeBackground(hTheme, hdc, NativeMethods.EP_EDITTEXT,
                                                  NativeMethods.ETS_NORMAL, ref rect, IntPtr.Zero);

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                }
            }
            else
            {
                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Globals");

                IntPtr hBrush = NativeMethods.GetThemeSysColorBrush(hTheme, 15);

                NativeMethods.FillRect(hdc, ref rect, hBrush);

                if (IntPtr.Zero != hBrush)
                {
                    NativeMethods.DeleteObject(hBrush);
                    hBrush = IntPtr.Zero;
                }

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                    hTheme = IntPtr.Zero;
                }

                hTheme = NativeMethods.OpenThemeData(Handle, "Edit");

                NativeMethods.DTBGOPTS options = new NativeMethods.DTBGOPTS();
                options.dwSize  = (uint)System.Runtime.InteropServices.Marshal.SizeOf(options);
                options.dwFlags = NativeMethods.DTBG_OMITCONTENT;

                NativeMethods.DrawThemeBackgroundEx(hTheme, hdc,
                                                    NativeMethods.EP_EDITTEXT, NativeMethods.ETS_DISABLED, ref rect, ref options);

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                }
            }

            e.Graphics.ReleaseHdc(hdc);
        }
        protected void OnPaintThemed(PaintEventArgs e)
        {
            NativeMethods.RECT rect = new NativeMethods.RECT();

            rect.left   = ClientRectangle.Left;
            rect.top    = ClientRectangle.Top;
            rect.right  = ClientRectangle.Right;
            rect.bottom = ClientRectangle.Bottom;

            IntPtr hdc = new IntPtr();

            hdc = e.Graphics.GetHdc();

            if (BackColor.ToKnownColor() != KnownColor.Window)
            {
                e.Graphics.ReleaseHdc(hdc);

                using (SolidBrush backgroundBrush = new SolidBrush(BackColor))
                {
                    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
                }

                hdc = e.Graphics.GetHdc();
            }
            else
            if (Enabled & !ReadOnly)
            {
                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Edit");

                NativeMethods.DTBGOPTS options = new NativeMethods.DTBGOPTS();
                options.dwSize  = (uint)System.Runtime.InteropServices.Marshal.SizeOf(options);
                options.dwFlags = NativeMethods.DTBG_OMITBORDER;

                NativeMethods.DrawThemeBackgroundEx(hTheme, hdc,
                                                    NativeMethods.EP_EDITTEXT, NativeMethods.ETS_NORMAL, ref rect, ref options);

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                }
            }
            else
            {
                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Globals");

                IntPtr hBrush = NativeMethods.GetThemeSysColorBrush(hTheme, 15);

                NativeMethods.FillRect(hdc, ref rect, hBrush);

                if (IntPtr.Zero != hBrush)
                {
                    NativeMethods.DeleteObject(hBrush);
                    hBrush = IntPtr.Zero;
                }

                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                    hTheme = IntPtr.Zero;
                }
            }

            e.Graphics.ReleaseHdc(hdc);

            uint colorref = 0;

            if (Enabled)
            {
                if (ForeColor.ToKnownColor() != KnownColor.WindowText)
                {
                    colorref = NativeMethods.RGB(ForeColor.R, ForeColor.G, ForeColor.B);
                }
                else
                {
                    IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Globals");
                    colorref = NativeMethods.GetThemeSysColor(hTheme, 6);
                    if (IntPtr.Zero != hTheme)
                    {
                        NativeMethods.CloseThemeData(hTheme);
                        hTheme = IntPtr.Zero;
                    }
                }
            }
            else
            {
                IntPtr hTheme = NativeMethods.OpenThemeData(Handle, "Globals");
                colorref = NativeMethods.GetThemeSysColor(hTheme, 16);
                if (IntPtr.Zero != hTheme)
                {
                    NativeMethods.CloseThemeData(hTheme);
                    hTheme = IntPtr.Zero;
                }
            }

            int r = NativeMethods.GetRValue(colorref);
            int g = NativeMethods.GetGValue(colorref);
            int b = NativeMethods.GetBValue(colorref);

            StringFormat stringFormat = new StringFormat();

            stringFormat.Alignment = StringAlignment.Center;

            using (SolidBrush textBrush = new SolidBrush(Color.FromArgb(r, g, b)))
            {
                e.Graphics.DrawString(Text, Font, textBrush, ClientRectangle, stringFormat);
            }
        }