On Mon, 21 Mar 2005 22:19:06 -0500, "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote:
Can you post the code for your UserControl?
well, this is the whole thing. I took the control from the colorpicker control on the MSDN website. I have changed it
very little so far. The original came with a form demo. I just moved the logic for creating the gradient into the
constructor from the form, added the Toolbox statements and moved the Event handler into the main module.
The control is a Photoshop style color slider taken from :
http://msdn.microsoft.com/library/de...olorpicker.asp
The color slider was just one of the controls inside the colorpicker control. It is basically a rectangle with a
gradient fill and 2 triangles on either side of it that shows the color level. Of red at the moment.
Thanks
using System;
using System.Data;
using System.Collecti ons.Specialized ;
using System.Diagnost ics;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Windows. Forms;
namespace NewControls
{
public delegate void ValueChangedHan dler(object sender, ValueChangedEve ntArgs e);
public class ValueChangedEve ntArgs : EventArgs
{
private int m_value;
public int Value
{
get { return m_value; }
}
public ValueChangedEve ntArgs( int newValue )
{
m_value = newValue;
}
}
[ToolboxItem(tru e)]
[ToolboxBitmap(t ypeof(ColorSlid er),@"D:\Develo pment\NewContro ls\ColorSlider. bmp")]
public class ColorSlider : System.Windows. Forms.UserContr ol
{
public event ValueChangedHan dler ValueChanged;
// private data members
private bool m_isLeftMouseBu ttonDown;
private bool m_isLeftMouseBu ttonDownAndMovi ng;
private int m_currentArrowY Location;
private Rectangle m_leftArrowRegi on;
private Rectangle m_rightArrowReg ion;
// readonly
private readonly Rectangle m_colorRegion = new Rectangle( 13, 7, 18, 256 );
private readonly Rectangle m_outerRegion = new Rectangle( 10, 4, 26, 264 );
private readonly Bitmap m_colorBitmap = new Bitmap( 18, 256 );
// constants
private const int POINTER_HEIGHT = 10;
private const int POINTER_WIDTH = 6;
/// <summary>
/// Constructor.
/// </summary>
public ColorSlider() : base()
{
m_currentArrowY Location = m_colorRegion.T op;
using ( Graphics g = Graphics.FromIm age(ColorBitmap ) )
{
Color startColor = Color.FromArgb( 0, 0, 0 );
Color endColor = Color.FromArgb( 255, 0, 0 );
Rectangle region = new Rectangle( 0, 0, 18, 300 );
using (LinearGradient Brush lgb = new LinearGradientB rush(region,
startColor,
endColor,
270f))
{
g.FillRectangle ( lgb, region );
}
}
} /* End of ColorSlider() */
/// <summary>
/// Gets or sets the color slider bitmap.
/// </summary>
public Bitmap ColorBitmap
{
get { return m_colorBitmap; }
} /* End of ColorBitmap */
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint (e);
using ( Graphics g = e.Graphics )
{
CreateLeftTrian glePointer( g, m_currentArrowY Location );
CreateRightTria nglePointer( g, m_currentArrowY Location );
if ( m_colorBitmap != null )
{
g.DrawImage( m_colorBitmap, m_colorRegion );
}
ControlPaint.Dr awBorder3D( g, m_outerRegion );
g.DrawRectangle ( Pens.Black, m_colorRegion );
}
} /* End of OnPaint(PaintEv entArgs) */
protected override void OnMouseDown(Mou seEventArgs e)
{
if ( e.Button == MouseButtons.Le ft )
{
m_isLeftMouseBu ttonDown = true;
CheckCursorYReg ion( e.Y );
}
base.OnMouseDow n (e);
} /* End of OnMouseDown(Mou seEventArgs) */
protected override void OnMouseMove(Mou seEventArgs e)
{
if ( m_isLeftMouseBu ttonDown )
{
m_isLeftMouseBu ttonDownAndMovi ng = true;
CheckCursorYReg ion( e.Y );
}
base.OnMouseMov e (e);
} /* End of OnMouseMove(Mou seEventArgs) */
protected override void OnMouseUp(Mouse EventArgs e)
{
m_isLeftMouseBu ttonDown = false;
m_isLeftMouseBu ttonDownAndMovi ng = false;
base.OnMouseUp (e);
} /* End of OnMouseUp(Mouse EventArgs) */
/// <summary>
/// Calculates the points needed to draw the left triangle pointer for
/// the value strip.
/// </summary>
/// <param name="g">Graphi cs object.</param>
/// <param name="y">Curren t cursor y-value.</param>
private void CreateLeftTrian glePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRe gion.Left - POINTER_WIDTH - 1, y - (POINTER_HEIGHT / 2)),
new Point(m_outerRe gion.Left - 2, y),
new Point(m_outerRe gion.Left - POINTER_WIDTH - 1,
y + (POINTER_HEIGHT / 2))};
g.DrawPolygon( Pens.Black, points );
}
/// <summary>
/// Calculates the points needed to draw the right triangle pointer for
/// the color slider.
/// </summary>
/// <param name="g">Graphi cs object.</param>
/// <param name="y">Curren t cursor y-value.</param>
private void CreateRightTria nglePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRe gion.Right - 1 + POINTER_WIDTH, y - (POINTER_HEIGHT / 2)),
new Point(m_outerRe gion.Right, y),
new Point(m_outerRe gion.Right - 1 + POINTER_WIDTH,
y + (POINTER_HEIGHT/2))};
g.DrawPolygon( Pens.Black, points );
}
/// <summary>
/// Determines the color slider left triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">C urrent cursor y-value.</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>
private Rectangle GetLeftTriangle PointerInvalida tionRegion(int arrowY)
{
int leftPadding = POINTER_WIDTH + 2;
int x = m_outerRegion.L eft - leftPadding;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;
return new Rectangle(x, y, width, height);
} /* End of GetLeftTriangle PointerInvalida tionRegion(int) */
/// <summary>
/// Determines the color slider right triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">C urrent cursor y-value</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>
private Rectangle GetRightTriangl ePointerInvalid ationRegion( int arrowY )
{
int x = m_outerRegion.R ight;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;
return new Rectangle( x, y, width, height );
} /* End of GetRightTriangl ePointerInvalid ationRegion(int ) */
private void CheckCursorYReg ion( int y )
{
int mValue = y;
if ( m_isLeftMouseBu ttonDown && !m_isLeftMouseB uttonDownAndMov ing )
{
if ( y < m_colorRegion.T op || y > m_colorRegion.B ottom )
return;
}
else if ( m_isLeftMouseBu ttonDownAndMovi ng )
{
if ( y < m_colorRegion.T op )
mValue = m_colorRegion.T op;
else if ( y >= m_colorRegion.B ottom )
mValue = m_colorRegion.B ottom - 1;
}
else
return;
m_currentArrowY Location = mValue;
InvalidateArrow Regions( mValue );
if ( ValueChanged != null )
ValueChanged(th is, new ValueChangedEve ntArgs(255 - (mValue - m_colorRegion.T op)));
} /* End of CheckCursorYReg ion(int) */
private void InvalidateArrow Regions(int y)
{
this.Invalidate ( m_leftArrowRegi on );
this.Invalidate ( m_rightArrowReg ion );
m_leftArrowRegi on = this.GetLeftTri anglePointerInv alidationRegion ( y );
m_rightArrowReg ion = this.GetRightTr ianglePointerIn validationRegio n( y );
this.Invalidate (m_leftArrowReg ion);
this.Invalidate (m_rightArrowRe gion);
} /* End of InvalidateArrow Regions(int) */
} /* End of public class ColorSlider */
} /* End of namespace NewControls