C# - ListView Column text size is not the same on each computer

烂漫一生 提交于 2019-12-25 04:17:58

问题


I am making a C# program with a listview and in my PC and netbook the columns sizes are perfect, but when I run the program in my friends computer the column's size appear to be smaller, the text is being cut, for example, if my column shows "Hello Stackoverflow" in my PC, in my friend's PC it shows "Hello Stac..." . The column size is set in the code, and he is not resizing...how may this be possible? We are using the same screen resoluiton (1080p), and even if he changes the resolution to something bigger (800x600), the column text is still cut out, just the window in general is bigger...may it be a default font from windows making all this problem?? Thank you all!!


回答1:


This may be a matter of the DPI setting. Your friend may have "large fonts" enabled. Setting fixed column widths, you need to take the DPI setting into account. 100 pixels are always 100 pixels, but changing the DPI setting makes 8pt font be larger on your friends machine than on yours.

You can easily compute this:

int columnWidth = Desired Width / 96.0 * DPI;

with Desired Width being the number of pixels you want the column to be wide and DPI being the horizontal DPI of the current machine.




回答2:


ListView doesn't override the ScaleControl method so it doesn't scale properly with DPI changes. You can do it in your form instead. This method doesn't require you to know the DPI setting. Code copied from MSDN forum.

private void ScaleListViewColumns(ListView listview, SizeF factor)
{
    foreach (ColumnHeader column in listview.Columns)
    {
        column.Width = (int)Math.Round(column.Width * factor.Width);
    }
}

protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
    base.ScaleControl(factor, specified);
    ScaleListViewColumns(listView1, factor);
}


来源:https://stackoverflow.com/questions/10795134/c-sharp-listview-column-text-size-is-not-the-same-on-each-computer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!