Hi there,
I have a form, on top of which is a rectangle filled with an image of a football pitch. When I move a player icon that has a transparent background over this pitch the transparent background on the icon shows the base color of the form rather then the football pitch. If someone could tell me how I can make the transparency show the pitch rather then the form's base colour I would be grateful.
Heres a picture of what it looks like:
http://homepage.ntlworld.com/andrew.baldock/pic.jpg
Below is the code I used:
public Form1()
{
InitializeComponent();
CenterToScreen();
beast.SizeMode = PictureBoxSizeMode.StretchImage;
beast.Location = new System.Drawing.Point(64, 32);
beast.Size = new System.Drawing.Size(38, 38);
beast.Cursor = Cursors.Hand;
beast.Image = new Bitmap("rtbeast1b.gif");
beast.MouseDown += new MouseEventHandler(beast_MouseDown);
beast.MouseUp += new MouseEventHandler(beast_MouseUp);
beast.MouseMove += new MouseEventHandler(beast_MouseMove);
Controls.Add(beast);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImageUnscaledAndClipped(pitch, dropRect);
}
private void beast_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
oldX = e.X;
oldY = e.Y;
}
private void beast_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
if (dropRect.Contains((e.X + beast.Left), (e.Y + beast.Top)))
{
int x = (e.X + beast.Left) - dropRect.X;
int y = (e.Y + beast.Top) - dropRect.Y;
x = x / 30;
y = y / 30;
beast.Left = (x * 30) + dropRect.Left;
beast.Top = (y * 30) + dropRect.Top;
}
}
}
private void beast_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
Many thanks,
John
Loading
Richard BlythePosted Jul 22, 2007, 7:20 PM
Actually what is happening is that the picturebox's backcolor is showing through and it is set to the form's backcolor. I ran into this same situation a while back. The best way to overcome this problem is to paint the image directly when you are painting your background. This will require you to write your own mouse detection code. Aw it ain't that hard! What you can do is create your own class that will store the necesarry paint items: Location, Size and Bitmap. Next create the mouse detection routine as such: Inside your custom class create a routine that's called HitTest. Here's the code in C#:
public bool HitTest(Point pnt)
{
//check and see if the current mouse position "pnt" is
//anywhere inside the bounds of the Bitmap
if(new Rectangle(Location,Size).Contains(pnt))
return true;
else
return false;
}
To wire this up to your form, trap the MouseMove event and inside, run the HitTest method in your class.
//Scenario
if(myclass.HitTest(e.Location) == true)
//.......Code response here
There are more things to do before the code will run completely but I hope you get the picture. Do yourself a favor and create your own custom class. In my beginning stages of programming I was leery about classes but I absolutly love them now!
It keeps all your code tucked away in a neat file instead of strung all over the place.
If you have any other questions just let me know.