简体   繁体   English

如何在运行时确定.NET列表视图的尺寸?

[英]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). 我最初是通过直观地微调值(在我的XP开发机器上)来确定panel1MinSize和panel2MinSize的值的。 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. 在我在Windows Vista上测试我的应用程序之前,这种方法很好-列表视图的基本尺寸不同,并且列表视图太小。

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. 为了克服这个问题,我相信我需要在运行时确定listview的尺寸,并做一些数学运算来设置面板的最小尺寸,但是我看不到如何做到这一点。 Is this the right approach or is there an easier way? 这是正确的方法还是有更简单的方法?

Sounds like a Screen DPI issue. 听起来像是屏幕DPI问题。 Winforms is a classic "PITA" when it comes to DPI and sizing of controls. 当涉及DPI和控件大小时,Winforms是经典的“ PITA”。 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. 在集成开发环境中,我们拥有96 dpi屏幕分辨率的Windows XP开发人员和120 dpi笔记本电脑上的Windows Vista开发人员,这导致Windows窗体设计人员不断地重新安排。 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. 最后,我们确定在屏幕上设置大小/位置时,所有控件都必须使用4的倍数,以最大程度地减少问题。

To a lesser extent WPF addresses this issue and Vista/Windows7/XP application look basically the same on when different DPI's are used. 在较小程度上,WPF解决了此问题,并且使用不同的DPI时,Vista / Windows7 / XP应用程序看起来基本相同。 But WindowsForms to WPF is not a straight forward technology change. 但是WindowsForms到WPF并不是一项直接的技术更改。 I'm not sure you can get at the ListViewItem size easily in a Windows Forms application. 我不确定您是否可以在Windows Forms应用程序中轻松获得ListViewItem的大小。

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. 当我查看Windows窗体的屏幕打印代码时,在将其渲染为位图之前,它使用Size方法获取尺寸。 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. 在WPF中,ListViewItem包含您可以使用的更多详细信息。 Eg 例如

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

Also I think WPF would also give you some fairly flexible layout options for Lists. 另外,我认为WPF还可以为列表提供一些相当灵活的布局选项。 I regularly insert WPF controls, containing complex lists designs, into my WindowsForms application to get better layout control. 我定期将包含复杂列表设计的WPF控件插入WindowsForms应用程序中,以获得更好的布局控件。 But like I said, WPF is a different kettle of fish. 但是就像我说的那样,WPF是另一种鱼。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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