创建一个背景色渐变的WINFORM

你是否厌倦了灰灰的WINFORM呢,今天介绍一个为WINFORM填充渐变颜色的方法。参考了以下两篇文章

http://breathingtech.com/2009/creating-gradient-background-with-transparent-labels-in-net-compact-framework/

http://msdn.microsoft.com/en-us/library/ms229655(VS.80).aspx


新的WINFORM要继承Form,然后重写OnPaintBackground()方法

using System.Drawing;
using System.Windows.Forms;
 
namespace GradientTransparentTest
{
    public partial class GradientTransparentForm : Form
    {
        public GradientTransparentForm()
        {
            InitializeComponent();
        }
 
        // Paints the background of the form with a GradientFill pattern.
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            GradientFill.Fill(
            e.Graphics, ClientRectangle,
            Color.Silver, Color.LightBlue,
            GradientFill.FillDirection.LeftToRight);
            e.Graphics.Dispose();
        }
    }
}

OnPaintBackground中所用到的GradientFill.Fill是MSDN中提供的方法。


public sealed class GradientFill
{
    // This method wraps the PInvoke to GradientFill.
    // Parmeters:
    //  gr - The Graphics object we are filling
    //  rc - The rectangle to fill
    //  startColor - The starting color for the fill
    //  endColor - The ending color for the fill
    //  fillDir - The direction to fill
    //
    // Returns true if the call to GradientFill succeeded; false
    // otherwise.
    public static bool Fill(
        Graphics gr,
        Rectangle rc,
        Color startColor, Color endColor,
        FillDirection fillDir)
    {

        // Initialize the data to be used in the call to GradientFill.
        Win32Helper.TRIVERTEX[] tva = new Win32Helper.TRIVERTEX[2];
        tva[0] = new Win32Helper.TRIVERTEX(rc.X, rc.Y, startColor);
        tva[1] = new Win32Helper.TRIVERTEX(rc.Right, rc.Bottom, endColor);
        Win32Helper.GRADIENT_RECT[] gra = new Win32Helper.GRADIENT_RECT[] {
    new Win32Helper.GRADIENT_RECT(0, 1)};

        // Get the hDC from the Graphics object.
        IntPtr hdc = gr.GetHdc();

        // PInvoke to GradientFill.
        bool b;

        b = Win32Helper.GradientFill(
                hdc,
                tva,
                (uint)tva.Length,
                gra,
                (uint)gra.Length,
                (uint)fillDir);
        System.Diagnostics.Debug.Assert(b, string.Format(
            "GradientFill failed: {0}",
            System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

        // Release the hDC from the Graphics object.
        gr.ReleaseHdc(hdc);

        return b;
    }

    // The direction to the GradientFill will follow
    public enum FillDirection
    {
        //
        // The fill goes horizontally
        //
        LeftToRight = Win32Helper.GRADIENT_FILL_RECT_H,
        //
        // The fill goes vertically
        //
        TopToBottom = Win32Helper.GRADIENT_FILL_RECT_V
    }
}

接下来便是Win32Helper Calss加入你的项目中

public sealed class Win32Helper
{
    public struct TRIVERTEX
    {
        public int x;
        public int y;
        public ushort Red;
        public ushort Green;
        public ushort Blue;
        public ushort Alpha;
        public TRIVERTEX(int x, int y, Color color)
            : this(x, y, color.R, color.G, color.B, color.A)
        {
        }
        public TRIVERTEX(
            int x, int y,
            ushort red, ushort green, ushort blue,
            ushort alpha)
        {
            this.x = x;
            this.y = y;
            this.Red = (ushort)(red << 8);
            this.Green = (ushort)(green << 8);
            this.Blue = (ushort)(blue << 8);
            this.Alpha = (ushort)(alpha << 8);
        }
    }
    public struct GRADIENT_RECT
    {
        public uint UpperLeft;
        public uint LowerRight;
        public GRADIENT_RECT(uint ul, uint lr)
        {
            this.UpperLeft = ul;
            this.LowerRight = lr;
        }
    }


    [DllImport("msimg32dll.dll", SetLastError = true, EntryPoint = "GradientFill")]
    public extern static bool GradientFill(
        IntPtr hdc,
        TRIVERTEX[] pVertex,
        uint dwNumVertex,
        GRADIENT_RECT[] pMesh,
        uint dwNumMesh,
        uint dwMode);

    public const int GRADIENT_FILL_RECT_H = 0x00000000;
    public const int GRADIENT_FILL_RECT_V = 0x00000001;

}
这样一个漂亮的窗体就出来了。

你可能感兴趣的:(技术文章,C#)