Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Drawing;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- //********************************************************************************
- //* Created by Predrag Gruevski (obi1kenobi) *
- //* Originally published on VBForums *
- //* 02.01.2010 *
- //* Feel free to use this code for any use you see fit, *
- //* just please do not alter this info box and credit me for the code. *
- //********************************************************************************
- public class TransparentListView : ListView
- {
- #region " Consts "
- const int CLR_NONE = -1;
- const int LVM_FIRST = 0x1000;
- const int LVM_GETBKCOLOR = LVM_FIRST + 0;
- const int LVM_SETBKCOLOR = LVM_FIRST + 1;
- const int WM_HSCROLL = 0x114;
- const int WM_VSCROLL = 0x115;
- const int SBM_SETSCROLLINFO = 0xe9;
- [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
- #endregion
- #region " APIs "
- #endregion
- #region " Variables "
- private Timer tmr = new Timer();
- private Stopwatch stp = new Stopwatch();
- private bool _redrawOnMouseMove;
- private int _interval = 300;
- private Color _highlightColor;
- private Color _errorColor;
- private int itemHeight;
- #endregion
- #region " Constructors "
- /// <summary>
- /// Creates a new TransparentListView object.
- /// </summary>
- /// <remarks></remarks>
- public TransparentListView()
- {
- this.OwnerDraw = true;
- this.DoubleBuffered = true;
- this.Font = new Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point);
- tmr.Interval = _interval;
- }
- #endregion
- #region " Properties "
- /// <summary>
- /// The color to use when a certain ListViewItem has unresolved issues.
- /// </summary>
- public Color ErrorTextColor {
- get { return _errorColor; }
- set { _errorColor = value; }
- }
- /// <summary>
- /// The color to use when highlighting an item.
- /// </summary>
- public Color HighlightColor {
- get { return _highlightColor; }
- set { _highlightColor = value; }
- }
- /// <summary>
- /// The intervals at which to redraw the control when scrolling. Higher values are reccomended for less powerful CPUs.
- /// Decrease this value if experiencing choppy redraws during scrolling. Values below 5-6ms may result in extreme CPU use.
- /// </summary>
- public int RedrawInterval {
- get { return _interval; }
- set {
- if (value <= 0) {
- _interval = 15;
- //15ms should result in appx. 60 refreshes per second (60Hz) - only when required
- tmr.Interval = 15;
- } else {
- _interval = value;
- tmr.Interval = value;
- }
- }
- }
- /// <summary>
- /// True if the control should be redrawn when the mouse is moved, otherwise False.
- /// </summary>
- /// <remarks>There have been some issues with the Set method, so it's temporarily disabled and has no effect.</remarks>
- public bool RedrawOnMouseMove {
- get { return _redrawOnMouseMove; }
- //_redrawOnMouseMove = value
- set { }
- }
- #endregion
- #region " Methods "
- #region " Overriden Methods "
- protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
- {
- base.OnMouseWheel(e);
- OnListViewScrolled(EventArgs.Empty);
- }
- protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
- {
- if (this.Items.Count > 0) {
- ListViewItem clickedItem = this.GetItemAt(5, e.Y);
- if ((clickedItem != null)) {
- clickedItem.Selected = true;
- clickedItem.Focused = true;
- //Else
- //Dim bnd As Integer = Me.Items.Count - 1
- //For i As Integer = 0 To bnd
- // clickedItem = Me.Items(bnd)
- // If clickedItem.Bounds.Contains(5, e.Y) Then
- // clickedItem.Selected = True
- // clickedItem.Focused = True
- // Exit For
- //End If
- // Next
- }
- }
- base.OnMouseUp(e);
- }
- protected override void OnResize(System.EventArgs e)
- {
- this.Refresh();
- base.OnResize(e);
- }
- [DebuggerStepThrough()]
- protected override void OnDrawItem(System.Windows.Forms.DrawListViewItemEventArgs e)
- {
- if (!((e.State & ListViewItemStates.Selected) == 0) || e.Item.Selected) {
- using (Drawing.SolidBrush br = new Drawing.SolidBrush(_highlightColor)) {
- //Using br As New Drawing2D.LinearGradientBrush(e.Bounds, Color.FromArgb(200, Color.LightSkyBlue.R, Color.LightSkyBlue.B, Color.LightSkyBlue.G), Color.FromArgb(230, Color.Yellow.R, Color.Yellow.B, Color.Yellow.G), Drawing2D.LinearGradientMode.Vertical)
- // Draw the background for a selected item.
- e.Graphics.FillRectangle(br, e.Bounds);
- //e.DrawFocusRectangle() 'Disabled focus rectangle since it was off-center
- }
- //Else
- // Draw the background for an unselected item.
- }
- //Dim sf As New StringFormat()
- //Dim index As Integer
- //Dim clr As Color
- //
- // index = SmallImageList.Images.IndexOfKey(e.Item.ImageKey)
- // e.Graphics.DrawImage(SmallImageList.Images.Item(index), New Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, 16, 16))
- // Using br As New SolidBrush(clr)
- // e.Graphics.DrawString(e.Item.Text, Me.Font, br, New Rectangle(e.Bounds.X + SmallImageList.ImageSize.Width + 4, e.Bounds.Y, e.Bounds.Width - SmallImageList.ImageSize.Width - 4, e.Bounds.Height), sf)
- // End Using
- //If Not DirectCast(e.Item.Tag, Users.FileData).FileExists Then
- //Using br2 As New Drawing.SolidBrush(Color.FromArgb(100, 255, 0, 0))
- // e.Graphics.FillRectangle(br2, e.Bounds)
- // End Using
- //End If
- // Draw the item text for views other than the Details view.
- if (!(this.View == View.Details)) {
- e.DrawText();
- }
- base.OnDrawItem(e);
- }
- //<DebuggerStepThrough()> _
- protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
- {
- //Dim flags As TextFormatFlags = TextFormatFlags.Left
- StringFormat sf = new StringFormat();
- Color clr = default(Color);
- int index = 0;
- try {
- sf.LineAlignment = StringAlignment.Far;
- // Store the column text alignment, letting it default
- // to Left if it has not been set to Center or Right.
- switch (e.Header.TextAlign) {
- case HorizontalAlignment.Center:
- sf.Alignment = StringAlignment.Center;
- break;
- case HorizontalAlignment.Right:
- sf.Alignment = StringAlignment.Far;
- break;
- }
- e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
- if (e.Item.ForeColor != _errorColor) {
- clr = this.ForeColor;
- } else {
- clr = _errorColor;
- }
- //Dim lv As ListViewItem = e.Item
- //Dim tmp As String
- //
- // For Each t As ListViewItem.ListViewSubItem In e.Item.SubItems
- // tmp = t.Text
- // Next
- using (SolidBrush br = new SolidBrush(clr)) {
- if (e.Item.Text == e.SubItem.Text) {
- if (SmallImageList != null) {
- index = SmallImageList.Images.IndexOfKey(e.Item.ImageKey);
- if (index != -1) {
- e.Graphics.DrawImage(SmallImageList.Images.Item(index), new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, 16, 16));
- e.Graphics.DrawString(e.Item.Text, this.Font, br, new Rectangle(e.Bounds.X + SmallImageList.ImageSize.Width + 4, e.Bounds.Y, e.Bounds.Width - SmallImageList.ImageSize.Width - 4, e.Bounds.Height), sf);
- } else {
- e.Graphics.DrawString(e.Item.Text, this.Font, br, new Rectangle(e.Bounds.X + 4, e.Bounds.Y, e.Bounds.Width - 4, e.Bounds.Height), sf);
- }
- } else {
- e.Graphics.DrawString(e.Item.Text, this.Font, br, new Rectangle(e.Bounds.X + 4, e.Bounds.Y, e.Bounds.Width - 4, e.Bounds.Height), sf);
- }
- } else {
- //e.DrawText()
- e.Graphics.DrawString(e.SubItem.Text, this.Font, br, e.Bounds, sf);
- }
- }
- //e.DrawText(flags)
- } finally {
- sf.Dispose();
- }
- //MyBase.OnDrawSubItem(e)
- }
- [DebuggerStepThrough()]
- protected override void OnDrawColumnHeader(System.Windows.Forms.DrawListViewColumnHeaderEventArgs e)
- {
- StringFormat sf = new StringFormat();
- try {
- // Store the column text alignment, letting it default
- // to Left if it has not been set to Center or Right.
- sf.LineAlignment = StringAlignment.Center;
- switch (e.Header.TextAlign) {
- case HorizontalAlignment.Center:
- sf.Alignment = StringAlignment.Center;
- break;
- case HorizontalAlignment.Right:
- sf.Alignment = StringAlignment.Far;
- break;
- }
- // Draw the standard header background.
- e.DrawBackground();
- // Draw the header text.
- Font headerFont = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
- try {
- e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf);
- } finally {
- headerFont.Dispose();
- }
- } finally {
- sf.Dispose();
- }
- //MyBase.OnDrawColumnHeader(e)
- }
- protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
- {
- if (_redrawOnMouseMove) {
- ListViewItem item = this.GetItemAt(e.X, e.Y);
- //AndAlso item.Tag Is Nothing Then
- if (item != null) {
- this.Invalidate(item.Bounds);
- //item.Tag = "tagged"
- }
- }
- base.OnMouseMove(e);
- }
- protected override void OnInvalidated(System.Windows.Forms.InvalidateEventArgs e)
- {
- //Related to the use of Tag and RedrawOnMouseMove properties.
- //For Each item As ListViewItem In Me.Items
- //If item Is Nothing Then Return
- //item.Tag = Nothing
- //Next
- base.OnInvalidated(e);
- }
- protected override void OnColumnWidthChanged(System.Windows.Forms.ColumnWidthChangedEventArgs e)
- {
- this.Invalidate();
- base.OnColumnWidthChanged(e);
- }
- protected override void OnHandleCreated(System.EventArgs e)
- {
- base.OnHandleCreated(e);
- SendMessage(this.Handle.ToInt32, LVM_SETBKCOLOR, 0, CLR_NONE);
- }
- [DebuggerStepThrough()]
- protected override void WndProc(ref System.Windows.Forms.Message m)
- {
- switch (m.Msg) {
- case WM_VSCROLL:
- OnListViewScrolled(EventArgs.Empty);
- break;
- case WM_HSCROLL:
- OnListViewScrolled(EventArgs.Empty);
- break;
- case SBM_SETSCROLLINFO:
- OnListViewScrolled(EventArgs.Empty);
- break;
- }
- base.WndProc(m);
- }
- #endregion
- #region " Custom Methods "
- protected virtual void OnListViewScrolled(EventArgs e)
- {
- if (stp.IsRunning) {
- if (stp.ElapsedMilliseconds > _interval) {
- stp = Stopwatch.StartNew;
- this.Invalidate();
- }
- } else {
- stp.Start();
- }
- tmr.Stop();
- tmr.Start();
- if (ListViewScrolled != null) {
- ListViewScrolled(this, e);
- }
- }
- #endregion
- #endregion
- #region " Event Handlers "
- private void // ERROR: Handles clauses are not supported in C#
- tmr_Tick(object sender, System.EventArgs e)
- {
- this.Invalidate();
- tmr.Enabled = false;
- }
- #endregion
- #region " Events "
- public event ListViewScrolledEventHandler ListViewScrolled;
- public delegate void ListViewScrolledEventHandler(object sender, EventArgs e);
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment