Re: image buttons by Chris
Chris
Fri Oct 01 08:49:24 CDT 2004
It seems like you're trying to run before you've learned to crawl. At this
point you really need to get a "Beginning C# Programming book" and read
through it. Control placement is about as simple a concept as there is, and
setting an image is fairly evident by just reading the code provided. Wrox
press does a good introduction to C# book, though I'm sure most publishers
do.
Once you've gone through a bsaic tutorial and understand the code Darren
posted, then you can begin to do more involved work.
As for Darren'c code specifically, I'd avoid all the object creation in
OnPaint - it's called a lot and this is a perf hit. Also, thing like Brush
objects should be disposed when you're done with them. Again, once you get
through some of the basics, these comments will make more sense.
-Chris
"pat" <pat@discussions.microsoft.com> wrote in message
news:5847D1BA-5159-49E2-9857-F848E288AC84@microsoft.com...
> Dear Mr. Shaffer!
>
> Thank you for your detailed reply!! Do i then only have to write something
> like this?
>
> ImageButton button = new ImageButton();
>
> But where can i indicate where the button should be situated on the Form
of
> the Smart Device Application and which image - the source - should
contain
> the button?
>
> regards
>
> patrick
>
> "Darren Shaffer" wrote:
>
> > Here you go Pat:
> >
> > using System;
> > using System.Drawing;
> > using System.Windows.Forms;
> > using System.Drawing.Imaging;
> >
> > namespace Sample
> > {
> > public class ImageButton : Control
> > {
> > private Image image;
> > private bool bPushed;
> > private Bitmap m_bmpOffscreen;
> >
> > public Image Image
> > {
> > get { return image; }
> > set { image = value; }
> > }
> >
> > public ImageButton()
> > {
> > bPushed = false;
> > this.Size = new Size(21, 21);
> > }
> >
> > protected override void
OnPaint(System.Windows.Forms.PaintEventArgs e )
> > {
> > Graphics gxOff;
> > Rectangle imgRect;
> > Brush backBrush;
> > if (m_bmpOffscreen == null)
> > m_bmpOffscreen = new Bitmap(ClientSize.Width,
ClientSize.Height);
> > gxOff = Graphics.FromImage(m_bmpOffscreen);
> > gxOff.Clear(this.BackColor);
> >
> > if (!bPushed)
> > backBrush = new SolidBrush(Parent.BackColor);
> > else
> > backBrush = new SolidBrush(Color.LightGray);
> >
> > gxOff.FillRectangle(backBrush, this.ClientRectangle);
> >
> > if (image != null)
> > {
> > int imageLeft = (this.Width - image.Width) / 2;
> > int imageTop = (this.Height - image.Height) / 2;
> >
> > if (!bPushed)
> > imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
> > else
> > imgRect = new Rectangle(imageLeft + 1 , imageTop +1, image.Width,
> > image.Height);
> >
> > ImageAttributes imageAttr = new ImageAttributes();
> > imageAttr.SetColorKey(BackgroundImageColor(image),
> > BackgroundImageColor(image));
> > gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height,
> > GraphicsUnit.Pixel, imageAttr);
> > }
> > if (bPushed)
> > {
> > Rectangle rc = this.ClientRectangle;
> > rc.Width--;
> > rc.Height--;
> > gxOff.DrawRectangle(new Pen(Color.Black), rc);
> > }
> > e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
> > base.OnPaint(e);
> > }
> >
> > protected override void
> > OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
> > {
> > }
> >
> > protected override void OnMouseDown (
System.Windows.Forms.MouseEventArgs e )
> > {
> > bPushed = true;
> > this.Invalidate();
> > }
> >
> > protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs
e )
> > {
> > bPushed = false;
> > this.Invalidate();
> > }
> >
> > private Color BackgroundImageColor(Image image)
> > {
> > Bitmap bmp = new Bitmap(image);
> > return bmp.GetPixel(0, 0);
> > }
> > }
> > }
> >
> > - Darren Shaffer
> >
> >
> >
> > "pat" wrote:
> >
> > >
> > >
> > > Sorry but this code sample is in VB and i have no experiences in VB
:( Is
> > > such an code sample also available in C#??:-/
> > >
> > > regards
> > >
> > > patrick