public static bool GradientFill(this Graphics gr, Rectangle rc, Color startColor, Color endColor, FillDirection fillDir) { if (DesignMode.IsTrue) { using (var pen = new Pen(startColor)) { if (fillDir == FillDirection.LeftToRight) { for (var x = rc.Left; x < rc.Right; x++) { pen.Color = ColorEx.InterpolateLinear(startColor, endColor, x, 0, rc.Width); gr.DrawLine(pen, x, rc.Top, x, rc.Bottom - 1); } } if (fillDir == FillDirection.TopToBottom) { for (var y = rc.Top; y < rc.Bottom; y++) { pen.Color = ColorEx.InterpolateLinear(startColor, endColor, y, 0, rc.Height); gr.DrawLine(pen, rc.Left, y, rc.Right - 1, y); } } } return(false); } var tva = new TRIVERTEX[2]; tva[0] = new TRIVERTEX(rc.X, rc.Y, startColor); tva[1] = new TRIVERTEX(rc.Right, rc.Bottom, endColor); var gra = new[] { new GRADIENT_RECT(0, 1) }; var hdc = gr.GetHdc(); var ret = NativeMethods.GradientFill(hdc, tva, (uint)tva.Length, gra, (uint)gra.Length, (uint)fillDir); gr.ReleaseHdc(hdc); return(ret); }
internal static extern bool GradientFill(IntPtr hdc, TRIVERTEX[] pVertex, uint dwNumVertex, GRADIENT_RECT[] pMesh, uint dwNumMesh, uint dwMode);
public static bool GradientFill(this Graphics gr, Rectangle rc, Color startColor, Color endColor, FillDirection fillDir) { if (DesignMode.IsTrue) { using (var pen = new Pen(startColor)) { if (fillDir == FillDirection.LeftToRight) { for (var x = rc.Left; x < rc.Right; x++) { pen.Color = ColorEx.InterpolateLinear(startColor, endColor, x, 0, rc.Width); gr.DrawLine(pen, x, rc.Top, x, rc.Bottom - 1); } } if (fillDir == FillDirection.TopToBottom) { for (var y = rc.Top; y < rc.Bottom; y++) { pen.Color = ColorEx.InterpolateLinear(startColor, endColor, y, 0, rc.Height); gr.DrawLine(pen, rc.Left, y, rc.Right - 1, y); } } } return false; } var tva = new TRIVERTEX[2]; tva[0] = new TRIVERTEX(rc.X, rc.Y, startColor); tva[1] = new TRIVERTEX(rc.Right, rc.Bottom, endColor); var gra = new[] { new GRADIENT_RECT(0, 1) }; var hdc = gr.GetHdc(); var ret = NativeMethods.GradientFill(hdc, tva, (uint)tva.Length, gra, (uint)gra.Length, (uint)fillDir); gr.ReleaseHdc(hdc); return ret; }