Catch mouse events from Listbox
Posted: Wed Dec 11, 2013 3:56 pm
I'm trying to make a listbox control scroll when a user clicks and drags anywhere on the control. Everything works (I used buttons to simulate mouse movement) except for some reason the listbox control never raises the mousedown, mousemove, or mouseup events. Is there something else I can use to capture mouse events or how can I get the listbox to fire the events?
Listbox control code:
Listbox control code:
Code: Select all
ListDrag listDrag = new ListDrag();
void Screen_Opened(System.Object sender, System.EventArgs e)
{
this.m_List.MouseMove += new System.Windows.Forms.MouseEventHandler(List_MouseMove);
}
void List_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
listDrag.BeginDrag(this.m_List, e.Y);
}
void List_MouseMove(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
if (listDrag.DragActive)
listDrag.DoDrag(this.m_List, e.Y);
}
void List_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
if (listDrag.DragActive)
{
listDrag.DoDrag(this.m_List, e.Y);
listDrag.EndDrag();
}
}
private class ListDrag
{
#region Externals
[DllImport("coredll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
private const int SB_HORZ = 0;
private const int SB_VERT = 1;
private const int SIF_RANGE = 0x1;
private const int SIF_PAGE = 0x2;
private const int SIF_POS = 0x4;
private const int SIF_DISABLENOSCROLL = 0x8;
private const int SIF_TRACKPOS = 0x10;
private const int SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS;
private struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
private SCROLLINFO GetInfo(IntPtr Handle)
{
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)SIF_ALL;
if (GetScrollInfo(Handle, SB_VERT, ref si))
{
if (si.nPage == 0)
{
si.nMin = 0;
si.nMax = 0;
si.nPos = 0;
}
else
{
int size = (int)si.nPage - 1;
if (size < (si.nMax - si.nMin))
si.nMax -= size;
else
si.nMax = si.nMin;
}
}
else
{
si.nMin = 0;
si.nMax = 0;
si.nPos = 0;
si.nPage = 0;
}
return si;
}
#endregion
private bool Drag;
private int dragY;
private int dragPos;
public ListDrag()
{
Drag = false;
}
public bool DragActive
{
get { return Drag; }
}
public bool BeginDrag(Neo.ApplicationFramework.Controls.Controls.ListBox listBox, int Y)
{
if (Drag) return false;
dragY = Y;
dragPos = GetInfo(listBox.Handle).nPos;
Drag = true;
return true;
}
public bool DoDrag(Neo.ApplicationFramework.Controls.Controls.ListBox listBox, int Y)
{
if (!Drag) return false;
SCROLLINFO si = GetInfo(listBox.Handle);
if (si.nPage == 0) return true;
int startPos = GetPos(listBox, (int)si.nPage, dragY);
int endPos = GetPos(listBox, (int)si.nPage, Y);
int pos = dragPos + startPos - endPos;
if (pos < si.nMin)
pos = si.nMin;
else if (pos > si.nMax)
pos = si.nMax;
listBox.TopIndex = pos;
return true;
}
public void EndDrag()
{
Drag = false;
}
private int GetPos(Neo.ApplicationFramework.Controls.Controls.ListBox listBox, int nPage, int Y)
{
int pos;
pos = Y - listBox.ClientRectangle.Y;
pos *= nPage;
pos /= listBox.ClientRectangle.Height;
if (Y < listBox.ClientRectangle.Y) pos--;
return pos;
}
}