0

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!!

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • Is he using larger text? Many programs cant handle larger text sizes (Control Panel > Appearance and Personalization > Display). – sisve May 29 '12 at 08:00

2 Answers2

6

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);
}
user1318499
  • 1,327
  • 11
  • 33
4

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Ok...it does makes sense...I will try it out and let you know...btw, how do I get the machine's DPI? – Michel Feinstein May 29 '12 at 08:35
  • Thank you Thorsten!...but this still makes me wonder...how does the pc knows the DPI? DPI means dots per inch, and every monitor has its own pixel size, so to know how many dots windows will put in every inch, it has to know how many inches are in the screen right? But the only info windows gets it's the resolution of the monitor and the frequency isn't it? so how does it know how much is an inch in the screen? – Michel Feinstein May 29 '12 at 13:14
  • It doesn't. http://www.sturmnet.org/blog/2010/09/02/the-crime-that-is-windows-dpi-handling – Thorsten Dittmar May 29 '12 at 13:56
  • Oh I see! Btw, I tested everything in his pc and it was indeed a dpi problem! Everything is solved, thank you guys! – Michel Feinstein May 30 '12 at 08:22
  • Fellows, one last question...is it possible to go around this problem with images? I have some buttons with images on them, and the images appear a lot smaller in computers with a not default DPI – Michel Feinstein May 31 '12 at 01:38
  • Create a 96 DPI image and a 120 DPI image and use one for default DPI (<= 96.0) and the other for large DPI (> 96 DPI). – Thorsten Dittmar May 31 '12 at 06:36
  • So there is no "auto size" function right? Of course I could code it, but its just too much trouble... – Michel Feinstein Jun 01 '12 at 07:22