Drawing Graphics Over Controls

  • Thread starter Thread starter Genie.Jinesh
  • Start date Start date
G

Genie.Jinesh

Guest
I have this code for a custom listview, but when the selection box is drawn, it is drawn underneath the listviewitems. How would i draw it over the ListViewItems.

using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace AlbumManager
{
public partial class Album : ScrollableControl
{

private int _Selection = 0;

private int _count = 0;

public Album()
{

AutoScroll = true;

InitializeComponent();

DoubleBuffered = true;

}

public Album(IContainer container)
{

AutoScroll = true;

container.Add(this);

DoubleBuffered = true;

InitializeComponent();

}

public int Selection
{
get
{
return _Selection;
}
set
{
_Selection = value;
}
}

public int Count
{
get
{
return _count;
}
set
{
_count = value;
}
}

public void AddAlbumImage()
{

OpenFileDialog _openFile = new OpenFileDialog();

_openFile.Filter = "Image Files|*.bmp; *.gif; *.jpg; *.png";

if (_openFile.ShowDialog() == DialogResult.OK)
{

Add(_openFile.FileName);

}

}

public void AddAlbumImage(bool Multiselect)
{

OpenFileDialog _openFile = new OpenFileDialog();

_openFile.Filter = "Image Files|*.bmp; *.gif; *.jpg; *.png";

if (Multiselect == true)
{
_openFile.Multiselect = true;
}

if (_openFile.ShowDialog() == DialogResult.OK)
{

if (Multiselect == true)
{

foreach (string _fileName in _openFile.FileNames)
{

Add(_fileName);

}

}
else
{

Add(_openFile.FileName);

}

}

}

private void Add(string FileName)
{
AlbumImage _albumImage = new AlbumImage();

_albumImage.Index = Count++;
_albumImage.Photo = Image.FromFile(FileName);
_albumImage.Text = Path.GetFileNameWithoutExtension(FileName);

int x = 20;
int y = 20;

foreach (Control _control in Controls)
{
if (_control.GetType() == typeof(AlbumImage))
{

x += 120;
if (x + 120 > Width)
{

y += 120;
x = 20;

}

}

}

_albumImage.Location = new Point(x, y);

Controls.Add(_albumImage);

_albumImage.BringToFront();
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

e.Graphics.Clear(Color.White);

if (s == true)
{
if (p.Y > d.Y && p.X < d.X)
{
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(p.X, d.Y, d.X - p.X, p.Y - d.Y));
}
else if (p.Y > d.Y && p.X > d.X)
{
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(d.X, d.Y, p.X - d.X, p.Y - d.Y));
}
else if (p.Y < d.Y && p.X > d.X)
{
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(d.X, p.Y, p.X - d.X, d.Y - p.Y));
}
else
{
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(p.X, p.Y, d.X - p.X, d.Y - p.Y));
}
}

}

private Point p;
private MouseEventArgs d;
private bool s = false;

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
foreach (Control _control in Controls)
{
if (_control.GetType() == typeof(AlbumImage))
{
AlbumImage _albumImage = (AlbumImage)_control;
_albumImage.Selected = false;
_albumImage.MouseOver = false;
}
}
p = e.Location;
s = true;
Refresh();
}

protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
d = e;
Refresh();
if (s == true)
{
foreach (Control _control in Controls)
{
if (_control.GetType() == typeof(AlbumImage))
{
AlbumImage _albumImage = (AlbumImage)_control;
if (p.Y > d.Y && p.X < d.X)
{
if ((_albumImage.Location.X >= p.X && _albumImage.Location.Y <= p.Y) && (_albumImage.Location.X <= d.X && _albumImage.Location.Y >= d.Y))
{
_albumImage.MouseOver = true; MessageBox.Show("S");
}
else
{
_albumImage.MouseOver = false;
}

}
else if (p.Y > d.Y && p.X > d.X)
{
if ((_albumImage.Location.X <= p.X && _albumImage.Location.Y <= p.Y) && (_albumImage.Location.X >= d.X && _albumImage.Location.Y >= d.Y))

{
_albumImage.MouseOver = true;
}
else
{
_albumImage.MouseOver = false;
}
}
else if (p.Y < d.Y && p.X > d.X)
{
if ((_albumImage.Location.X <= p.X && _albumImage.Location.Y >= p.Y) && (_albumImage.Location.X >= d.X && _albumImage.Location.Y <= d.Y))
{
_albumImage.MouseOver = true;
}
else
{
_albumImage.MouseOver = false;
}
}
else
{
if ((_albumImage.Location.X >= p.X && _albumImage.Location.Y >= p.Y) && (_albumImage.Location.X <= d.X && _albumImage.Location.Y <= d.Y))
{
_albumImage.MouseOver = true;
}
else
{
_albumImage.MouseOver = false;
}
}


}

}
}
}

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
s = false;
Refresh();
foreach (Control _control in Controls)
{
if (_control.GetType() == typeof(AlbumImage))
{
AlbumImage _albumImage = (AlbumImage)_control;

if (p.Y > d.Y && p.X < d.X)
{
if ((_albumImage.Location.X >= p.X && _albumImage.Location.Y <= p.Y) && (_albumImage.Location.X <= d.X && _albumImage.Location.Y >= d.Y))
{
_albumImage.Selected = true;
}

}
else if (p.Y > d.Y && p.X > d.X)
{
if ((_albumImage.Location.X <= p.X && _albumImage.Location.Y <= p.Y) && (_albumImage.Location.X >= d.X && _albumImage.Location.Y >= d.Y))
{
_albumImage.Selected = true;
}
}
else if (p.Y < d.Y && p.X > d.X)
{
if ((_albumImage.Location.X <= p.X && _albumImage.Location.Y >= p.Y) && (_albumImage.Location.X >= d.X && _albumImage.Location.Y <= d.Y))
{
_albumImage.Selected = true;
}
}
else
{
if ((_albumImage.Location.X >= p.X && _albumImage.Location.Y >= p.Y) && (_albumImage.Location.X <= d.X && _albumImage.Location.Y <= d.Y))
{
_albumImage.Selected = true;
}
}

}

}

}

}
}

Continue reading...
 
Back
Top