Ejemplo n.º 1
0
        public void GradientFillRectangle(Rect theRect, System.Drawing.Color Color1, System.Drawing.Color Color2, System.Drawing.Drawing2D.LinearGradientMode GradientType)
        {
            int ColorR1 = Color1.R;
            int ColorG1 = Color1.G;
            int ColorB1 = Color1.B;

            int ColorR2 = Color2.R - ColorR1;
            int ColorG2 = Color2.G - ColorG1;
            int ColorB2 = Color2.B - ColorB1;

            int Width = theRect.Size.X;
            int Height = theRect.Size.Y;
            int x0 = theRect.Location.X;
            int y0 = theRect.Location.Y;
            int x1 = theRect.Right;
            int y1 = theRect.Bottom;

            if (Width <= 0 || Height <= 0)
                return;

            double Coeff;
            int StepSize;

            API.RECT R = new API.RECT();

            const int NumberOfSteps = 128; // number of steps

            if (GradientType == System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
            {
                double InvWidth = 1.0 / Width;

                StepSize = Width / NumberOfSteps;
                if (StepSize < 1)
                    StepSize = 1;

                R.Top = y0;
                R.Bottom = y1;

                for (int i = x0; i <= x1; i += StepSize)
                {
                    R.Left = i;
                    R.Right = i + StepSize;
                    if (R.Right > x1)
                    {
                        R.Right = x1;
                    }

                    Coeff = (i - x0) * InvWidth;

                    IntPtr hBrush = API.CreateSolidBrush((int)
                        (int)(ColorR1 + (double)ColorR2 * Coeff) |
                        (int)(ColorG1 + (double)ColorG2 * Coeff) << 8 |
                        (int)(ColorB1 + (double)ColorB2 * Coeff) << 16
                        );

                    API.FillRect(hDC, ref R, hBrush);
                    API.DeleteObject(hBrush);
                }
            }
            else
            {
                double InvHeight = 1.0 / Height;

                StepSize = Height / NumberOfSteps;

                if (StepSize < 1)
                    StepSize = 1;

                R.Left = x0;
                R.Right = x1;

                for (int i = y0; i <= y1; i += StepSize)
                {
                    R.Top = i;
                    R.Bottom = i + StepSize;
                    if (R.Bottom > y1)
                    {

                        R.Bottom = y1;

                    }

                    Coeff = (i - y0) * InvHeight;
                    IntPtr hBrush = API.CreateSolidBrush(
                        (int)(ColorR1 + (double)ColorR2 * Coeff) |
                        (int)(ColorG1 + (double)ColorG2 * Coeff) << 8 |
                        (int)(ColorB1 + (double)ColorB2 * Coeff) << 16
                    );

                    API.FillRect(hDC, ref R, hBrush);
                    API.DeleteObject(hBrush);
                }
            }
        }
Ejemplo n.º 2
0
        public void DrawString(string Text, Rect theRect, IFontStyleInfo theFont)
        {
            GDIFontStyle FontStyle = (GDIFontStyle)theFont;

            if (CurrentTextColor != FontStyle.Win32ForeColor)
            {
                CurrentTextColor = FontStyle.Win32ForeColor;
                API.SetTextColor(hDC, FontStyle.Win32ForeColor);
            }

            IntPtr hOldFont = API.SelectObject(hDC, ((GDIFont)FontStyle.Font).hFont);

            API.RECT r = new API.RECT();

            r.Left = theRect.Location.X;
            r.Top = theRect.Location.Y;
            r.Right = theRect.Right;
            r.Bottom = theRect.Bottom;

            // API.DrawText(hDC, Text, Text.Length, ref r, 2368);

            API.ExtTextOut(hDC, r.Left, r.Top, 4, ref r, Text, (uint)Text.Length, null);

            API.SelectObject(hDC, hOldFont);

            // No need to Delete hFont because we're going to reuse it
            // it is being saved in GDIFontStyle FontStyle
            // API.DeleteObject(hFont);

            // No need to restore old text color
            // because we're setting it new each time anyway
            // API.SetTextColor(hDC, hOldColor);
        }