in my project I need to render textures on UserControl objects. The only problem is, that the textures are not rendered. Instead a white box with red borders and a red cross on it is rendered (see image).
[attachment=6063:UserControl.png]
What can cause this problem?
Here's my code:
/// <summary>
/// Abstract base class for all DirectX controls.
/// </summary>
public abstract partial class ControlBase : UserControl
{
/// <summary>
/// Blending action of the control.
/// </summary>
protected enum AlphaBlendAction
{
None,
BlendIn,
BlendOut
};
/// <summary>
/// Graphical device instance.
/// </summary>
protected Device Device;
/// <summary>
/// Sprite used to render textures.
/// </summary>
protected Sprite Sprite;
/// <summary>
/// Alpha value of the control.
/// </summary>
protected int iAlpha;
/// <summary>
/// Indicates if alpha blending should be used.
/// </summary>
protected bool bUseBlendingEffect;
/// <summary>
/// Blending action.
/// </summary>
protected AlphaBlendAction eBlendAction;
/// <summary>
/// Minimal alpha value of control's background texture.
/// </summary>
public int MinAlpha
{
get;
set;
}
/// <summary>
/// Maximal alpha value of control's background texture.
/// </summary>
public int MaxAlpha
{
get;
set;
}
/// <summary>
/// Indicates if blending should be used.
/// </summary>
public bool UseBlendingEffect
{
get
{
return this.bUseBlendingEffect;
}
set
{
this.bUseBlendingEffect = value;
if ( this.bUseBlendingEffect )
{
this.iAlpha = this.MinAlpha;
}
else
{
this.iAlpha = this.MaxAlpha;
}
}
}
/// <summary>
/// Creates a new instance of ControlBase.
/// </summary>
public ControlBase()
: base()
{
this.MinAlpha = 150;
this.MaxAlpha = 255;
this.UseBlendingEffect = false;
this.eBlendAction = AlphaBlendAction.None;
this.MouseHover += new EventHandler( OnMouseHover );
this.MouseLeave += new EventHandler( OnMouseLeave );
InitializeGraphicDevice();
}
/// <summary>
/// Initializes the graphical device.
/// </summary>
protected virtual void InitializeGraphicDevice()
{
PresentParameters PresentParams = new PresentParameters();
PresentParams.SwapEffect = SwapEffect.Discard;
PresentParams.DeviceWindowHandle = this.Handle;
PresentParams.Windowed = true;
PresentParams.BackBufferWidth = this.ClientSize.Width;
PresentParams.BackBufferHeight = this.ClientSize.Height;
PresentParams.BackBufferFormat = Format.A8R8G8B8;
try
{
this.Device = new Device( new Direct3D(), 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, PresentParams );
}
catch ( Exception e )
{
MessageBox.Show( e.Message );
}
this.Device.SetRenderState( RenderState.AlphaBlendEnable, true );
this.Device.SetRenderState( RenderState.Lighting, false );
this.Device.SetRenderState( RenderState.SourceBlend, Blend.One );
this.Device.SetRenderState( RenderState.DestinationBlend, Blend.One );
// Create sprite
this.Sprite = new Sprite( this.Device );
}
/// <summary>
/// Update method of the control. Can be overridden by derived classes.
/// </summary>
new public virtual void Update()
{
if ( AlphaBlendAction.BlendIn == this.eBlendAction )
{
if ( this.MaxAlpha - this.iAlpha >= 5 )
{
this.iAlpha += 5;
}
else
{
this.iAlpha = this.MaxAlpha;
this.eBlendAction = AlphaBlendAction.None;
}
}
else if ( AlphaBlendAction.BlendOut == this.eBlendAction )
{
if ( this.iAlpha - this.MinAlpha >= 5 )
{
this.iAlpha -= 5;
}
else
{
this.iAlpha = this.MinAlpha;
this.eBlendAction = AlphaBlendAction.None;
}
}
}
/// <summary>
/// Render method of the control. Must be overridden by derived classes.
/// </summary>
public abstract void Render();
/// <summary>
/// Releases all unmanaged Resources of the object.
/// </summary>
new public virtual void Dispose()
{
this.Device.Direct3D.Dispose();
this.Device.Dispose();
this.Sprite.Dispose();
}
/// <summary>
/// Eventhandler for event MouseHover. Starts the blending in if blending is activated.
/// </summary>
/// <param name="sender">Sender of event.</param>
/// <param name="e">Event argument.</param>
protected void OnMouseHover( object sender, EventArgs e )
{
if ( this.UseBlendingEffect )
{
this.eBlendAction = AlphaBlendAction.BlendIn;
}
}
/// <summary>
/// Eventhandler for event MouseLeave. Starts the blending out if blending is activated.
/// </summary>
/// <param name="sender">Sender of event.</param>
/// <param name="e">Event argument.</param>
protected void OnMouseLeave( object sender, EventArgs e )
{
if ( this.UseBlendingEffect )
{
this.eBlendAction = AlphaBlendAction.BlendOut;
}
}
/// <summary>
/// Called when control must be redrawn.
/// </summary>
/// <param name="e">Event argument.</param>
protected override void OnPaint( PaintEventArgs e )
{
this.Device.Clear( ClearFlags.Target, new Color4( Color.Black ), 1f, 0 );
if ( this.Visible )
{
this.Device.BeginScene();
this.Render();
this.Device.EndScene();
}
this.Device.Present();
}
/// <summary>
/// Does nothing. Overridden to prevent flickering.
/// </summary>
/// <param name="e">Event argument.</param>
protected override void OnPaintBackground( PaintEventArgs e )
{
// Do nothing to prevent flickering.
}
/// <summary>
/// Called when control has been resized.
/// </summary>
/// <param name="e">Event argument.</param>
protected override void OnSizeChanged( EventArgs e )
{
InitializeGraphicDevice();
}
}
/// <summary>
/// Textured button control using DirectX.
/// </summary>
public class TextureButton : ControlBase
{
/// <summary>
/// Bitmap of the button.
/// </summary>
//private Bitmap Bitmap;
/// <summary>
/// Texture of the button.
/// </summary>
private TextureItem TextureItem;
/// <summary>
/// Creates a new TextureButton instance.
/// </summary>
/// <param name="Bitmap">Bitmap of the button's texture.</param>
//public TextureButton( Bitmap Bitmap )
public TextureButton(string Filename)
: base()
{
this.TextureItem = new TextureItem( this.Device, Filename );
}
/// <summary>
/// Renders the TextureButton instance.
/// </summary>
public override void Render()
{
// Calculate scaling factor for texture.
Matrix Scaling = Matrix.Scaling( this.Width / (float)this.TextureItem.Width,
this.Height / (float)this.TextureItem.Height,
0f );
this.Sprite.Begin( SpriteFlags.AlphaBlend );
this.Sprite.Transform = Scaling;
this.Sprite.Draw( TextureItem.Texture, Rectangle.Empty, Vector3.Zero, Vector3.Zero, Color.FromArgb( iAlpha, 255, 255, 255 ) );
this.Sprite.End();
}
/// <summary>
/// Releases all unmanaged Resources of the object.
/// </summary>
public override void Dispose()
{
//this.TextureItem.Dispose();
base.Dispose();
}
}
/// <summary>
/// Wrapper class around Texture with additional information.
/// </summary>
public class TextureItem
{
/// <summary>
/// Heigth of the texture.
/// </summary>
public int Height
{
get;
private set;
}
/// <summary>
/// Width of the texture.
/// </summary>
public int Width
{
get;
private set;
}
/// <summary>
/// Texture instance.
/// </summary>
public Texture Texture
{
get;
private set;
}
/// <summary>
/// Creates a new instance of TextureItem.
/// </summary>
/// <param name="Device">Graphical device.</param>
/// <param name="Filename">Name of the texture file.</param>
public TextureItem( Device Device, string Filename )
{
Image Image = Image.FromFile( Filename );
this.Height = Image.Height;
this.Width = Image.Width;
this.Texture = Texture.FromFile( Device, Filename );
}
/// <summary>
/// Creates a new instance of TextureItem.
/// </summary>
/// <param name="Device">Graphical device.</param>
/// <param name="Bitmap">Bitmap of the texture.</param>
//public TextureItem( Device Device, Bitmap Bitmap )
//{
// this.Height = Bitmap.Height;
// this.Width = Bitmap.Width;
// MemoryStream Stream = new MemoryStream();
// Bitmap.Save( Stream, ImageFormat.Png );
// byte[] test = Stream.ToArray();
// this.Texture = Texture.FromMemory( Device, test );
//}
/// <summary>
/// Releases all unmanaged Resources of the object.
/// </summary>
public void Dispose()
{
this.Texture.Dispose();
}
}
Hope for help!
Best regards