简体   繁体   English

将Windows窗体C#中的Listview列的文本对齐,而不更改标题文本对齐方式

[英]Align the text of a Listview Column in Windows Forms C# to right without changing the header text alignment

I have a System Windows Forms ListView in C# 2008 (Net 2.0) on Win7 which has 4 columns and is in details mode with small icons, fullrowselection on, hideselection false, showtooltips true. 我在Win7上的C#2008(Net 2.0)中有一个系统Windows窗体ListView,它有4列,并且处于详细模式,带有小图标,fullrowselection on,hideselection false,showtooltips true。

The column No. 3 shall be as the only column aligned to the right and not to the left like the others. 第3列应作为唯一的列与右侧对齐而不是与其他列对齐。 I have set the corresponding designer-property to textalignment right which is logical. 我已将相应的designer-property设置为textalignment right,这是合乎逻辑的。 But Microsoft disappoints here hardly: Microsoft Visual Studio also aligns the columns headline to the right which I don't want!!! 但微软在这里几乎没有让人失望:微软Visual Studio也将列标题与我不想要的权利排列在一起! Only all the elements shall be aligned right in the column No. 3, the headline texts of all the columns will all stay aligned left. 只有所有元素都应在第3列中对齐,所有列的标题文本都将保持对齐。 This seems to be not possible with the designer settings. 这似乎是设计师设置无法实现的。

I searched long in Google and found this - the code aligns my items how I want it, but the items text disappears and appears again, the highlighting and selection doesn't work partial the ListView disappears in parts or in a whole, the text renders weird and like not being sharp etc. etc. - the ListView stopped default behavior after I applied the code below. 我在谷歌长时间搜索并发现了这个 - 代码对齐我的项目我想要的方式,但项目文本消失并再次出现,突出显示和选择不起作用,ListView消失部分或整体,文本呈现奇怪,喜欢不锐利等等 - 在我应用下面的代码后,ListView停止了默认行为。 How can I solve this without this troubles (Ownerdraw is with the code below set to true. I have Fullrowselect, hideselection false and detailsmode on). 如果没有这个麻烦我怎么能解决这个问题(Ownerdraw将下面的代码设置为true。我有Fullrowselect,hideselection false和detailsmode on)。 Basically I need only the subitems in column 3 to be aligned right by my wish without the columnheaders textalignment changed, all other shall be default drawing - I don't wanna ever change any drawing - only the alignment shall be my custom one which I described above. 基本上我只需要按照我的意愿对第3列中的子项进行对齐,而不改变列标题文本对齐,所有其他都应该是默认绘图 - 我不想改变任何绘图 - 只有对齐应该是我自定义的一个我描述的以上。

 private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        TextFormatFlags flags = TextFormatFlags.Left;

        if (e.ColumnIndex == 3)
        {
            flags = TextFormatFlags.Right;
            e.DrawText(flags);
        }
        else
        {
            e.DrawDefault = true;
        }
    }

    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        e.DrawDefault = true;
    }

Instead of drawing the ColumnHeader as default while drawing all the items yourself, why not draw all the items as default and draw the ColumnHeader yourself? 相反绘制的ColumnHeader为默认值,而自己绘制的所有项目,为什么不画的所有项目的默认,绘制ColumnHeader自己呢? I've tried this code but the look and feel of the ColumnHeader when the mouse is over is not good, it's not like the default look of a ColumnHeader , to make it look similarly, I'm sure we need much more code, however it's not much different because of the very similar color I used. 我已经尝试过这段代码但是当鼠标结束时ColumnHeader的外观和感觉并不好,它不像ColumnHeader的默认外观,为了使它看起来类似,我相信我们需要更多的代码,不过因为我使用的颜色非常相似,所以没有太大的不同。 You can take more time to customize it. 您可以花更多时间来自定义它。 This is just a demo: 这只是一个演示:

public Form1(){
  InitializeComponent();
  listView1.OwnerDraw = true;
  invalidateHeaders = typeof(ListView).GetMethod("InvalidateColumnHeaders",
                                       System.Reflection.BindingFlags.NonPublic |
                                       System.Reflection.BindingFlags.Instance);
} 

bool hot;
System.Reflection.MethodInfo invalidateHeaders;
//DrawColumnHeader event handler
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
  if (e.Header.TextAlign == HorizontalAlignment.Right) {
      e.DrawBackground();
      e.DrawText(TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
      if (e.Bounds.Contains(listView1.PointToClient(MousePosition))) {
          bool selected = (e.State & ListViewItemStates.Selected) != 0;
          var solidColor = selected ? Color.FromArgb(30, Color.FromArgb(0, 200, 200)) :
                                      Color.FromArgb(30, Color.Aqua);
          var borderColor = selected ? Color.DarkGray : Color.Aqua;
          e.Graphics.FillRectangle(new SolidBrush(solidColor), e.Bounds);
          var rect = e.Bounds;
          rect.Width -= 2;
          rect.Height -= 2;                    
          ControlPaint.DrawBorder(e.Graphics, rect, 
                       Color.FromArgb(40, borderColor), ButtonBorderStyle.Solid);
          hot = true;
       }
       else hot = false;
   } else {
       e.DrawDefault = true;
       if (hot) {
          invalidateHeaders.Invoke(listView1, null);
          hot = false;
       }
   }
}
//DrawItem event handler
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) {
   e.DrawDefault = true;
}
//MouseMove event handler
private void listView1_MouseMove(object sender, MouseEventArgs e) {
   invalidateHeaders.Invoke(listView1, null);
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM