Code clean up on Mainform and VirtuallistView.

This commit is contained in:
adelikat 2013-12-20 22:05:56 +00:00
parent cc23c120c0
commit 1c44d21562
2 changed files with 827 additions and 512 deletions

View File

@ -1,105 +1,105 @@
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace BizHawk.Client.EmuHawk
{
#region win32interop
[StructLayout(LayoutKind.Sequential)]
internal struct LVDISPINFO {
public NMHDR hdr;
public LVITEM item;
internal struct LvDispInfo
{
public NmHdr Hdr;
public LvItem Item;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NMHDR {
public int hwndFrom;
public int idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NMITEMACTIVATE {
NMHDR hdr;
int iItem;
int iSubItem;
uint uNewState;
uint uOldState;
uint uChanged;
POINT ptAction;
uint lParam;
uint uKeyFlags;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECT {
public int left, top, right, bottom;
internal struct NmHdr
{
public int HwndFrom;
public int IdFrom;
public int Code;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NMCUSTOMDRAWINFO {
public NMHDR hdr;
internal struct NmItemActivate
{
public NmHdr Hdr;
public int Item;
public int SubItem;
public uint NewState;
public uint OldState;
public uint uChanged;
public POINT Action;
public uint lParam;
public uint KeyFlags;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int Top;
public int Left;
public int Bottom;
public int Right;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NmCustomDrawInfo
{
public NmHdr Hdr;
public uint dwDrawStage;
public IntPtr hdc;
public RECT rc;
public IntPtr Hdc;
public RECT Rect;
public int dwItemSpec;
public uint uItemState;
public uint ItemState;
public int lItemlParam;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NMLVCUSTOMDRAW {
public NMCUSTOMDRAWINFO nmcd;
public int clrText;
public int clrTextBk;
public int iSubItem;
internal struct NmLvCustomDraw
{
public NmCustomDrawInfo Nmcd;
public int ClearText;
public int ClearTextBackground;
public int SubItem;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct LVITEM {
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
internal struct LvItem
{
public uint Mask;
public int Item;
public int SubItem;
public uint State;
public uint StateMask;
public IntPtr PszText;
public int cchTextMax;
public int iImage;
public int Image;
public IntPtr lParam;
//#if (_WIN32_IE >= 0x0300)
public int iIndent;
//#endif
//#if (_WIN32_WINNT >= 0x501)
//public int iGroupId;
//public uint cColumns;
//public IntPtr puColumns;
//#endif
public int Indent;
}
[FlagsAttribute]
enum CUSTOMDRAWRETURNFLAGS {
internal enum CustomDrawReturnFlags
{
CDRF_DODEFAULT = 0x00000000,
CDRF_NEWFONT = 0x00000002,
CDRF_SKIPDEFAULT = 0x00000004,
CDRF_NOTIFYPOSTPAINT = 0x00000010,
CDRF_NOTIFYITEMDRAW = 0x00000020,
//#if (_WIN32_IE >= 0x0400)
CDRF_NOTIFYSUBITEMDRAW = 0x00000020,//[sic]
//#endif
CDRF_NOTIFYSUBITEMDRAW = 0x00000020,
CDRF_NOTIFYPOSTERASE = 0x00000040,
}
[FlagsAttribute]
enum CUSTOMDRAWDRAWSTAGEFLAGS {
internal enum CustomDrawDrawStageFlags
{
CDDS_PREPAINT = 0x00000001,
CDDS_POSTPAINT = 0x00000002,
CDDS_PREERASE = 0x00000003,
@ -111,17 +111,16 @@ namespace BizHawk.Client.EmuHawk
CDDS_ITEMPOSTPAINT = (CDDS_ITEM | CDDS_POSTPAINT),
CDDS_ITEMPREERASE = (CDDS_ITEM | CDDS_PREERASE),
CDDS_ITEMPOSTERASE = (CDDS_ITEM | CDDS_POSTERASE),
//#if (_WIN32_IE >= 0x0400)
CDDS_SUBITEM = 0x00020000,
CDDS_SUBITEMPREPAINT = (CDDS_SUBITEM | CDDS_ITEMPREPAINT),
CDDS_SUBITEMPOSTPAINT = (CDDS_SUBITEM | CDDS_ITEMPOSTPAINT),
CDDS_SUBITEMPREERASE = (CDDS_SUBITEM | CDDS_ITEMPREERASE),
CDDS_SUBITEMPOSTERASE = (CDDS_SUBITEM | CDDS_ITEMPOSTERASE),
//#endif
}
[FlagsAttribute]
enum LVHITTESTFLAGS {
internal enum LvHitTestFlags
{
LVHT_NOWHERE = 0x0001,
LVHT_ONITEMICON = 0x0002,
LVHT_ONITEMLABEL = 0x0004,
@ -135,22 +134,25 @@ namespace BizHawk.Client.EmuHawk
}
[StructLayout(LayoutKind.Sequential)]
internal struct POINT {
public int x;
public int y;
internal struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
internal class LVHITTESTINFO {
public POINT pt;
public uint flags;
public int iItem;
public int iSubItem;
internal class LvHitTestInfo
{
public POINT Point;
public uint Flags;
public int Item;
public int SubItem;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NMLISTVIEW {
public NMHDR hdr;
internal struct NMLISTVIEW
{
public NmHdr hdr;
public int iItem;
public int iSubItem;
public uint uNewState;
@ -161,7 +163,8 @@ namespace BizHawk.Client.EmuHawk
}
internal enum ListViewItemMask : short {
internal enum ListViewItemMask : short
{
LVIF_TEXT = 0x0001,
LVIF_IMAGE = 0x0002,
LVIF_PARAM = 0x0004,
@ -173,7 +176,8 @@ namespace BizHawk.Client.EmuHawk
LVIF_COLUMNS = 0x0200
}
internal enum LVNI {
internal enum LvNi
{
ALL = 0x0000,
FOCUSED = 0x0001,
SELECTED = 0x0002,
@ -186,7 +190,8 @@ namespace BizHawk.Client.EmuHawk
TORIGHT = 0x0800
}
internal enum ListViewMessages {
internal enum ListViewMessages
{
LVM_FIRST = 0x1000,
LVM_GETITEMCOUNT = (LVM_FIRST + 4),
LVM_SETCALLBACKMASK = (LVM_FIRST + 11),
@ -199,7 +204,8 @@ namespace BizHawk.Client.EmuHawk
LVM_GETSUBITEMRECT = (LVM_FIRST + 56)
}
internal enum ListViewStyles : short {
internal enum ListViewStyles : short
{
LVS_OWNERDATA = 0x1000,
LVS_SORTASCENDING = 0x0010,
LVS_SORTDESCENDING = 0x0020,
@ -207,12 +213,15 @@ namespace BizHawk.Client.EmuHawk
LVS_NOLABELWRAP = 0x0080,
LVS_AUTOARRANGE = 0x0100
}
internal enum ListViewStylesICF : uint {
internal enum ListViewStylesICF : uint
{
LVSICF_NOINVALIDATEALL = 0x00000001,
LVSICF_NOSCROLL = 0x00000002
}
internal enum WindowsMessage : uint {
internal enum WindowsMessage : uint
{
WM_ERASEBKGND = 0x0014,
WM_SCROLL = 0x115,
WM_LBUTTONDOWN = 0x0201,
@ -227,28 +236,31 @@ namespace BizHawk.Client.EmuHawk
WM_REFLECT = WM_USER + 0x1c00
}
internal enum Notices : int {
internal enum Notices
{
NM_FIRST = 0,
NM_CUSTOMDRAW = NM_FIRST - 12,
NM_CLICK = NM_FIRST - 2,
NM_DBLCLICK = NM_FIRST - 3,
}
internal enum ListViewNotices : int {
internal enum ListViewNotices
{
LVN_FIRST = (0 - 100),
LVN_LAST = (0 - 199),
LVN_BEGINDRAG = (int)LVN_FIRST - 9,
LVN_BEGINRDRAG = (int)LVN_FIRST - 11,
LVN_GETDISPINFOA = (int)LVN_FIRST - 50,
LVN_GETDISPINFOW = (int)LVN_FIRST - 77,
LVN_SETDISPINFOA = (int)LVN_FIRST - 51,
LVN_SETDISPINFOW = (int)LVN_FIRST - 78,
LVN_ODCACHEHINT = (int)LVN_FIRST - 13,
LVN_ODFINDITEMW = (int)LVN_FIRST - 79
LVN_BEGINDRAG = LVN_FIRST - 9,
LVN_BEGINRDRAG = LVN_FIRST - 11,
LVN_GETDISPINFOA = LVN_FIRST - 50,
LVN_GETDISPINFOW = LVN_FIRST - 77,
LVN_SETDISPINFOA = LVN_FIRST - 51,
LVN_SETDISPINFOW = LVN_FIRST - 78,
LVN_ODCACHEHINT = LVN_FIRST - 13,
LVN_ODFINDITEMW = LVN_FIRST - 79
}
[Flags]
internal enum ListViewCallBackMask : uint {
internal enum ListViewCallBackMask : uint
{
LVIS_FOCUSED = 0x0001,
LVIS_SELECTED = 0x0002,
LVIS_CUT = 0x0004,
@ -260,10 +272,10 @@ namespace BizHawk.Client.EmuHawk
LVIS_STATEIMAGEMASK = 0xF000,
}
#endregion
#region VirtualListView Delegates
/// <summary>
/// Retrieve the background color for a Listview cell (item and subitem).
/// </summary>
@ -272,7 +284,6 @@ namespace BizHawk.Client.EmuHawk
/// <param name="color">Background color to use</param>
public delegate void QueryItemBkColorHandler(int item, int subItem, ref Color color);
/// <summary>
/// Retrieve the text for a Listview cell (item and subitem).
/// </summary>
@ -298,6 +309,7 @@ namespace BizHawk.Client.EmuHawk
public delegate void QueryItemIndentHandler(int item, out int itemIndent);
public delegate void QueryItemHandler(int idx, out ListViewItem item);
#endregion
/// <summary>
@ -305,11 +317,13 @@ namespace BizHawk.Client.EmuHawk
/// to be displayed. The virtual Listview contains very little actual information -
/// mainly item selection and focus information.
/// </summary>
public class VirtualListView : ListView {
public class VirtualListView : ListView
{
// store the item count to prevent the call to SendMessage(LVM_GETITEMCOUNT)
private int itemCount = 0;
private int _itemCount;
#region Display query callbacks
/// <summary>
/// Fire the QueryItemBkColor event which requests the background color for the passed Listview cell
/// </summary>
@ -320,66 +334,86 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
[Category("Data")]
public event QueryItemTextHandler QueryItemText;
/// <summary>
/// Fire the QueryItemImage event which requests the ImageIndex for the passed Listview item.
/// </summary>
[Category("Data")]
public event QueryItemImageHandler QueryItemImage;
/// <summary>
/// Fire the QueryItemIndent event which requests the indent for the passed Listview item.
/// </summary>
[Category("Data")]
public event QueryItemIndentHandler QueryItemIndent;
[Category("Data")]
public event QueryItemHandler QueryItem;
#endregion
#region Properties
/// <summary>
/// Gets/sets the sets the virtual number of items to be displayed.
/// Gets or sets the sets the virtual number of items to be displayed.
/// </summary>
[Category("Behavior")]
public int ItemCount {
get {
return itemCount;
public int ItemCount
{
get
{
return _itemCount;
}
set {
itemCount = value;
// if the virtual item count is set before the handle is created
set
{
_itemCount = value;
// If the virtual item count is set before the handle is created
// then the image lists don't get loaded properly
if(!this.IsHandleCreated)
if (!IsHandleCreated)
{
return;
}
SetVirtualItemCount();
}
}
/// <summary>
/// Gets/sets how list items are to be displayed.
///
/// Gets or sets how list items are to be displayed.
/// Hide the ListView.View property.
/// Virtual Listviews only allow Details or List.
/// </summary>
public new System.Windows.Forms.View View {
get {
public new View View
{
get
{
return base.View;
}
set {
if(value == View.LargeIcon ||
value == View.SmallIcon) {
set
{
if (value == View.LargeIcon ||
value == View.SmallIcon)
{
throw new ArgumentException("Icon views are invalid for virtual ListViews", "View");
}
base.View = value;
}
}
/// <summary>
/// Gets the required creation parameters when the control handle is created.
///
/// Use LVS_OWNERDATA to set this as a virtual Listview.
/// </summary>
protected override System.Windows.Forms.CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
// LVS_OWNERDATA style must be set when the control is created
cp.Style |= (int)ListViewStyles.LVS_OWNERDATA;
return cp;
@ -389,22 +423,26 @@ namespace BizHawk.Client.EmuHawk
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="VirtualListView"/> class.
/// Create a new instance of this control.
/// </summary>
public VirtualListView() {
public VirtualListView()
{
// virtual listviews must be Details or List view with no sorting
View = View.Details;
Sorting = SortOrder.None;
ptrlvhti = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LVHITTESTINFO)));
ptrlvhti = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LvHitTestInfo)));
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
~VirtualListView() {
~VirtualListView()
{
Marshal.FreeHGlobal(ptrlvhti);
}
@ -416,110 +454,136 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
/// <param name="index">Listview item's index.</param>
/// <param name="selected">Select the passed item?</param>
public void SelectItem(int index, bool selected) {
IntPtr ptrItem = IntPtr.Zero;
public void SelectItem(int index, bool selected)
{
var ptrItem = IntPtr.Zero;
try {
try
{
// Determine whether selecting or unselecting.
uint select = selected ? (uint)ListViewCallBackMask.LVIS_SELECTED : 0;
// Fill in the LVITEM structure with state fields.
LVITEM stateItem = new LVITEM();
stateItem.mask = (uint)ListViewItemMask.LVIF_STATE;
stateItem.iItem = index;
stateItem.iSubItem = 0;
stateItem.state = select;
stateItem.stateMask = (uint)ListViewCallBackMask.LVIS_SELECTED;
var stateItem = new LvItem
{
Mask = (uint)ListViewItemMask.LVIF_STATE,
Item = index,
SubItem = 0,
State = @select,
StateMask = (uint)ListViewCallBackMask.LVIS_SELECTED
};
// Copy the structure to unmanaged memory.
ptrItem = Marshal.AllocHGlobal(Marshal.SizeOf(stateItem.GetType()));
Marshal.StructureToPtr(stateItem, ptrItem, true);
// Send the message to the control window.
int result = Win32.SendMessage(
Win32.SendMessage(
this.Handle,
(int)ListViewMessages.LVM_SETITEMSTATE,
index,
ptrItem.ToInt32());
} catch(Exception ex) {
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("VirtualListView.SetItemState error=" + ex.Message);
// TODO: should this eat any exceptions?
throw ex;
} finally {
throw;
}
finally
{
// Always release the unmanaged memory.
if(ptrItem != IntPtr.Zero) {
if (ptrItem != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptrItem);
}
}
}
private void SetVirtualItemCount() {
int result;
result = Win32.SendMessage(
private void SetVirtualItemCount()
{
Win32.SendMessage(
this.Handle,
(int)ListViewMessages.LVM_SETITEMCOUNT,
itemCount,
this._itemCount,
0);
}
protected void OnDispInfoNotice(ref Message m, bool useAnsi) {
protected void OnDispInfoNotice(ref Message m, bool useAnsi)
{
var info = (LvDispInfo)m.GetLParam(typeof(LvDispInfo));
LVDISPINFO info = (LVDISPINFO)m.GetLParam(typeof(LVDISPINFO));
string lvtext = null;
if((info.item.mask & (uint)ListViewItemMask.LVIF_TEXT) > 0) {
if(QueryItemText != null) {
QueryItemText(info.item.iItem, info.item.iSubItem, out lvtext);
if(lvtext != null) {
try {
int maxIndex = Math.Min(info.item.cchTextMax - 1, lvtext.Length);
char[] data = new char[maxIndex + 1];
if ((info.Item.Mask & (uint)ListViewItemMask.LVIF_TEXT) > 0)
{
if (QueryItemText != null)
{
string lvtext;
QueryItemText(info.Item.Item, info.Item.SubItem, out lvtext);
if (lvtext != null)
{
try
{
int maxIndex = Math.Min(info.Item.cchTextMax - 1, lvtext.Length);
var data = new char[maxIndex + 1];
lvtext.CopyTo(0, data, 0, lvtext.Length);
data[maxIndex] = '\0';
System.Runtime.InteropServices.Marshal.Copy(data, 0, info.item.pszText, data.Length);
} catch(Exception e) {
Debug.WriteLine("Failed to copy text name from client: " + e.ToString(), "VirtualListView.OnDispInfoNotice");
Marshal.Copy(data, 0, info.Item.PszText, data.Length);
}
catch (Exception e)
{
Debug.WriteLine("Failed to copy text name from client: " + e, "VirtualListView.OnDispInfoNotice");
}
}
}
}
if((info.item.mask & (uint)ListViewItemMask.LVIF_IMAGE) > 0) {
if ((info.Item.Mask & (uint)ListViewItemMask.LVIF_IMAGE) > 0)
{
int imageIndex = 0;
if(QueryItemImage != null) {
QueryItemImage(info.item.iItem, info.item.iSubItem, out imageIndex);
if (QueryItemImage != null)
{
QueryItemImage(info.Item.Item, info.Item.SubItem, out imageIndex);
}
info.item.iImage = imageIndex;
System.Runtime.InteropServices.Marshal.StructureToPtr(info, m.LParam, false);
info.Item.Image = imageIndex;
Marshal.StructureToPtr(info, m.LParam, false);
}
if((info.item.mask & (uint)ListViewItemMask.LVIF_INDENT) > 0) {
if ((info.Item.Mask & (uint)ListViewItemMask.LVIF_INDENT) > 0)
{
int itemIndent = 0;
if(QueryItemIndent != null) {
QueryItemIndent(info.item.iItem, out itemIndent);
if (QueryItemIndent != null)
{
QueryItemIndent(info.Item.Item, out itemIndent);
}
info.item.iIndent = itemIndent;
System.Runtime.InteropServices.Marshal.StructureToPtr(info, m.LParam, false);
info.Item.Indent = itemIndent;
Marshal.StructureToPtr(info, m.LParam, false);
}
m.Result = new IntPtr(0);
}
protected void OnCustomDrawNotice(ref System.Windows.Forms.Message m) {
NMLVCUSTOMDRAW cd = (NMLVCUSTOMDRAW)m.GetLParam(typeof(NMLVCUSTOMDRAW));
switch(cd.nmcd.dwDrawStage) {
case (int)CUSTOMDRAWDRAWSTAGEFLAGS.CDDS_ITEMPREPAINT:
case (int)CUSTOMDRAWDRAWSTAGEFLAGS.CDDS_PREPAINT:
m.Result = new IntPtr((int)CUSTOMDRAWRETURNFLAGS.CDRF_NOTIFYSUBITEMDRAW);
protected void OnCustomDrawNotice(ref Message m)
{
var cd = (NmLvCustomDraw)m.GetLParam(typeof(NmLvCustomDraw));
switch (cd.Nmcd.dwDrawStage)
{
case (int)CustomDrawDrawStageFlags.CDDS_ITEMPREPAINT:
case (int)CustomDrawDrawStageFlags.CDDS_PREPAINT:
m.Result = new IntPtr((int)CustomDrawReturnFlags.CDRF_NOTIFYSUBITEMDRAW);
break;
case (int)CUSTOMDRAWDRAWSTAGEFLAGS.CDDS_SUBITEMPREPAINT:
Color color;
if(QueryItemBkColor != null) {
color = Color.FromArgb(cd.clrTextBk & 0xFF, (cd.clrTextBk >> 8) & 0xFF, (cd.clrTextBk >> 16) & 0xFF);
QueryItemBkColor(cd.nmcd.dwItemSpec, cd.iSubItem, ref color);
cd.clrTextBk = (color.B << 16) | (color.G << 8) | (color.R);
System.Runtime.InteropServices.Marshal.StructureToPtr(cd, m.LParam, false);
case (int)CustomDrawDrawStageFlags.CDDS_SUBITEMPREPAINT:
if (QueryItemBkColor != null)
{
var color = Color.FromArgb(cd.ClearTextBackground & 0xFF, (cd.ClearTextBackground >> 8) & 0xFF, (cd.ClearTextBackground >> 16) & 0xFF);
QueryItemBkColor(cd.Nmcd.dwItemSpec, cd.SubItem, ref color);
cd.ClearTextBackground = (color.B << 16) | (color.G << 8) | color.R;
Marshal.StructureToPtr(cd, m.LParam, false);
}
m.Result = new IntPtr((int)CUSTOMDRAWRETURNFLAGS.CDRF_DODEFAULT);
m.Result = new IntPtr((int)CustomDrawReturnFlags.CDRF_DODEFAULT);
break;
}
}
@ -530,28 +594,33 @@ namespace BizHawk.Client.EmuHawk
public event ScrollEventHandler Scroll;
protected virtual void OnScroll(ScrollEventArgs e)
{
ScrollEventHandler handler = this.Scroll;
if (handler != null) handler(this, e);
var handler = this.Scroll;
if (handler != null)
{
handler(this, e);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetScrollPos(IntPtr hWnd, System.Windows.Forms.Orientation nBar);
public static extern int GetScrollPos(IntPtr hWnd, Orientation nBar);
/// <summary>
/// Gets and Sets the Vertical Scroll position of the control.
/// </summary>
public int VScrollPos
{
get { return GetScrollPos((IntPtr)this.Handle, System.Windows.Forms.Orientation.Vertical); }
get { return GetScrollPos(this.Handle, Orientation.Vertical); }
}
protected override void WndProc(ref System.Windows.Forms.Message m) {
NMHDR nm1;
bool messageProcessed = false;
switch(m.Msg) {
protected override void WndProc(ref Message m)
{
var messageProcessed = false;
switch (m.Msg)
{
case (int)WindowsMessage.WM_REFLECT + (int)WindowsMessage.WM_NOTIFY:
nm1 = (NMHDR)m.GetLParam(typeof(NMHDR));
switch(nm1.code) {
var nm1 = (NmHdr)m.GetLParam(typeof(NmHdr));
switch (nm1.Code)
{
case (int)Notices.NM_CUSTOMDRAW:
OnCustomDrawNotice(ref m);
messageProcessed = true;
@ -568,138 +637,142 @@ namespace BizHawk.Client.EmuHawk
OnBeginItemDrag(MouseButtons.Right, ref m);
messageProcessed = true;
break;
//case (int)Notices.NM_CLICK:
//case (int)Notices.NM_DBLCLICK:
// {
// NMITEMACTIVATE test = (NMITEMACTIVATE)m.GetLParam(typeof(NMITEMACTIVATE));
// messageProcessed = true;
// break;
// }
default:
break;
}
break;
break;
case (int)WindowsMessage.WM_SCROLL:
//http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp
// http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp
OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), m.WParam.ToInt32()));
break;
//obscure message loop flakiness when exceptions are thrown from the message loop...
//THIS BREAKS PROPER LISTVIEW FOCUS SELECTION (blue)
//next time we run into this, debug it better.
//case (int)WindowsMessage.WM_SETFOCUS:
// if (SelectedIndices.Count > 0 && SelectedIndices[0] >= VirtualListSize)
// messageProcessed = true;
// break;
//TRY THIS HACK INSTEAD TO FIX THE EXCEPTION
// (( NOPE, THEY ALL BREAK ONE THING OR ANOTHER. WINFORMS LISTVIEW JUST THROWS EXCEPTIONS, WHAT CAN WE DO? ))
//case 0x100C:
// {
// base.WndProc(ref m);
// int num = m.Result.ToInt32();
// messageProcessed = true;
// m.Result = new IntPtr(-1);
// break;
// }
//case 0x1012:
// {
// base.WndProc(ref m);
// int num = m.Result.ToInt32();
// messageProcessed = true;
// m.Result = new IntPtr(-1);
// break;
// }
case (int)WindowsMessage.WM_ERASEBKGND:
if (BlazingFast)
{
messageProcessed = true;
m.Result = new IntPtr(1);
}
break;
default:
break;
}
if(!messageProcessed) {
try {
if (!messageProcessed)
{
try
{
base.WndProc(ref m);
} catch(Exception ex) {
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("Message {0} caused an exception: {1}", m, ex.Message));
}
}
}
public bool BlazingFast = false;
public bool BlazingFast { get; set; }
protected ListViewItem GetItem(int idx) {
protected ListViewItem GetItem(int idx)
{
ListViewItem item = null;
if(QueryItem != null) {
if (QueryItem != null)
{
QueryItem(idx, out item);
}
if(item == null) {
throw new ArgumentException("cannot find item " + idx.ToString() + " via QueryItem event");
if (item == null)
{
throw new ArgumentException("cannot find item " + idx + " via QueryItem event");
}
return item;
}
protected void OnBeginItemDrag(MouseButtons mouseButton, ref System.Windows.Forms.Message m) {
NMLISTVIEW info = (NMLISTVIEW)m.GetLParam(typeof(NMLISTVIEW));
protected void OnBeginItemDrag(MouseButtons mouseButton, ref Message m)
{
var info = (NMLISTVIEW)m.GetLParam(typeof(NMLISTVIEW));
ListViewItem item = null;
if(QueryItem != null) {
if (QueryItem != null)
{
QueryItem(info.iItem, out item);
}
OnItemDrag(new ItemDragEventArgs(mouseButton, item));
}
protected override void OnHandleCreated(System.EventArgs e) {
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// ensure the value for ItemCount is sent to the control properly if the user set it
// before the handle was created
SetVirtualItemCount();
}
protected override void OnHandleDestroyed(System.EventArgs e) {
protected override void OnHandleDestroyed(EventArgs e)
{
// the ListView OnHandleDestroyed accesses the Items list for all selected items
ItemCount = 0;
base.OnHandleDestroyed(e);
}
#endregion
LVHITTESTINFO lvhti = new LVHITTESTINFO();
LvHitTestInfo lvhti = new LvHitTestInfo();
IntPtr ptrlvhti;
int selection = -1;
public int hitTest(int x, int y) {
lvhti.pt.x = x;
lvhti.pt.y = y;
public int hitTest(int x, int y)
{
lvhti.Point.X = x;
lvhti.Point.Y = y;
Marshal.StructureToPtr(lvhti, ptrlvhti, true);
int z = Win32.SendMessage(this.Handle, (int)ListViewMessages.LVM_HITTEST, 0, ptrlvhti.ToInt32());
Marshal.PtrToStructure(ptrlvhti, lvhti);
return z;
}
public void ensureVisible(int index) {
public void ensureVisible(int index)
{
Win32.SendMessage(Handle, (int)ListViewMessages.LVM_ENSUREVISIBLE, index, 1);
}
public void ensureVisible() {
public void ensureVisible()
{
ensureVisible(selectedItem);
}
public void setSelection(int index) {
public void setSelection(int index)
{
clearSelection();
selection = index;
SelectItem(selection, true);
}
public int selectedItem { get { if(SelectedIndices.Count == 0) return -1; else return SelectedIndices[0]; } set { setSelection(value); } }
public int selectedItem
{
get
{
if (SelectedIndices.Count == 0)
{
return -1;
}
else
{
return SelectedIndices[0];
}
}
set
{
setSelection(value);
}
}
public void clearSelection() {
if(selection != -1)
public void clearSelection()
{
if (selection != -1)
{
SelectItem(selection, false);
}
selection = -1;
}
}

File diff suppressed because it is too large Load Diff