Je veux désactiver le redimensionnement des colonnes de listview. Comment puis-je le faire ?
Réponses
Trop de publicités?
ratty
Points
3926
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 aussi voir ceci
Si vous souhaitez que certaines colonnes soient redimensionnables et que d'autres ne le soient pas, utilisez la fonction ObjectListView . Dans ce contrôle, on peut donner à chaque colonne une largeur Max/Min. Voir cette recette pour les instructions.