简体   繁体   中英

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

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

To overcome this I believe I need to determine the listview dimensions at runtime and do some maths to set the panel min size but I can't see how to do this. Is this the right approach or is there an easier way?

Sounds like a Screen DPI issue. Winforms is a classic "PITA" when it comes to DPI and sizing of controls. In an integrated developement environment we has windows XP developers with 96 dpi screen resolution and Windows Vista Developers on Laptops with 120 dpi and it caused the Windows forms designers to get rearranged constantly. In the end we determined that all controls must use a multiple of 4 when setting size/location on a screen to minimise the issue.

To a lesser extent WPF addresses this issue and Vista/Windows7/XP application look basically the same on when different DPI's are used. But WindowsForms to WPF is not a straight forward technology change. I'm not sure you can get at the ListViewItem size easily in a Windows Forms application.

When I look at my screen printing code for Windows Forms it uses the Size method to get dimensions before I render it to a bitmap. Maybe that might 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 you could use. Eg

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

Also I think WPF would also give you some fairly flexible layout options for Lists. I regularly insert WPF controls, containing complex lists designs, into my WindowsForms application to get better layout control. But like I said, WPF is a different kettle of fish.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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