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

时间:2010-05-04 12:37:46

标签: c# .net listview

我有一个带有拆分容器的表单,每个面板都有一个列表视图和一些按钮。我想确保在listview中可以看到标题和至少两行。我最初确定了panel1MinSize&的值。 panel2MinSize通过可视化微调值(在我的XP开发机器上)。这种方法很好,直到我在Windows Vista上测试我的应用程序 - 列表视图的基本维度不同,列表视图太小。

为了克服这个问题,我相信我需要在运行时确定列表视图尺寸,并做一些数学设置面板最小尺寸,但我看不到如何做到这一点。这是正确的方法还是有更简单的方法?

1 个答案:

答案 0 :(得分:0)

听起来像是屏幕DPI问题。在DPI和控件大小方面,Winforms是一款经典的“PITA”。在集成开发环境中,我们拥有96 dpi屏幕分辨率的Windows XP开发人员和120 dpi的笔记本电脑上的Windows Vista开发人员,它使Windows表单设计人员不断重新排列。最后,我们确定在屏幕上设置大小/位置时,所有控件必须使用4的倍数才能最大限度地减少问题。

在较小程度上,WPF解决了这个问题,Vista / Windows7 / XP应用程序在使用不同的DPI时看起来基本相同。但WindowsForms到WPF并不是一项直接的技术变革。我不确定您是否可以在Windows窗体应用程序中轻松获得ListViewItem大小。

当我查看Windows窗体的屏幕打印代码时,它会使用Size方法获取尺寸,然后再将其渲染为位图。也许这可能有用。

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

在WPF中,ListViewItem包含您可以使用的更详细信息。 E.g。

System.Windows.Controls.ListView.ListViewItem.ActualHeight参见[MSDN] [1]

另外我认为WPF还会为列表提供一些相当灵活的布局选项。我经常将包含复杂列表设计的WPF控件插入到我的WindowsForms应用程序中,以获得更好的布局控制。但就像我说的那样,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