T
TonyR
I have written a pictureButton control, but I cannot seem to set a default,
width, height and BackColor.
I am using Visual Studio 2005.
I thought you could set it in the constructor, but nothing seems to change,
the control is created as a 200x200 control
The code I have used is as follows...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace rcl.ppc.Controls
{
public partial class pictureButton : Control
{
public pictureButton()
{
InitializeComponent();
}
Bitmap image;
public Bitmap Image
{
get
{
return this.image;
}
set
{
this.image = value;
}
}
private ContentAlignment imageAlign = ContentAlignment.MiddleCenter;
public ContentAlignment ImageAlign
{
get
{
return imageAlign;
}
set
{
imageAlign = value;
this.Invalidate();
}
}
private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
public ContentAlignment TextAlign
{
get
{
return textAlign;
}
set
{
textAlign = value;
this.Invalidate();
}
}
private Color disabledBackColor = Color.WhiteSmoke;
/// <value>Color.WhiteSmoke</value>
public Color DisabledBackColor
{
get
{
return disabledBackColor;
}
set
{
disabledBackColor = value;
//may need to invalidate
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseUp(e);
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle dstRect;
if (this.image != null)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(this.image.GetPixel(0, 0), this.image.GetPixel(0,
0));
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,this.image.Width,this.image.Height,imageAlign);
e.Graphics.DrawImage(this.image, dstRect, 0, 0, image.Width,
image.Height,
GraphicsUnit.Pixel, attr);
}
// Draw the text if there is any.
if (this.Text.Length > 0)
{
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,Convert.ToInt32(size.Width),Convert.ToInt32(size.Height),textAlign);
// Center the text inside the client area of the PictureButton.
e.Graphics.DrawString(this.Text,
this.Font,
new SolidBrush(this.ForeColor),dstRect.X,dstRect.Y);
}
// Draw a border around the outside of the
// control to look like Pocket PC buttons.
e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0,
this.ClientSize.Width - 1, this.ClientSize.Height - 1);
if (Enabled == false)
{
e.Graphics.FillRectangle(new SolidBrush(disabledBackColor), 1, 1,
this.ClientSize.Width - 2, this.ClientSize.Height - 2);
}
base.OnPaint(e);
}
}
}
width, height and BackColor.
I am using Visual Studio 2005.
I thought you could set it in the constructor, but nothing seems to change,
the control is created as a 200x200 control
The code I have used is as follows...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace rcl.ppc.Controls
{
public partial class pictureButton : Control
{
public pictureButton()
{
InitializeComponent();
}
Bitmap image;
public Bitmap Image
{
get
{
return this.image;
}
set
{
this.image = value;
}
}
private ContentAlignment imageAlign = ContentAlignment.MiddleCenter;
public ContentAlignment ImageAlign
{
get
{
return imageAlign;
}
set
{
imageAlign = value;
this.Invalidate();
}
}
private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
public ContentAlignment TextAlign
{
get
{
return textAlign;
}
set
{
textAlign = value;
this.Invalidate();
}
}
private Color disabledBackColor = Color.WhiteSmoke;
/// <value>Color.WhiteSmoke</value>
public Color DisabledBackColor
{
get
{
return disabledBackColor;
}
set
{
disabledBackColor = value;
//may need to invalidate
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseUp(e);
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle dstRect;
if (this.image != null)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(this.image.GetPixel(0, 0), this.image.GetPixel(0,
0));
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,this.image.Width,this.image.Height,imageAlign);
e.Graphics.DrawImage(this.image, dstRect, 0, 0, image.Width,
image.Height,
GraphicsUnit.Pixel, attr);
}
// Draw the text if there is any.
if (this.Text.Length > 0)
{
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,Convert.ToInt32(size.Width),Convert.ToInt32(size.Height),textAlign);
// Center the text inside the client area of the PictureButton.
e.Graphics.DrawString(this.Text,
this.Font,
new SolidBrush(this.ForeColor),dstRect.X,dstRect.Y);
}
// Draw a border around the outside of the
// control to look like Pocket PC buttons.
e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0,
this.ClientSize.Width - 1, this.ClientSize.Height - 1);
if (Enabled == false)
{
e.Graphics.FillRectangle(new SolidBrush(disabledBackColor), 1, 1,
this.ClientSize.Width - 2, this.ClientSize.Height - 2);
}
base.OnPaint(e);
}
}
}