Click here to Skip to main content
15,887,444 members
Home / Discussions / C#
   

C#

 
GeneralRe: To handle list view column width change Pin
machocr29-Apr-04 14:51
machocr29-Apr-04 14:51 
GeneralRe: To handle list view column width change Pin
machocr29-Apr-04 15:08
machocr29-Apr-04 15:08 
GeneralRe: To handle list view column width change Pin
Heath Stewart30-Apr-04 3:23
protectorHeath Stewart30-Apr-04 3:23 
GeneralRe: To handle list view column width change Pin
machocr30-Apr-04 4:58
machocr30-Apr-04 4:58 
GeneralRe: To handle list view column width change Pin
Heath Stewart30-Apr-04 5:18
protectorHeath Stewart30-Apr-04 5:18 
GeneralRe: To handle list view column width change Pin
machocr30-Apr-04 6:14
machocr30-Apr-04 6:14 
GeneralRe: To handle list view column width change Pin
Heath Stewart30-Apr-04 6:29
protectorHeath Stewart30-Apr-04 6:29 
GeneralRe: To handle list view column width change Pin
Heath Stewart30-Apr-04 5:39
protectorHeath Stewart30-Apr-04 5:39 
Actually, I have that backward: return TRUE to deny changes or FALSE to allow them. My bad. Also, something in base.WndProc is resetting the Message.Result back to 0, so you will have to call it first. Since I made a mistake, I'll post my modified source here:
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyFile(@"C:\Proplanner.NET\KeyFile.snk")]

public class Test : Form
{
  static void Main(string[] args)
  {
    bool b = true;
    if (args.Length > 0)
    {
      try
      {
        b = bool.Parse(args[0]);
      }
      catch {}
    }
    
    Application.Run(new Test(b));
  }

  public Test(bool allowResizing)
  {
    SuspendLayout();

    Text = "MyListView Test";

    MyListView lv = new MyListView();
    Controls.Add(lv);
    lv.Dock = DockStyle.Fill;
    lv.View = View.Details;
    lv.AllowResizing = allowResizing;
    lv.ColumnHeaderChanged += new ColumnHeaderChangedEventHandler(
      lv_ColumnHeaderChanged);

    ColumnHeader col1 = new ColumnHeader();
    col1.Text = "One";

    ColumnHeader col2 = new ColumnHeader();
    col2.Text = "Two";
    
    lv.Columns.Add(col1);
    lv.Columns.Add(col2);

    ResumeLayout();
  }

  private void lv_ColumnHeaderChanged(object sender,
    ColumnHeaderChangedEventArgs e)
  {
    Console.WriteLine("Column {0} changed: new width = {1}",
      e.ColumnHeader.Text, e.ColumnHeader.Width);
  }
}

public class MyListView : ListView
{
  private IntPtr hWndHeader;
  private bool allowResizing = true;
  
  public bool AllowResizing
  {
    get { return allowResizing; }
    set { allowResizing = value; }
  }

  protected override void OnHandleCreated(EventArgs e)
  {
    base.OnHandleCreated(e);
    hWndHeader = SendMessage(Handle, LVM_GETHEADER, 0, 0);
  }
  
  protected override void WndProc(ref Message m)
  {
    base.WndProc(ref m);
    if (m.Msg == WM_NOTIFY)
    {
      NMHDR hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
      if (hdr.hwndFrom == hWndHeader)
      {
        if (hdr.code == HDN_ITEMCHANGINGA ||
          hdr.code == HDN_ITEMCHANGINGW)
        {
          if (allowResizing) m.Result = FALSE;
          else m.Result = TRUE;
        }
        else if (hdr.code == HDN_ITEMCHANGEDA ||
          hdr.code == HDN_ITEMCHANGEDW)
        {
          NMHEADER header = (NMHEADER)
            Marshal.PtrToStructure(m.LParam, typeof(NMHEADER));

          int index = header.iItem;
          if (index >= 0 && index < Columns.Count)
          {
            ColumnHeader col = Columns[index];
            OnColumnHeaderChanged(
              new ColumnHeaderChangedEventArgs(col));
          }
        }
      }
    }
  }

  public event ColumnHeaderChangedEventHandler ColumnHeaderChanged;

  protected virtual void OnColumnHeaderChanged(ColumnHeaderChangedEventArgs e)
  {
    if (ColumnHeaderChanged != null)
      ColumnHeaderChanged(this, e);
  }

  private const int WM_NOTIFY = 78;
  private const int HDN_ITEMCHANGINGA = -300;
  private const int HDN_ITEMCHANGINGW = -320;
  private const int HDN_ITEMCHANGEDA = -301;
  private const int HDN_ITEMCHANGEDW = -321;
  private const int LVM_GETHEADER = 4127;
  private static readonly IntPtr FALSE = new IntPtr(0);
  private static readonly IntPtr TRUE = new IntPtr(1);

  [StructLayout(LayoutKind.Sequential)]
  public class NMHDR
  {
    public IntPtr hwndFrom;
    [MarshalAs(UnmanagedType.U4)] public int idFrom;
    [MarshalAs(UnmanagedType.U4)] public int code;
  }

  [StructLayout(LayoutKind.Sequential)]
  public class NMHEADER
  {
    public IntPtr hwndFrom;
    [MarshalAs(UnmanagedType.U4)] public int idFrom;
    [MarshalAs(UnmanagedType.U4)] public int code;
    public int iItem;
    public int iButton;
    public IntPtr pitem;
  }

  [DllImport("user32.dll")]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg,
    int wParam, int lParam);
}

public delegate void ColumnHeaderChangedEventHandler(object sender,
  ColumnHeaderChangedEventArgs e);

public class ColumnHeaderChangedEventArgs : EventArgs
{
  private ColumnHeader columnHeader;

  public ColumnHeaderChangedEventArgs(ColumnHeader header)
  {
    columnHeader = header;
  }

  public ColumnHeader ColumnHeader
  {
    get { return columnHeader; }
  }
}
To test this, copy it and compile it then run the application. As you can see you can resize the column headers. Now execute the program passing "false" as a command-line parameter and you'll see that you can't resize the column headers.

 

Microsoft MVP, Visual C#
My Articles
GeneralID3 algorithm Pin
Member 75113929-Apr-04 10:14
Member 75113929-Apr-04 10:14 
GeneralRe: ID3 algorithm Pin
Heath Stewart29-Apr-04 10:34
protectorHeath Stewart29-Apr-04 10:34 
GeneralRun time change Pin
boruk29-Apr-04 10:09
boruk29-Apr-04 10:09 
GeneralRe: Run time change Pin
Heath Stewart29-Apr-04 10:26
protectorHeath Stewart29-Apr-04 10:26 
QuestionHow to make the return key to click the OK button? Pin
Daniel Stagg29-Apr-04 8:50
Daniel Stagg29-Apr-04 8:50 
AnswerRe: How to make the return key to click the OK button? Pin
Dave Kreskowiak29-Apr-04 8:55
mveDave Kreskowiak29-Apr-04 8:55 
GeneralRe: How to make the return key to click the OK button? Pin
Heath Stewart29-Apr-04 9:24
protectorHeath Stewart29-Apr-04 9:24 
GeneralSocket Chat?! Pin
Morten Kristensen29-Apr-04 7:50
Morten Kristensen29-Apr-04 7:50 
GeneralRe: Socket Chat?! Pin
Heath Stewart29-Apr-04 8:36
protectorHeath Stewart29-Apr-04 8:36 
GeneralRe: Socket Chat?! Pin
Morten Kristensen30-Apr-04 4:08
Morten Kristensen30-Apr-04 4:08 
GeneralCreating an Access database at runtine Pin
bsargos29-Apr-04 7:11
bsargos29-Apr-04 7:11 
GeneralRe: Creating an Access database at runtine Pin
Heath Stewart29-Apr-04 8:33
protectorHeath Stewart29-Apr-04 8:33 
GeneralRe: Creating an Access database at runtine Pin
Dave Kreskowiak29-Apr-04 8:37
mveDave Kreskowiak29-Apr-04 8:37 
GeneralControl Box does not close form Pin
Darryl Borden29-Apr-04 6:52
Darryl Borden29-Apr-04 6:52 
GeneralRe: Control Box does not close form Pin
Heath Stewart29-Apr-04 8:31
protectorHeath Stewart29-Apr-04 8:31 
GeneralClient / Server Pin
Bonsai200429-Apr-04 6:20
Bonsai200429-Apr-04 6:20 
GeneralRe: Client / Server Pin
Heath Stewart29-Apr-04 6:46
protectorHeath Stewart29-Apr-04 6:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.