How do I determine the dimensions of a .NET listview at runtime?

I have a split container form and each panel has a list view and some buttons. I wanted to make sure the title and at least two lines are visible in the list. I originally determined the values ​​for panel1MinSize and panel2MinSize by fine-tuning the values ​​visually (on my XP development machine). This approach was fine until I tested my application on Windows Vista. The basic dimensions of the list look different, and the list is too small.

To overcome this, I believe I need to determine the dimensions of the list at runtime and do some math to set the minimum size for the panel, but I can't see how. Is this the correct approach or is there an easier way?

+2


a source to share


1 answer


Sounds like a DPI screen issue. Winforms is the classic "PITA" when it comes to DPI and control sizing. In the IDE, we have Windows XP developers at 96dpi and Windows Vista developers on 120dpi laptops, and this has caused Windows Forms designers to constantly rebuild. In the end, we specified that all controls should use a multiple of 4 when adjusting the size / location on the screen to minimize the problem.

To a lesser extent WPF solves this problem, and Vista / Windows7 / XP application looks mostly the same when different DPIs are used. But WindowsForms to WPF is not a direct technology transformation. I'm not sure if you can easily get the size of a ListViewItem in a Windows Forms Application.

When I look at my Windows Forms display screen, it uses the Size method to get the measurements before placing it in the bitmap. Maybe this could work.

Public Class PrintScreen

    '   Code that explicity finds the outter rectangle size of the current form and then 
    '   takes a screen print of that image.
    Shared Sub CurrentForm(ByRef myForm As Windows.Forms.Form)
        Dim memoryImage As Bitmap

        Dim myGraphics As Graphics = myForm.CreateGraphics()
        Dim s As Size = myForm.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(myForm.Location.X, myForm.Location.Y, 0, 0, s)
        memoryGraphics.DrawImage(memoryImage, 0, 0)

        SaveImage(memoryImage, myForm)
    End Sub

    '   Method to save the screen to a file in the \My Pictures\AppName\ folder
    Private Shared Sub SaveImage(ByVal b As Bitmap, ByVal form As Windows.Forms.Form)
        Dim AppPictureFolder As String
        Dim fileName As String
        '   Create a file with the forms title + datetime stamp.
        fileName = String.Format("{0} {1}.png", form.Text, Date.Now.ToString("yyyyMMdd HHmmss"))
        AppPictureFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), [Assembly].GetExecutingAssembly.GetName.Name)
        If Not System.IO.Directory.Exists(AppPictureFolder) Then
            System.IO.Directory.CreateDirectory(AppPictureFolder)
        End If
        b.Save(System.IO.Path.Combine(AppPictureFolder, fileName))
        System.Diagnostics.Process.Start(System.IO.Path.Combine(AppPictureFolder, fileName))
    End Sub
End Class

      



In WPF, ListViewItem contains much more detailed information that you could use. For instance.

System.Windows.Controls.ListView.ListViewItem.ActualHeight See [MSDN] [1]

Also I think WPF will give you some pretty flexible options for the layout of lists as well. I regularly insert WPF controls that contain complex lists of lists into my WindowsForms application to improve layout control. But as I said, WPF is a different fish kettle.

[1]: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.WINDOWS.FRAMEWORKELEMENT.ACTUALHEIGHTPROPERTY%29;k%28TargetFrameworkMoniker-%OREWORK.NET % 3dV3.5% 22% 29; k% 28DevLang-VB% 29 & rd = true "MSDN

0


a source







All Articles