|
-
Apr 12th, 2012, 08:09 AM
#1
Thread Starter
Frenzied Member
Need somehelp with GDI+ and button
Hi!
I need to create avery basic control that is a button. It will have an image and a text label under the image.
I have come so far that I have created a control that inherits from "Control".
Code:
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;
//default minimal size
this.Size = new Size(21, 21);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )
{
Graphics gxOff; //Offscreen graphics
Rectangle imgRect; //image rectangle
Brush backBrush; //brush for filling a backcolor
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
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 //change the background when it's pressed
// backBrush = new SolidBrush(Parent.BackColor);
backBrush = new SolidBrush(Parent.BackColor);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
if (image != null)
{
//Center the image relativelly to the control
int imageLeft = (this.Width - image.Width) / 2;
int imageTop = (this.Height - this.Height + 6);
if (!bPushed)
{
imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
}
else //The button was pressed
{
//Shift the image by one pixel
imgRect = new Rectangle(imageLeft + 1 , imageTop +1, image.Width, image.Height);
}
//Set transparent key
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image));
//Draw image
gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
// Draw text under the image
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
string drawString = "press me";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
if (!bPushed)
{
Rectangle r = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y + 25, this.ClientRectangle.Width, this.ClientRectangle.Height);
//gxOff.DrawString(drawString, drawFont, drawBrush, imageLeft, imageTop + image.Height, stringFormat);
gxOff.DrawString(drawString, drawFont, drawBrush, r, stringFormat);
}
else
{
Rectangle r = new Rectangle(this.ClientRectangle.X + 1, this.ClientRectangle.Y + 26, this.ClientRectangle.Width, this.ClientRectangle.Height);
//gxOff.DrawString(drawString, drawFont, drawBrush, imageLeft + 1, imageTop + image.Height + 1, stringFormat);
gxOff.DrawString(drawString, drawFont, drawBrush, r, stringFormat);
}
}
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
{
//Do nothing
}
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);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
this.Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
this.Invalidate();
}
}
The problem I have is that I can't seem to align the text with the image, and when I click on the text, the button doesn't fire the onclick event. Only when I click the image does the click event fire. The text should be centered under the image with a small distance between. The entire button should be composed by both the image and the text (clientrectangle?). So the user should be able to click anywhere. And when clicked, the entire button should shift down right one pixel.
To be honest, this is my first time doing something with GDI+, and the above code is probably crap.
One other strange thing is that the text gets clipped when I run the program. In the designer (if I just drag it onto a form and adjust borders) it looks fine.
Can someone please help me out, the above code compile and run fine, but it most likely bad due to the errors I get.
kind regards
Henrik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|