如何禁用listview列调整大小?

时间:2010-02-12 08:55:36

标签: listview powershell

我想禁用listview列调整大小。我该怎么办?

2 个答案:

答案 0 :(得分:1)

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);

}

还可以查看此内容

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

答案 1 :(得分:1)

如果您希望某些列可调整大小而其他列不可调整大小,请使用ObjectListView。在该控件中,每列可以被赋予最大/最小宽度。有关说明,请参阅this recipe