2010-02-12 3 views

Répondre

1

Si vous voulez des colonnes redimensionnables et d'autres non redimensionnable, utilisez ObjectListView. Dans ce contrôle, chaque colonne peut avoir une largeur Max/Min. Voir this recipe pour les instructions.

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

} 

Et Voir aussi ce

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

+0

ça ne m'aide pas beaucoup. J'utilise PowerShell. – Varyanica

Questions connexes