badsha

Listview

Jan 3rd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4.  
  5. //********************************************************************************
  6. //* Created by Predrag Gruevski (obi1kenobi) *
  7. //* Originally published on VBForums *
  8. //* 02.01.2010 *
  9. //* Feel free to use this code for any use you see fit, *
  10. //* just please do not alter this info box and credit me for the code. *
  11. //********************************************************************************
  12.  
  13. public class TransparentListView : ListView
  14. {
  15.  
  16. #region " Consts "
  17.  
  18. const int CLR_NONE = -1;
  19. const int LVM_FIRST = 0x1000;
  20. const int LVM_GETBKCOLOR = LVM_FIRST + 0;
  21. const int LVM_SETBKCOLOR = LVM_FIRST + 1;
  22. const int WM_HSCROLL = 0x114;
  23. const int WM_VSCROLL = 0x115;
  24.  
  25. const int SBM_SETSCROLLINFO = 0xe9;
  26. [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  27. private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
  28. #endregion
  29.  
  30. #region " APIs "
  31.  
  32.  
  33. #endregion
  34.  
  35. #region " Variables "
  36.  
  37. private Timer tmr = new Timer();
  38. private Stopwatch stp = new Stopwatch();
  39. private bool _redrawOnMouseMove;
  40. private int _interval = 300;
  41. private Color _highlightColor;
  42. private Color _errorColor;
  43.  
  44. private int itemHeight;
  45. #endregion
  46.  
  47. #region " Constructors "
  48.  
  49. /// <summary>
  50. /// Creates a new TransparentListView object.
  51. /// </summary>
  52. /// <remarks></remarks>
  53. public TransparentListView()
  54. {
  55. this.OwnerDraw = true;
  56. this.DoubleBuffered = true;
  57. this.Font = new Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point);
  58. tmr.Interval = _interval;
  59. }
  60.  
  61. #endregion
  62.  
  63. #region " Properties "
  64.  
  65. /// <summary>
  66. /// The color to use when a certain ListViewItem has unresolved issues.
  67. /// </summary>
  68. public Color ErrorTextColor {
  69. get { return _errorColor; }
  70. set { _errorColor = value; }
  71. }
  72.  
  73. /// <summary>
  74. /// The color to use when highlighting an item.
  75. /// </summary>
  76. public Color HighlightColor {
  77. get { return _highlightColor; }
  78. set { _highlightColor = value; }
  79. }
  80.  
  81. /// <summary>
  82. /// The intervals at which to redraw the control when scrolling. Higher values are reccomended for less powerful CPUs.
  83. /// Decrease this value if experiencing choppy redraws during scrolling. Values below 5-6ms may result in extreme CPU use.
  84. /// </summary>
  85. public int RedrawInterval {
  86. get { return _interval; }
  87. set {
  88. if (value <= 0) {
  89. _interval = 15;
  90. //15ms should result in appx. 60 refreshes per second (60Hz) - only when required
  91. tmr.Interval = 15;
  92. } else {
  93. _interval = value;
  94. tmr.Interval = value;
  95. }
  96. }
  97. }
  98.  
  99. /// <summary>
  100. /// True if the control should be redrawn when the mouse is moved, otherwise False.
  101. /// </summary>
  102. /// <remarks>There have been some issues with the Set method, so it's temporarily disabled and has no effect.</remarks>
  103. public bool RedrawOnMouseMove {
  104. get { return _redrawOnMouseMove; }
  105. //_redrawOnMouseMove = value
  106. set { }
  107. }
  108.  
  109. #endregion
  110.  
  111. #region " Methods "
  112.  
  113. #region " Overriden Methods "
  114.  
  115. protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
  116. {
  117. base.OnMouseWheel(e);
  118. OnListViewScrolled(EventArgs.Empty);
  119. }
  120.  
  121. protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
  122. {
  123. if (this.Items.Count > 0) {
  124. ListViewItem clickedItem = this.GetItemAt(5, e.Y);
  125. if ((clickedItem != null)) {
  126. clickedItem.Selected = true;
  127. clickedItem.Focused = true;
  128. //Else
  129. //Dim bnd As Integer = Me.Items.Count - 1
  130. //For i As Integer = 0 To bnd
  131. // clickedItem = Me.Items(bnd)
  132. // If clickedItem.Bounds.Contains(5, e.Y) Then
  133. // clickedItem.Selected = True
  134. // clickedItem.Focused = True
  135. // Exit For
  136. //End If
  137. // Next
  138. }
  139. }
  140. base.OnMouseUp(e);
  141. }
  142.  
  143. protected override void OnResize(System.EventArgs e)
  144. {
  145. this.Refresh();
  146. base.OnResize(e);
  147. }
  148.  
  149. [DebuggerStepThrough()]
  150.  
  151. protected override void OnDrawItem(System.Windows.Forms.DrawListViewItemEventArgs e)
  152. {
  153. if (!((e.State & ListViewItemStates.Selected) == 0) || e.Item.Selected) {
  154. using (Drawing.SolidBrush br = new Drawing.SolidBrush(_highlightColor)) {
  155.  
  156. //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)
  157. // Draw the background for a selected item.
  158. e.Graphics.FillRectangle(br, e.Bounds);
  159. //e.DrawFocusRectangle() 'Disabled focus rectangle since it was off-center
  160. }
  161. //Else
  162. // Draw the background for an unselected item.
  163. }
  164.  
  165. //Dim sf As New StringFormat()
  166. //Dim index As Integer
  167. //Dim clr As Color
  168. //
  169. // index = SmallImageList.Images.IndexOfKey(e.Item.ImageKey)
  170. // e.Graphics.DrawImage(SmallImageList.Images.Item(index), New Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, 16, 16))
  171. // Using br As New SolidBrush(clr)
  172. // 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)
  173. // End Using
  174.  
  175. //If Not DirectCast(e.Item.Tag, Users.FileData).FileExists Then
  176. //Using br2 As New Drawing.SolidBrush(Color.FromArgb(100, 255, 0, 0))
  177. // e.Graphics.FillRectangle(br2, e.Bounds)
  178. // End Using
  179. //End If
  180.  
  181. // Draw the item text for views other than the Details view.
  182. if (!(this.View == View.Details)) {
  183. e.DrawText();
  184. }
  185. base.OnDrawItem(e);
  186. }
  187.  
  188. //<DebuggerStepThrough()> _
  189. protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
  190. {
  191. //Dim flags As TextFormatFlags = TextFormatFlags.Left
  192.  
  193. StringFormat sf = new StringFormat();
  194. Color clr = default(Color);
  195. int index = 0;
  196.  
  197.  
  198. try {
  199. sf.LineAlignment = StringAlignment.Far;
  200. // Store the column text alignment, letting it default
  201. // to Left if it has not been set to Center or Right.
  202. switch (e.Header.TextAlign) {
  203. case HorizontalAlignment.Center:
  204. sf.Alignment = StringAlignment.Center;
  205. break;
  206. case HorizontalAlignment.Right:
  207. sf.Alignment = StringAlignment.Far;
  208. break;
  209. }
  210.  
  211. e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
  212.  
  213. if (e.Item.ForeColor != _errorColor) {
  214. clr = this.ForeColor;
  215. } else {
  216. clr = _errorColor;
  217. }
  218.  
  219. //Dim lv As ListViewItem = e.Item
  220. //Dim tmp As String
  221. //
  222. // For Each t As ListViewItem.ListViewSubItem In e.Item.SubItems
  223. // tmp = t.Text
  224. // Next
  225.  
  226. using (SolidBrush br = new SolidBrush(clr)) {
  227. if (e.Item.Text == e.SubItem.Text) {
  228. if (SmallImageList != null) {
  229. index = SmallImageList.Images.IndexOfKey(e.Item.ImageKey);
  230. if (index != -1) {
  231. e.Graphics.DrawImage(SmallImageList.Images.Item(index), new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, 16, 16));
  232. 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);
  233. } else {
  234. 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);
  235. }
  236. } else {
  237. 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);
  238. }
  239. } else {
  240. //e.DrawText()
  241. e.Graphics.DrawString(e.SubItem.Text, this.Font, br, e.Bounds, sf);
  242. }
  243. }
  244.  
  245. //e.DrawText(flags)
  246.  
  247. } finally {
  248. sf.Dispose();
  249. }
  250.  
  251. //MyBase.OnDrawSubItem(e)
  252. }
  253.  
  254. [DebuggerStepThrough()]
  255. protected override void OnDrawColumnHeader(System.Windows.Forms.DrawListViewColumnHeaderEventArgs e)
  256. {
  257. StringFormat sf = new StringFormat();
  258.  
  259. try {
  260. // Store the column text alignment, letting it default
  261. // to Left if it has not been set to Center or Right.
  262. sf.LineAlignment = StringAlignment.Center;
  263. switch (e.Header.TextAlign) {
  264. case HorizontalAlignment.Center:
  265. sf.Alignment = StringAlignment.Center;
  266. break;
  267. case HorizontalAlignment.Right:
  268. sf.Alignment = StringAlignment.Far;
  269. break;
  270. }
  271.  
  272. // Draw the standard header background.
  273. e.DrawBackground();
  274.  
  275. // Draw the header text.
  276. Font headerFont = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
  277. try {
  278. e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf);
  279. } finally {
  280. headerFont.Dispose();
  281. }
  282.  
  283. } finally {
  284. sf.Dispose();
  285. }
  286.  
  287. //MyBase.OnDrawColumnHeader(e)
  288. }
  289.  
  290. protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
  291. {
  292. if (_redrawOnMouseMove) {
  293. ListViewItem item = this.GetItemAt(e.X, e.Y);
  294. //AndAlso item.Tag Is Nothing Then
  295. if (item != null) {
  296. this.Invalidate(item.Bounds);
  297. //item.Tag = "tagged"
  298. }
  299. }
  300. base.OnMouseMove(e);
  301. }
  302.  
  303. protected override void OnInvalidated(System.Windows.Forms.InvalidateEventArgs e)
  304. {
  305. //Related to the use of Tag and RedrawOnMouseMove properties.
  306.  
  307. //For Each item As ListViewItem In Me.Items
  308. //If item Is Nothing Then Return
  309. //item.Tag = Nothing
  310. //Next
  311. base.OnInvalidated(e);
  312. }
  313.  
  314. protected override void OnColumnWidthChanged(System.Windows.Forms.ColumnWidthChangedEventArgs e)
  315. {
  316. this.Invalidate();
  317. base.OnColumnWidthChanged(e);
  318. }
  319.  
  320. protected override void OnHandleCreated(System.EventArgs e)
  321. {
  322. base.OnHandleCreated(e);
  323. SendMessage(this.Handle.ToInt32, LVM_SETBKCOLOR, 0, CLR_NONE);
  324. }
  325.  
  326. [DebuggerStepThrough()]
  327. protected override void WndProc(ref System.Windows.Forms.Message m)
  328. {
  329. switch (m.Msg) {
  330. case WM_VSCROLL:
  331. OnListViewScrolled(EventArgs.Empty);
  332. break;
  333. case WM_HSCROLL:
  334. OnListViewScrolled(EventArgs.Empty);
  335. break;
  336. case SBM_SETSCROLLINFO:
  337. OnListViewScrolled(EventArgs.Empty);
  338. break;
  339. }
  340. base.WndProc(m);
  341. }
  342.  
  343. #endregion
  344.  
  345. #region " Custom Methods "
  346.  
  347. protected virtual void OnListViewScrolled(EventArgs e)
  348. {
  349. if (stp.IsRunning) {
  350. if (stp.ElapsedMilliseconds > _interval) {
  351. stp = Stopwatch.StartNew;
  352. this.Invalidate();
  353. }
  354. } else {
  355. stp.Start();
  356. }
  357. tmr.Stop();
  358. tmr.Start();
  359. if (ListViewScrolled != null) {
  360. ListViewScrolled(this, e);
  361. }
  362. }
  363.  
  364. #endregion
  365.  
  366. #endregion
  367.  
  368. #region " Event Handlers "
  369.  
  370. private void // ERROR: Handles clauses are not supported in C#
  371. tmr_Tick(object sender, System.EventArgs e)
  372. {
  373. this.Invalidate();
  374. tmr.Enabled = false;
  375. }
  376.  
  377. #endregion
  378.  
  379. #region " Events "
  380.  
  381. public event ListViewScrolledEventHandler ListViewScrolled;
  382. public delegate void ListViewScrolledEventHandler(object sender, EventArgs e);
  383.  
  384. #endregion
  385.  
  386. }
Advertisement
Add Comment
Please, Sign In to add comment