Form Backgound Image display problem

S

Steve

Hi:
I created a form without frame and the background image is replaced with a
png file.
I also created a close button and MouseEnter () MouseLeave () event to
change the image of the closed button.

The form doesn't display the backgound image sometimes, about 1/5 times.

my source code is:
===================================
public partial class Launcher : Form
{
// to move window by mouse
public class FormCtrl
{
public bool m_bMouseDown;
public Point m_pMouseScnPos;
public Point m_pFormLoc;
};

public FormCtrl m_formCtrl;

public Launcher()
{
InitializeComponent();

m_formCtrl = new FormCtrl ();
m_formCtrl.m_bMouseDown = false;
}

private void Launcher_Load(object sender, EventArgs e)
{
}

private void Launcher_MouseDown(object sender, MouseEventArgs e)
{
m_formCtrl.m_bMouseDown = true;
m_formCtrl.m_pMouseScnPos = this.PointToScreen(new Point(e.X, e.Y));
m_formCtrl.m_pFormLoc = Location;
}

private void Launcher_MouseMove(object sender, MouseEventArgs e)
{
if (m_formCtrl.m_bMouseDown)
{
Point pPos;
pPos = this.PointToScreen(new Point(e.X, e.Y));

Point dP = pPos - new Size(m_formCtrl.m_pMouseScnPos);
Location = m_formCtrl.m_pFormLoc + new Size(dP);
}
}

private void Launcher_MouseUp(object sender, MouseEventArgs e)
{
m_formCtrl.m_bMouseDown = false;
}

private void closeBox_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void closeBox_MouseEnter(object sender, EventArgs e)
{
closeBox.Image =
Image.FromFile("../../Resources/image/Tennis/Close_button_MouseOver.png");
}

private void closeBox_MouseLeave(object sender, EventArgs e)
{
closeBox.Image =
Image.FromFile("../../Resources/image/Tennis/Close_button.png");
}
}
========================================

thank you.

steve
 
M

Morten Wennevik [C# MVP]

Hi Steve,

I can't reproduce the problem, but there are a couple of things you can try.
Instead of loading the image from disk each time keep the images in memory.
Furthermore, you might want to call closeBox.Invalidate() to force it to
redraw itself on MouseEnter/MouseLeave.
 
S

Steve

Thank you for your help.

After add extra lines of codes, the problem is gone. I don't know why it's
gone. I had rebuilt the solution. I guess my computer (or my .NET 2005) is
not stable.

I will keep watching this issue. If it happen again, I will try Invalidate
() for my form background to force redraw.

steve


Morten Wennevik said:
Hi Steve,

I can't reproduce the problem, but there are a couple of things you can
try.
Instead of loading the image from disk each time keep the images in
memory.
Furthermore, you might want to call closeBox.Invalidate() to force it to
redraw itself on MouseEnter/MouseLeave.

--
Happy Coding!
Morten Wennevik [C# MVP]


Steve said:
Hi:
I created a form without frame and the background image is replaced with
a
png file.
I also created a close button and MouseEnter () MouseLeave () event to
change the image of the closed button.

The form doesn't display the backgound image sometimes, about 1/5 times.

my source code is:
===================================
public partial class Launcher : Form
{
// to move window by mouse
public class FormCtrl
{
public bool m_bMouseDown;
public Point m_pMouseScnPos;
public Point m_pFormLoc;
};

public FormCtrl m_formCtrl;

public Launcher()
{
InitializeComponent();

m_formCtrl = new FormCtrl ();
m_formCtrl.m_bMouseDown = false;
}

private void Launcher_Load(object sender, EventArgs e)
{
}

private void Launcher_MouseDown(object sender, MouseEventArgs e)
{
m_formCtrl.m_bMouseDown = true;
m_formCtrl.m_pMouseScnPos = this.PointToScreen(new Point(e.X,
e.Y));
m_formCtrl.m_pFormLoc = Location;
}

private void Launcher_MouseMove(object sender, MouseEventArgs e)
{
if (m_formCtrl.m_bMouseDown)
{
Point pPos;
pPos = this.PointToScreen(new Point(e.X, e.Y));

Point dP = pPos - new Size(m_formCtrl.m_pMouseScnPos);
Location = m_formCtrl.m_pFormLoc + new Size(dP);
}
}

private void Launcher_MouseUp(object sender, MouseEventArgs e)
{
m_formCtrl.m_bMouseDown = false;
}

private void closeBox_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void closeBox_MouseEnter(object sender, EventArgs e)
{
closeBox.Image =
Image.FromFile("../../Resources/image/Tennis/Close_button_MouseOver.png");
}

private void closeBox_MouseLeave(object sender, EventArgs e)
{
closeBox.Image =
Image.FromFile("../../Resources/image/Tennis/Close_button.png");
}
}
========================================

thank you.

steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top