3535using System . Drawing . Drawing2D ;
3636using System . Drawing . Text ;
3737using System . Windows . Forms ;
38+ using System . Windows . Forms . VisualStyles ;
3839
3940namespace Com . Couchcoding . GuiLibrary . Controls
4041{
@@ -71,6 +72,19 @@ public override string ToString()
7172 #endregion
7273 }
7374
75+ /// <summary>
76+ /// Defines all known <see cref="ListView"/> states.
77+ /// </summary>
78+ private enum ListViewState
79+ {
80+ Normal = 1 ,
81+ Hot = 2 ,
82+ Selected = 3 ,
83+ Disabled = 4 ,
84+ SelectedAnNotFocused = 5 ,
85+ HotSelected = 6
86+ }
87+
7488 #endregion
7589
7690 #region Private Fields
@@ -185,15 +199,29 @@ private void InvalidateItem(int index)
185199 /// Handles the item drawing of a non selected item.
186200 /// </summary>
187201 /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
188- protected virtual void DrawItemHighlighted ( DrawItemEventArgs e )
202+ /// <param name="selected">Determines wheter the item is currently selected.</param>
203+ protected virtual void DrawItemHighlighted ( DrawItemEventArgs e , bool selected )
189204 {
205+ if ( VisualStyleRenderer . IsSupported )
206+ {
207+ VisualStyleElement elementToDraw = selected
208+ ? VisualStyleElement . CreateElement ( "Explorer::ListView" , 1 , ( int ) ListViewState . HotSelected )
209+ : VisualStyleElement . CreateElement ( "Explorer::ListView" , 1 , ( int ) ListViewState . Hot ) ;
210+
211+ if ( VisualStyleRenderer . IsElementDefined ( elementToDraw ) )
212+ {
213+ new VisualStyleRenderer ( elementToDraw ) . DrawBackground ( e . Graphics , e . Bounds ) ;
214+ return ;
215+ }
216+ }
217+
190218 using ( Pen backPen = new Pen ( Color . FromArgb ( 25 , SystemColors . Highlight ) ) )
191219 {
192220 e . Graphics . FillRectangle (
193221 backPen . Brush
194222 , 0
195223 , e . Bounds . Top
196- , e . Bounds . Width - 0
224+ , e . Bounds . Width - 0
197225 , e . Bounds . Height - 0 ) ;
198226
199227 using ( Pen highlightPen = new Pen ( Color . FromArgb ( 100 , backPen . Color ) ) )
@@ -202,7 +230,7 @@ protected virtual void DrawItemHighlighted(DrawItemEventArgs e)
202230 highlightPen
203231 , 0
204232 , e . Bounds . Top
205- , e . Bounds . Width - 1
233+ , e . Bounds . Width - 1
206234 , e . Bounds . Height - 1 ) ;
207235 }
208236 }
@@ -212,15 +240,30 @@ protected virtual void DrawItemHighlighted(DrawItemEventArgs e)
212240 /// Handles the item drawing of a selected item.
213241 /// </summary>
214242 /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data.</param>
215- protected virtual void DrawItemSelected ( DrawItemEventArgs e )
243+ /// <param name="hover">Determines whether the cursor is currently hovering over the item.</param>
244+ protected virtual void DrawItemSelected ( DrawItemEventArgs e , bool hover )
216245 {
246+ if ( VisualStyleRenderer . IsSupported )
247+ {
248+ VisualStyleElement elementToDraw = hover
249+ ? VisualStyleElement . CreateElement ( "Explorer::ListView" , 1 , ( int ) ListViewState . HotSelected )
250+ : VisualStyleElement . CreateElement ( "Explorer::ListView" , 1 , ( int ) ListViewState . Selected ) ;
251+
252+ if ( VisualStyleRenderer . IsElementDefined ( elementToDraw ) )
253+ {
254+
255+ new VisualStyleRenderer ( elementToDraw ) . DrawBackground ( e . Graphics , e . Bounds ) ;
256+ return ;
257+ }
258+ }
259+
217260 using ( Pen backPen = new Pen ( Color . FromArgb ( 50 , SystemColors . Highlight ) ) )
218261 {
219262 e . Graphics . FillRectangle (
220263 backPen . Brush
221264 , 0
222265 , e . Bounds . Top
223- , e . Bounds . Width - 0
266+ , e . Bounds . Width - 0
224267 , e . Bounds . Height - 0 ) ;
225268
226269 using ( Pen highlightPen = new Pen ( Color . FromArgb ( 200 , backPen . Color ) ) )
@@ -229,7 +272,7 @@ protected virtual void DrawItemSelected(DrawItemEventArgs e)
229272 highlightPen
230273 , 0
231274 , e . Bounds . Top
232- , e . Bounds . Width - 1
275+ , e . Bounds . Width - 1
233276 , e . Bounds . Height - 1 ) ;
234277 }
235278 }
@@ -289,11 +332,11 @@ protected override void OnDrawItem(DrawItemEventArgs e)
289332 }
290333
291334 bool isSelected = ( e . State & DrawItemState . Selected ) == DrawItemState . Selected ;
292- bool isHover = ! isSelected && mMouseIndex != - 1 && e . Index == mMouseIndex ;
335+ bool isHover = mMouseIndex != - 1 && e . Index == mMouseIndex ;
293336
294337 if ( isSelected )
295338 {
296- DrawItemSelected ( e ) ;
339+ DrawItemSelected ( e , isHover ) ;
297340
298341 if ( Focused && ShowFocusCues )
299342 {
@@ -307,7 +350,7 @@ protected override void OnDrawItem(DrawItemEventArgs e)
307350
308351 if ( isHover )
309352 {
310- DrawItemHighlighted ( e ) ;
353+ DrawItemHighlighted ( e , isSelected ) ;
311354 }
312355
313356 DrawItemText ( e ) ;
@@ -357,10 +400,10 @@ protected override void OnPaint(PaintEventArgs e)
357400 if ( GetStyle ( ControlStyles . UserPaint ) )
358401 {
359402 Message m = new Message ( ) ;
360- m . HWnd = Handle ;
361- m . Msg = WM_PRINTCLIENT ;
362- m . WParam = e . Graphics . GetHdc ( ) ;
363- m . LParam = ( IntPtr ) PRF_CLIENT ;
403+ m . HWnd = Handle ;
404+ m . Msg = WM_PRINTCLIENT ;
405+ m . WParam = e . Graphics . GetHdc ( ) ;
406+ m . LParam = ( IntPtr ) PRF_CLIENT ;
364407
365408 DefWndProc ( ref m ) ;
366409 e . Graphics . ReleaseHdc ( m . WParam ) ;
0 commit comments