English 中文(简体)
ListView 列表所有者绘图和列自动恢复
原标题:ListView owner drawing and column auto-resizing
问题回答

没有一条信息可以告诉您列将自动调整大小。 但正如您所提到的, 用户只能用两种方式触发 : 双击分隔符, 和 < code> Ctrl- Shift- / code> 。 您可以截取这两条, 然后做任何你想做的事 。

步骤 # 1: 要拦截双击鼠标, 您需要使用子类 < code> ListView 并监听 < code> HDN_ DIVIDERDDDLLICK 通知 :

protected override void WndProc(ref Message m) {
    switch (m.Msg) {
        case 0x4E: // WM_NOTIFY
            if (!this.HandleNotify(ref m))
                base.WndProc(ref m);
            break;
        default:
            base.WndProc(ref m);
            break;
    }
}

protected bool HandleNotify(ref Message m) {

    const int HDN_DIVIDERDBLCLICKW = (HDN_FIRST - 25);

    NativeMethods.NMHEADER nmheader = (NativeMethods.NMHEADER)m.GetLParam(typeof(NativeMethods.NMHEADER));

    switch (nmheader.nhdr.code) {

        case HDN_DIVIDERDBLCLICKW:
            if (nmheader.iItem >= 0 && nmheader.iItem < this.Columns.Count) {
                this.AutoResizeColumn(nmheader.iItem);
                m.Result = (IntPtr)1; // prevent the change from happening
                return true;
            }
            break;

        default:
            break;
    }

    return false;
}

当然, 您必须执行 < code> AutoResize Column () 来做你想做的事 。

步骤 # 2 截取 Ctrl- Shift- + 时, 您需要推翻 < code> processKeyPreview 。 < a href=" http://www.codeproject. com/ artes/22570/ overriding- Keydown- a- User- control- untrol- user- Process" rel= "nown" 标题=" this art article" > 将显示您如何做到这一点。 您的覆盖中, 您会为每列调用 < code > AutResizecolumn () 。

Alternative approach

如果您构造您的 ListViewemts , 使每个子项的“ W” 字符串与所有者想要绘制的数据的长度相同, 则控制将使用这些字符串自动计算列。 这可能是一个简单的方法 。

您可以在以下两个场景中手动完成此操作 :


  • 如果您想要修正 minimimimwidth 最大Width ,

    Column Width changing 事件 ListView 中使用此功能:

   int minimumWidth = 200;
   int maximumWidth = 500;
   if (e.NewWidth  maximumWidth)
   {
      e.Cancel = true;
      e.NewWidth = maximumWidth;
   }

  • If you want to fix a ColumnWidth for the ListViewColumn, Set the desired ColumnWidth

    Column Width changing 事件 ListView 中使用此功能:

   ListView ColumnList = sender as ListView;
   e.Cancel = true;
   e.NewWidth = ColumnList.Columns[e.ColumnIndex].Width;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签