Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Last active August 29, 2015 13:56
Show Gist options
  • Save MatthewKing/8839470 to your computer and use it in GitHub Desktop.
Save MatthewKing/8839470 to your computer and use it in GitHub Desktop.
ListViewWithoutFocus.cs
// Copyright Matthew King 2014.
// Licensed under the Boost Software License, Version 1.0.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
/// <summary>
/// Extends the System.Windows.Forms.ListView class, removing the focus indicators.
/// </summary>
class ListViewWithoutFocus : ListView
{
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls
/// the window procedure for the specified window and does not return until the window
/// procedure has processed the message.
/// </summary>
/// <param name="hWnd">A handle to the window that will receive the message.</param>
/// <param name="wMsg">The message to be sent.</param>
/// <param name="wParam">Additional message-specific information.</param>
/// <param name="lParam">Additional message-specific information.</param>
/// <returns>The result of the message processing.</returns>
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
/// <summary>
/// Raises the System.Windows.Forms.ListView.SelectedIndexChanged event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
this.HideFocusIndicators();
}
/// <summary>
/// Raises the System.Windows.Forms.Control.Enter event.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.HideFocusIndicators();
}
/// <summary>
/// Hide focus indicators.
/// </summary>
private void HideFocusIndicators()
{
// wMsg = 0x127 (WM_CHANGEUISTATE)
// wParam = 0x10001 (UIS_SET | UISF_HIDEFOCUS)
// low-order word UIS_SET (0x1), high-order word UISF_HIDEFOCUS (0x1)
SendMessage(this.Handle, 0x127, 0x10001, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment