- //COPYRIGHT Original author ....
-
- public class NotResizableListView: ListView
- {
- #region Interop
- #region Consts
- const int LVM_FIRST = 0x1000;
- const int LVM_GETHEADER = LVM_FIRST + 31;
- const int HDM_FIRST = 0x1200;
- const int HDM_HITTEST = HDM_FIRST + 6;
- const int HHT_ONDIVIDER = 0x0004;
- const int WM_SETCURSOR = 0x0020;
- const int WM_LBUTTONDOWN = 0x0201;
- #endregion Consts
-
- [StructLayout(LayoutKind.Sequential)]
- struct HDHITTESTINFO
- {
- public Point pt;
- public int flags;
- public int iItem;
- }
- [DllImport("User32.dll")]
- static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
-
- #endregion Interop
-
- protected override void OnHandleCreated(EventArgs e)
- {
- base.OnHandleCreated(e);
- //Recupere l'objet columnheader.
- IntPtr hHeader = (IntPtr)SendMessage(Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
- ColumnHeader nativeWindow = new ColumnHeader(hHeader);
- }
-
- private class ColumnHeader : NativeWindow
- {
- public ColumnHeader(IntPtr hHeader)
- {
- this.AssignHandle(hHeader);
- }
-
- protected override void WndProc(ref Message m)
- {
- switch(m.Msg)
- {
- case WM_SETCURSOR:
- // Pas de changement de curseur
- return;
- case WM_LBUTTONDOWN:
- Int16 x = (Int16)m.LParam;
- Int16 y = (Int16)((int)m.LParam >> 16);
- // Recuepere la position du cursor
- Point cursorPosition = new Point(x, y);
- HDHITTESTINFO hitInfo = new HDHITTESTINFO();
- hitInfo.pt = cursorPosition;
- IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(hitInfo));
- Marshal.StructureToPtr(hitInfo, buffer, true);
- //Declenche le hit, permet l'execution des autres actions lié au click sur le header
- SendMessage(m.HWnd, HDM_HITTEST, IntPtr.Zero, buffer);
- hitInfo = (HDHITTESTINFO)Marshal.PtrToStructure(buffer, typeof(HDHITTESTINFO));
- Marshal.FreeHGlobal(buffer);
- if (hitInfo.flags == HHT_ONDIVIDER)
- //On est sur le separateur donc pas d'action
- return;
- break;
- }
-
- base.WndProc(ref m);
- }
- }
-
- }
//COPYRIGHT Original author ....
public class NotResizableListView: ListView
{
#region Interop
#region Consts
const int LVM_FIRST = 0x1000;
const int LVM_GETHEADER = LVM_FIRST + 31;
const int HDM_FIRST = 0x1200;
const int HDM_HITTEST = HDM_FIRST + 6;
const int HHT_ONDIVIDER = 0x0004;
const int WM_SETCURSOR = 0x0020;
const int WM_LBUTTONDOWN = 0x0201;
#endregion Consts
[StructLayout(LayoutKind.Sequential)]
struct HDHITTESTINFO
{
public Point pt;
public int flags;
public int iItem;
}
[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
#endregion Interop
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
//Recupere l'objet columnheader.
IntPtr hHeader = (IntPtr)SendMessage(Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
ColumnHeader nativeWindow = new ColumnHeader(hHeader);
}
private class ColumnHeader : NativeWindow
{
public ColumnHeader(IntPtr hHeader)
{
this.AssignHandle(hHeader);
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_SETCURSOR:
// Pas de changement de curseur
return;
case WM_LBUTTONDOWN:
Int16 x = (Int16)m.LParam;
Int16 y = (Int16)((int)m.LParam >> 16);
// Recuepere la position du cursor
Point cursorPosition = new Point(x, y);
HDHITTESTINFO hitInfo = new HDHITTESTINFO();
hitInfo.pt = cursorPosition;
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(hitInfo));
Marshal.StructureToPtr(hitInfo, buffer, true);
//Declenche le hit, permet l'execution des autres actions lié au click sur le header
SendMessage(m.HWnd, HDM_HITTEST, IntPtr.Zero, buffer);
hitInfo = (HDHITTESTINFO)Marshal.PtrToStructure(buffer, typeof(HDHITTESTINFO));
Marshal.FreeHGlobal(buffer);
if (hitInfo.flags == HHT_ONDIVIDER)
//On est sur le separateur donc pas d'action
return;
break;
}
base.WndProc(ref m);
}
}
}