【问题标题】:C# ListView DrawSubitem Font changedC# ListView DrawSubitem 字体改变
【发布时间】:2014-10-12 11:34:46
【问题描述】:

我对函数“listView1_DrawSubItem”有疑问。我只更改第二列,因为我必须在第二列中放置一些图像。 问题出在字体上。当我画第二列时,字体比第一列更清晰。令人惊奇的是,它仅在我第一次打开图表表单时才出现。 如代码所示,第一列默认绘制,第二列由我绘制。

有这样的图像。以全分辨率观看。

这是我的代码:

fo 是我可以更改的字体。

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        if (e.Header != this.columnHeader2)
        {
            e.DrawDefault = true;
            return;
        }

        if (e.Item.SubItems[1].Text == "1")
        {
            e.DrawBackground();
            e.Graphics.DrawImage(Properties.Resources.Blank_Badge_Green, e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y, 10, 10);
        }
        else if (e.Item.SubItems[1].Text == "0")
        {
            e.DrawBackground();
            e.Graphics.DrawImage(Properties.Resources.Blank_Badge_Grey, e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y, 10, 10);
        }
        else
        {
            e.DrawBackground();
            e.Graphics.DrawString(e.SubItem.Text, fo, new SolidBrush(e.SubItem.ForeColor), e.SubItem.Bounds.Location.X, e.SubItem.Bounds.Location.Y);
        }
    }

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

【问题讨论】:

    标签: c# listview fonts charts


    【解决方案1】:
       e.Graphics.DrawString(...)
    

    两个问题。第一个是您使用的方法,ListView 在引擎盖下使用 TextRenderer.DrawText()。当您使用像 SysInternals 的 ZoomIt(推荐)这样的实用程序时,第二个问题很明显,您会看到呈现令人讨厌的文本时没有蓝色/红色抗锯齿像素。您需要设置 Graphics.TextRenderingHint 属性来避免这种情况。

    所以,大致:

    else
    {
        e.DrawBackground();
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        TextRenderer.DrawText(e.Graphics, ...);
    }
    

    【讨论】:

    • 关于 Treeview 的另一个问题。当我更新和更改复选框时它会闪烁。我使用属性 DoubleBuffered alredy: "PropertyInfo property1 = typeof(TreeView).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance); property1.SetValue(treeView1, true, null);"
    • 好的,我在TreeView Flickering找到它
    【解决方案2】:

    您很可能必须测试所绘制图形的各种 SmoothingMode:

        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
    

    尝试看看哪个与系统绘制的单元格的质量最匹配!

    在绘制文本之前设置它!

    理论上,其他一些属性可能会有所不同:

    int e.Graphics.TextContrast    // for adding a gamma correction
    e.Graphics.InterpolationMode   // for resizing images
    e.Graphics.CompositingMode     // for combining an image with the pixels below
    e.Graphics.CompositingQuality  // controls the quality thereof
    

    但很可能是 SmoothingMode。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多