views:

424

answers:

2

I want to disable listview column resizing. How can i do it?

A: 
Add a new class to your project and paste the code shown below.  Build.  Drag the new control from the top of your toolbox onto a form. 

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

public class FixedColumnView : ListView { 
  private HeaderWindow mHeader; 
  public FixedColumnView() { 
    this.View = View.Details; 
  } 
  protected override void WndProc(ref Message m) { 
    Console.WriteLine(m.ToString()); 
    if (m.Msg == WM_DESTROY) { 
      // Un-subclass header control 
      if (mHeader != null) mHeader.ReleaseHandle(); 
      mHeader = null; 
    } 
    if (m.Msg == WM_NOTIFY) { 
      // Prevent dragging 
      NMHDR nm = (NMHDR)m.GetLParam(typeof(NMHDR)); 
      if (nm.code == HDN_BEGINTRACK) { 
        m.Result = (IntPtr)1; 
        return; 
      } 
    } 
    base.WndProc(ref m); 
    if (m.Msg == WM_CREATE) { 
      // Subclass the header control 
      IntPtr hWnd = SendMessage(this.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero); 
      if (mHeader == null || mHeader.Handle != hWnd) { 
        if (mHeader != null) mHeader.ReleaseHandle(); 
        else mHeader = new HeaderWindow(); 
        mHeader.AssignHandle(hWnd); 
      } 
    } 
  } 
  private class HeaderWindow : NativeWindow { 
    protected override void WndProc(ref Message m) { 
      if (m.Msg == WM_SETCURSOR) { 
        // Prevent cursor from changing 
        m.Result = (IntPtr)1; 
        return; 
      } 
      if (m.Msg == WM_LBUTTONDBLCLICK) { 
        // Prevent double-click from changing column 
        return; 
      } 
      base.WndProc(ref m); 
    } 
  } 
  // P/Invoke declarations 
  private const int WM_CREATE = 0x0001; 
  private const int WM_DESTROY = 0x0002; 
  private const int WM_SETCURSOR = 0x0020; 
  private const int WM_NOTIFY = 0x004e; 
  private const int WM_LBUTTONDBLCLICK = 0x0203; 
  private const int LVM_GETHEADER = 0x101f; 
  private const int HDN_FIRST = -300; 
  private const int HDN_BEGINTRACK = HDN_FIRST - 26; 
  [StructLayout(LayoutKind.Sequential)] 
  private struct NMHDR { 
    public int hWndFrom; 
    public int idFrom; 
    public int code; 
  } 
  [DllImport("user32.dll")] 
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

}

And also view this

http://www.codeproject.com/KB/cpp/LVHeaderSubclass.aspx

ratty
it doesnt help me much. i am using powershell.
Varyanica
+1  A: 

If you want some columns resizable and other not resizeable, use ObjectListView. In that control, each column can be given a Max/Min width. See this recipe for instructions.

Grammarian