I have implemented re-ordering (drag and drop) of ListView items using code in this article, with minor changes. Also View property of the ListView is set to "Tile" and Alignment set to "Left". Code works fine, however Insertion Mark is not entirely in the middle between the Items.
Would it be possible to adjust location of the mark? It is offset by 1px. I also tried to change TileSize but spacing and offset remains the same.
Update: I tried to offset Item drawing by changing corresponding rectangles, however have problem with selected Item rectangle (bottom part is not drawn properly). Also tried to offset Items by simply adding e.Graphics.TranslateTransform(0, 1);
to listView1_DrawItem
event handler but got same unexpected result.
Sample code where the rectangles are drawn. Credits to Uwe Keim.
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = false;
e.Graphics.TranslateTransform(0, 1);
Brush backgroundBrush;
Brush textBrush;
if (e.Item.Selected)
{
backgroundBrush = SystemBrushes.Highlight;
textBrush = SystemBrushes.HighlightText;
}
else
{
backgroundBrush = SystemBrushes.Window;
textBrush = SystemBrushes.WindowText;
}
e.Graphics.FillRectangle(backgroundBrush, e.Bounds); // Draw Background Rectangle
const int squareDistance = 1;
int squareWidth = (e.Bounds.Height - 2) * 2 * squareDistance;
int squareHeight = e.Bounds.Height - 2 * squareDistance;
int offsetX = e.Bounds.Left + squareDistance;
int offsetY = e.Bounds.Top + squareDistance;
Rectangle r = new Rectangle(offsetX, offsetY, squareWidth, squareHeight);
using (Brush brush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(brush, r); // Draw Item Rectangle
}
Rectangle r2 = new Rectangle(r.Left, r.Top, r.Width - 1, r.Height - 1);
Pen pen = SystemPens.ControlDarkDark;
e.Graphics.DrawRectangle(pen, r2); // Draw Boarder of Item Rectangle
offsetX += squareWidth + squareDistance * 2;
RectangleF rf = new RectangleF(offsetX, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString(e.Item.Text, e.Item.Font, textBrush, rf); // Draw Text
}