Search

Thursday, February 11, 2010

Draw LinearGradientBrush Background in C# windows mobile .netcf

wanted to draw gradient background on windows form in .net compact framework but not able to find it in the system.Drawing2d this is the most common problem developers face when they first code on .netframework this is because .netcompact framework does not support LinearGradientBrush class recently i too faced the same problem as i wanted to paint double shade linear gradient color as the background to the form but after a small R&D i came to know that there is GradientFill API in windows CE that can be used to get accomplish the task.

i found the sample code in msdn library which liked like a charm now you can display nice gradient fill graphics by using below code as the background. with this sample code you will be able to fill the gradient in two different styles leftoright and toptobottom as shown in below Pic left picture shows leftright pattern and right picture shows topBottom pattern. below is the sample source code which to get the above gradient effect







public void 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);


}


public enum FillDirection
{
//
// The fill goes horizontally
//
LeftToRight = Win32Helper.GRADIENT_FILL_RECT_H,
//
// The fill goes vertically
//
TopToBottom = Win32Helper.GRADIENT_FILL_RECT_V
}



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("coredll.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;

}


Now just copy the above code and paste it in appropriate class file and location it should definitely work

0 comments:

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google